kratos-swagger-ui
How To Use
at the first, you need install this lib:
go get -u github.com/tx7do/kratos-swagger-ui
direct use:
package main
import (
"net/http"
swaggerUI "github.com/tx7do/kratos-swagger-ui"
)
func main() {
swaggerHandler := swaggerUI.New(
"Petstore",
"https://petstore3.swagger.io/api/v3/openapi.json",
"/docs/",
)
http.Handle("/docs/", swaggerHandler)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("Hello World!"))
})
println("docs at http://localhost:8080/docs/")
_ = http.ListenAndServe("localhost:8080", http.DefaultServeMux)
}
use it in kratos:
package server
import (
rest "github.com/go-kratos/kratos/v2/transport/http"
swaggerUI "github.com/tx7do/kratos-swagger-ui"
)
func NewRESTServer() *rest.Server {
srv := CreateRestServer()
swaggerHandler := swaggerUI.New(
"Petstore",
"https://petstore3.swagger.io/api/v3/openapi.json",
"/docs/",
)
srv.HandlePrefix("/docs/", swaggerHandler)
}
or
package server
import (
rest "github.com/go-kratos/kratos/v2/transport/http"
swaggerUI "github.com/tx7do/kratos-swagger-ui"
)
func NewRESTServer() *rest.Server {
srv := CreateRestServer()
swaggerUI.RegisterSwaggerUIServer(
srv,
"Petstore",
"https://petstore3.swagger.io/api/v3/openapi.json",
"/docs/",
)
}
Test Data
OpenAPI v2
- Petstore JSON: https://petstore.swagger.io/v2/swagger.json
- Petstore YAML: https://petstore.swagger.io/v2/swagger.yaml
OpenAPI v3
- Petstore JSON: https://petstore3.swagger.io/api/v3/openapi.json
- Petstore YAML: https://petstore3.swagger.io/api/v3/openapi.yaml