Created
November 11, 2013 04:06
-
-
Save viblo/7407666 to your computer and use it in GitHub Desktop.
swagger models problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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