Skip to content

Instantly share code, notes, and snippets.

@viblo
Created November 11, 2013 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viblo/7407666 to your computer and use it in GitHub Desktop.
Save viblo/7407666 to your computer and use it in GitHub Desktop.
swagger models problem
package main
import (
"log"
"net/http"
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful/swagger"
)
type User struct {
Id, Name string
}
func findUser(request *restful.Request, response *restful.Response) {
response.WriteErrorString(http.StatusNotFound, "Not implemented")
}
func main() {
wsContainer := restful.NewContainer()
ws := new(restful.WebService)
ws.Path("/users")
ws.Route(ws.GET("/{user-id}").To(findUser).
// docs
Doc("get a user").
Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")).
Writes(User{})) // on the response
wsContainer.Add(ws)
// Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API
// You need to download the Swagger HTML5 assets and change the FilePath location in the config below.
// Open http://localhost:8080/apidocs and enter http://localhost:8080/apidocs.json in the api input field.
config := swagger.Config{
WebServices: wsContainer.RegisteredWebServices(), // you control what services are visible
WebServicesUrl: "http://localhost:8080",
ApiPath: "/apidocs.json",
// Optionally, specifiy where the UI is located
SwaggerPath: "/apidocs/",
SwaggerFilePath: "/Users/blomqvist/go-test/swagger-ui/dist"}
swagger.RegisterSwaggerService(config, wsContainer)
server := &http.Server{Addr: ":8080", Handler: wsContainer}
log.Fatal(server.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment