dpyic24480 2018-02-14 16:49
浏览 291
已采纳

如何将swagger ui与golang和goa集成

I have been building my first RESTful service with go and goa, so basically I started doing this and at the end, goa generate two swagger files, swagger.yaml and swagger.json. The thing is that I've been looking for a way to add the swagger ui to, let's say, the index. But I haven't been able to find an example for that.

I also look in the examples and in the design - swagger section but the only suggestion that they gave me is to use the goa swagger design generator, but I don't know if there's a way to perform this action without the need of accessing a public host?

  • 写回答

1条回答 默认 最新

  • dqfkd82886 2018-02-22 16:08
    关注

    TL;DR

    The files example shows how to build an API that serves static assets, which can be adapted to including Swager-UI.

    In order to follow the example:

    1. Get the dependencies
    2. Add Resources to the design.go file
    3. Mount controllers in the main.go
    4. Download and edit Swagger-UI
    5. Run the generators

    After this you should have a working REST-API including the Swagger-UI

    Details

    (Please replace example.com/your-package/ with the path to your package).

    1. Get the dependencies

    In order to serve files, the example uses:

    • go-bindata
      Small utility which generates Go code from any file. Useful for embedding binary data in a Go program.

    • go-bindata-assetfs
      Serves embedded files from go-bindata with net/http.

    Get these using:

    go get github.com/a-urth/go-bindata/...
    go get github.com/elazarl/go-bindata-assetfs/...
    

    2. Add Resources to the design.go file:

        var _ = Resource("schema", func() {
            Files("/schema/*filepath", "public/schema/")
        })
    
        var _ = Resource("swagger", func() {
            Files("/swagger/*filepath", "public/swagger/")
        })
    

    3. Mount controllers in the main.go

    import (
        "net/http"
    
        "example.com/your-package/app"
        "example.com/your-package/public/swagger"
    
        "github.com/elazarl/go-bindata-assetfs"
        "github.com/goadesign/goa"
        "github.com/goadesign/goa/middleware"
    )
    
    func main() {
        // ...
    
        // Mount "schema" controller
        c1 := NewSchemaController(service)
        app.MountSchemaController(service, c1)
        // Mount "swagger" controller
        c2 := NewSwaggerController(service)
        // You can override FileSystem of the controller.
        // For example using github.com/elazarl/go-bindata-assetfs is like below.
        c2.FileSystem = func(dir string) http.FileSystem {
            return &assetfs.AssetFS{
                Asset:     swagger.Asset,
                AssetDir:  swagger.AssetDir,
                AssetInfo: swagger.AssetInfo,
                Prefix:    dir,
            }
        }
        app.MountSwaggerController(service, c2)
    
        // ...
    }
    

    4. Download Swagger-UI

    Use your favorite method to download the dist folder of Swagger UI. Place all the files in the example.com/your-package/public/swagger folder.

    In the example.com/your-package/public/swagger/index.html file:

    • Search for http://petstore.swagger.io/v2/swagger.json
    • Replace it with ./swagger.json.
      That way the generated swagger.json will be used instead of the Petstore example.

    5. Run the generators

    go generate goagen -d example.com/your-package/design app
    go generate goagen -d example.com/your-package/design main
    go generate goagen -d example.com/your-package/design swagger -o public
    go generate goagen -d example.com/your-package/design schema -o public
    go generate go-bindata -ignore 'bindata.go' -pkg swagger -o public/swagger/bindata.go ./public/swagger/...
    

    This will create the controllers, swagger, schema and static content.

    Done

    You should now have a fully functional Swagger UI.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题