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?
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:
Resource
s to the design.go
filemain.go
After this you should have a working REST-API including the Swagger-UI
(Please replace example.com/your-package/
with the path to your package).
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/...
design.go
file: var _ = Resource("schema", func() {
Files("/schema/*filepath", "public/schema/")
})
var _ = Resource("swagger", func() {
Files("/swagger/*filepath", "public/swagger/")
})
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)
// ...
}
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:
http://petstore.swagger.io/v2/swagger.json
./swagger.json
.swagger.json
will be used instead of the Petstore example.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.
You should now have a fully functional Swagger UI.