简体   繁体   English

如何将 swagger ui 与 golang 和 goa 集成

[英]How to integrate swagger ui with golang and 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 .我一直在建设有去和Goa我的第一个RESTful服务,所以基本上我开始做这个,并在年底,GOA生成两个文件招摇, swagger.yamlswagger.json The thing is that I've been looking for a way to add the swagger ui to, let's say, the index.问题是我一直在寻找一种方法来将 swagger ui 添加到索引中。 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?我还查看了示例设计 - swagger部分,但他们给我的唯一建议是使用goa swagger 设计生成器,但我不知道是否有办法执行此操作而无需访问公共主机?

TL;DR TL; 博士

The files example shows how to build an API that serves static assets, which can be adapted to including Swager-UI.文件示例展示了如何构建一个为静态资产提供服务的 API,该 API 可以适应包含 Swager-UI。

In order to follow the example:为了遵循这个例子:

  1. Get the dependencies获取依赖
  2. Add Resource s to the design.go filedesign.go文件中添加Resource
  3. Mount controllers in the main.gomain.go挂载控制器
  4. Download and edit Swagger-UI下载和编辑 Swagger-UI
  5. Run the generators运行生成器

After this you should have a working REST-API including the Swagger-UI在此之后,您应该有一个有效的 REST-API,包括 Swagger-UI

Details详情

(Please replace example.com/your-package/ with the path to your package). (请将example.com/your-package/替换为您的包的路径)。

1. Get the dependencies 1. 获取依赖

In order to serve files, the example uses:为了提供文件,该示例使用:

  • go-bindata
    Small utility which generates Go code from any file.从任何文件生成 Go 代码的小实用程序。 Useful for embedding binary data in a Go program.用于在 Go 程序中嵌入二进制数据。

  • go-bindata-assetfs
    Serves embedded files from go-bindata with net/http .使用net/http提供来自go-bindata嵌入文件。

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: 2.在design.go文件中添加Resources:

    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 3. 在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 4. 下载 Swagger-UI

Use your favorite method to download the dist folder of Swagger UI.使用你喜欢的方法下载 Swagger UI 的dist文件夹 Place all the files in the example.com/your-package/public/swagger folder.将所有文件放在example.com/your-package/public/swagger文件夹中。

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

  • Search for http://petstore.swagger.io/v2/swagger.json搜索http://petstore.swagger.io/v2/swagger.json
  • Replace it with ./swagger.json ../swagger.json替换它。
    That way the generated swagger.json will be used instead of the Petstore example.这样,将使用生成的swagger.json而不是 Petstore 示例。

5. Run the generators 5. 运行生成器

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.您现在应该拥有一个功能齐全的 Swagger UI。

I was running this recently and also wondered how to get the swagger ui up.我最近在运行这个,也想知道如何让 swagger ui 起来。 Once you've generated the openapi3.json files, you can use this docker-compose config to bring up the swagger ui.生成openapi3.json文件后,您可以使用此openapi3.json -compose 配置来调出 swagger ui。 You can replace the mounted volume and the SWAGGER_JSON variable according to where the files are.您可以根据文件所在的位置替换安装的卷和 SWAGGER_JSON 变量。 Swagger ui should run okay in http://localhost:8080. Swagger ui 应该可以在 http://localhost:8080 中正常运行。

Note: You might need to enable CORS in goa to execute the requests.注意:您可能需要在 goa 中启用 CORS 才能执行请求。

swagger:
  image: swaggerapi/swagger-ui
  ports:
    - '8080:8080'
  environment:
    SWAGGER_JSON: '/openapi3.json'
  volumes:
    - ./gen/http/openapi3.json:/openapi3.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM