既然goa框架自动生成啦swagger-json文件,那么如何用swagger-ui展示出来呢?
这里分三步:
1.下载swagger-ui的web代码
2.添加swagger.json 和 swagger-ui资源的导出
3.main.go里面mount这两个资源,然后编译启动程序,访问即可
为什么连swagger-ui一并导出?因为在swagger-ui中的test程序,需要请求api,如果时部署在不同端口,会有跨域请求问题(这个坑我踩了)。
跨域请求解决有很多方法:
1)把所有api设置为可接受跨域请求
2)把程序和swagger-ui部署到同一个域名下(或者设置代理访问)
3)其它
这里选择了第2种方法(顺便使用goa里面到Files这个controller)。
不多废话,继续。。。
1.下载swagger-ui的web代码
直接从swagger-ui的git上拉下来,然后我们要用的就是dist目录里头的文件。
2.添加swagger.json 和 swagger-ui资源的导出
package design import (
. "github.com/goadesign/goa/design"
. "github.com/goadesign/goa/design/apidsl"
) //这里的host会生成swagger host
var _ = API("adder", func() {
Title("The adder API")
Description("A teaser for goa")
Host("localhost:8080")
Scheme("http")
}) var _ = Resource("operands", func() {
Action("add", func() {
Routing(GET("add/:left/:right"))
Description("add returns the sum of the left and right parameters in the response body")
Params(func() {
Param("left", Integer, "Left operand")
Param("right", Integer, "Right operand")
})
Response(OK, "text/plain")
})
Action("des", func() {
Routing(GET("des/:left/:right"))
Description("des returns the sum of the left and right parameters in the response body")
Params(func() {
Param("left", Integer, "Left operand")
Param("right", Integer, "Right operand")
})
Response(OK, "text/plain")
})
})
//这里,添加josn和swagger-ui的资源导出
var _ = Resource("swagger", func() {
Origin("*", func() {
Methods("GET") // Allow all origins to retrieve the Swagger JSON (CORS)
})
Files("/swagger.json", "swagger/swagger.json")
}) var _ = Resource("swagger-ui", func() { //建立静态httpserver
Files("/swagger-ui/*filepath", "swagger-ui/")
})
然后重新生成代码:
~/gocode/src/playgoa/demo$ goagen bootstrap -d playgoa/demo/design
这个时候,会有两个新的文件:
swagger.go
swagger-ui.go
3.main.go里面mount这两个资源,然后编译启动程序,访问即可
然后,需要修改main.go文件
package main import (
"playgoa/demo/app" "github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
) func main() {
// Create service
service := goa.New("adder") // Mount middleware
service.Use(middleware.RequestID())
service.Use(middleware.LogRequest(true))
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover()) // Mount "operands" controller
c := NewOperandsController(service)
app.MountOperandsController(service, c) //add swagger
s := NewSwaggerController(service)
app.MountSwaggerController(service, s) //add swagger-ui
sui := NewSwaggerUIController(service)
app.MountSwaggerUIController(service, sui) // Start service
if err := service.ListenAndServe(":8080"); err != nil {
service.LogError("startup", "err", err)
}
}
然后编译程序:
~/gocode/src/playgoa/demo$ go build -o playgoa
这里别忘记了把swagger-ui中的dist目录下的所有文件,copy到demo目录下的swagger-ui下哦
qingping.zhang@bogon ~/gocode/src/playgoa/demo$ ll
total
drwxr-xr-x qingping.zhang staff : app
drwxr-xr-x qingping.zhang staff : client
drwxr-xr-x qingping.zhang staff : design
-rw-r--r-- qingping.zhang staff : main.go
-rw-r--r-- qingping.zhang staff : operands.go
-rwxr-xr-x qingping.zhang staff : playgoa
drwxr-xr-x qingping.zhang staff : swagger
drwxr-xr-x qingping.zhang staff : swagger-ui
-rw-r--r-- qingping.zhang staff : swagger-ui.go
-rw-r--r-- qingping.zhang staff : swagger.go #在这里。。。
qingping.zhang@bogon ~/gocode/src/playgoa/demo$ ll swagger-ui
total
drwxr-xr-x qingping.zhang staff : css
drwxr-xr-x qingping.zhang staff : fonts
drwxr-xr-x qingping.zhang staff : images
-rw-r--r-- qingping.zhang staff : index.html
drwxr-xr-x qingping.zhang staff : lang
drwxr-xr-x qingping.zhang staff : lib
-rw-r--r-- qingping.zhang staff : o2c.html
-rw-r--r-- qingping.zhang staff : swagger-ui.js
-rw-r--r-- qingping.zhang staff : swagger-ui.min.js
启动运行./playgoa
然后访问:http://localhost:8080/swagger-ui/index.html 就可以看到效果了。