restful-user-service.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/emicklei/go-restful"
  6. "github.com/emicklei/go-restful/swagger"
  7. )
  8. // This example is functionally the same as the example in restful-user-resource.go
  9. // with the only difference that is served using the restful.DefaultContainer
  10. type User struct {
  11. Id, Name string
  12. }
  13. type UserService struct {
  14. // normally one would use DAO (data access object)
  15. users map[string]User
  16. }
  17. func (u UserService) Register() {
  18. ws := new(restful.WebService)
  19. ws.
  20. Path("/users").
  21. Consumes(restful.MIME_XML, restful.MIME_JSON).
  22. Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well
  23. ws.Route(ws.GET("/").To(u.findAllUsers).
  24. // docs
  25. Doc("get all users").
  26. Operation("findAllUsers").
  27. Returns(200, "OK", []User{}))
  28. ws.Route(ws.GET("/{user-id}").To(u.findUser).
  29. // docs
  30. Doc("get a user").
  31. Operation("findUser").
  32. Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")).
  33. Writes(User{})) // on the response
  34. ws.Route(ws.PUT("/{user-id}").To(u.updateUser).
  35. // docs
  36. Doc("update a user").
  37. Operation("updateUser").
  38. Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")).
  39. Reads(User{})) // from the request
  40. ws.Route(ws.PUT("").To(u.createUser).
  41. // docs
  42. Doc("create a user").
  43. Operation("createUser").
  44. Reads(User{})) // from the request
  45. ws.Route(ws.DELETE("/{user-id}").To(u.removeUser).
  46. // docs
  47. Doc("delete a user").
  48. Operation("removeUser").
  49. Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")))
  50. restful.Add(ws)
  51. }
  52. // GET http://localhost:8080/users
  53. //
  54. func (u UserService) findAllUsers(request *restful.Request, response *restful.Response) {
  55. response.WriteEntity(u.users)
  56. }
  57. // GET http://localhost:8080/users/1
  58. //
  59. func (u UserService) findUser(request *restful.Request, response *restful.Response) {
  60. id := request.PathParameter("user-id")
  61. usr := u.users[id]
  62. if len(usr.Id) == 0 {
  63. response.WriteErrorString(http.StatusNotFound, "User could not be found.")
  64. } else {
  65. response.WriteEntity(usr)
  66. }
  67. }
  68. // PUT http://localhost:8080/users/1
  69. // <User><Id>1</Id><Name>Melissa Raspberry</Name></User>
  70. //
  71. func (u *UserService) updateUser(request *restful.Request, response *restful.Response) {
  72. usr := new(User)
  73. err := request.ReadEntity(&usr)
  74. if err == nil {
  75. u.users[usr.Id] = *usr
  76. response.WriteEntity(usr)
  77. } else {
  78. response.WriteError(http.StatusInternalServerError, err)
  79. }
  80. }
  81. // PUT http://localhost:8080/users/1
  82. // <User><Id>1</Id><Name>Melissa</Name></User>
  83. //
  84. func (u *UserService) createUser(request *restful.Request, response *restful.Response) {
  85. usr := User{Id: request.PathParameter("user-id")}
  86. err := request.ReadEntity(&usr)
  87. if err == nil {
  88. u.users[usr.Id] = usr
  89. response.WriteHeaderAndEntity(http.StatusCreated, usr)
  90. } else {
  91. response.WriteError(http.StatusInternalServerError, err)
  92. }
  93. }
  94. // DELETE http://localhost:8080/users/1
  95. //
  96. func (u *UserService) removeUser(request *restful.Request, response *restful.Response) {
  97. id := request.PathParameter("user-id")
  98. delete(u.users, id)
  99. }
  100. func main() {
  101. u := UserService{map[string]User{}}
  102. u.Register()
  103. // Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API
  104. // You need to download the Swagger HTML5 assets and change the FilePath location in the config below.
  105. // Open http://localhost:8080/apidocs and enter http://localhost:8080/apidocs.json in the api input field.
  106. config := swagger.Config{
  107. WebServices: restful.RegisteredWebServices(), // you control what services are visible
  108. WebServicesUrl: "http://localhost:8080",
  109. ApiPath: "/apidocs.json",
  110. // Optionally, specifiy where the UI is located
  111. SwaggerPath: "/apidocs/",
  112. SwaggerFilePath: "/Users/emicklei/Projects/swagger-ui/dist"}
  113. swagger.InstallSwaggerService(config)
  114. log.Printf("start listening on localhost:8080")
  115. log.Fatal(http.ListenAndServe(":8080", nil))
  116. }