restful-curly-router.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "github.com/emicklei/go-restful"
  4. "log"
  5. "net/http"
  6. )
  7. // This example has the same service definition as restful-user-resource
  8. // but uses a different router (CurlyRouter) that does not use regular expressions
  9. //
  10. // POST http://localhost:8080/users
  11. // <User><Id>1</Id><Name>Melissa Raspberry</Name></User>
  12. //
  13. // GET http://localhost:8080/users/1
  14. //
  15. // PUT http://localhost:8080/users/1
  16. // <User><Id>1</Id><Name>Melissa</Name></User>
  17. //
  18. // DELETE http://localhost:8080/users/1
  19. //
  20. type User struct {
  21. Id, Name string
  22. }
  23. type UserResource struct {
  24. // normally one would use DAO (data access object)
  25. users map[string]User
  26. }
  27. func (u UserResource) Register(container *restful.Container) {
  28. ws := new(restful.WebService)
  29. ws.
  30. Path("/users").
  31. Consumes(restful.MIME_XML, restful.MIME_JSON).
  32. Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well
  33. ws.Route(ws.GET("/{user-id}").To(u.findUser))
  34. ws.Route(ws.POST("").To(u.updateUser))
  35. ws.Route(ws.PUT("/{user-id}").To(u.createUser))
  36. ws.Route(ws.DELETE("/{user-id}").To(u.removeUser))
  37. container.Add(ws)
  38. }
  39. // GET http://localhost:8080/users/1
  40. //
  41. func (u UserResource) findUser(request *restful.Request, response *restful.Response) {
  42. id := request.PathParameter("user-id")
  43. usr := u.users[id]
  44. if len(usr.Id) == 0 {
  45. response.AddHeader("Content-Type", "text/plain")
  46. response.WriteErrorString(http.StatusNotFound, "User could not be found.")
  47. } else {
  48. response.WriteEntity(usr)
  49. }
  50. }
  51. // POST http://localhost:8080/users
  52. // <User><Id>1</Id><Name>Melissa Raspberry</Name></User>
  53. //
  54. func (u *UserResource) updateUser(request *restful.Request, response *restful.Response) {
  55. usr := new(User)
  56. err := request.ReadEntity(&usr)
  57. if err == nil {
  58. u.users[usr.Id] = *usr
  59. response.WriteEntity(usr)
  60. } else {
  61. response.AddHeader("Content-Type", "text/plain")
  62. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  63. }
  64. }
  65. // PUT http://localhost:8080/users/1
  66. // <User><Id>1</Id><Name>Melissa</Name></User>
  67. //
  68. func (u *UserResource) createUser(request *restful.Request, response *restful.Response) {
  69. usr := User{Id: request.PathParameter("user-id")}
  70. err := request.ReadEntity(&usr)
  71. if err == nil {
  72. u.users[usr.Id] = usr
  73. response.WriteHeader(http.StatusCreated)
  74. response.WriteEntity(usr)
  75. } else {
  76. response.AddHeader("Content-Type", "text/plain")
  77. response.WriteErrorString(http.StatusInternalServerError, err.Error())
  78. }
  79. }
  80. // DELETE http://localhost:8080/users/1
  81. //
  82. func (u *UserResource) removeUser(request *restful.Request, response *restful.Response) {
  83. id := request.PathParameter("user-id")
  84. delete(u.users, id)
  85. }
  86. func main() {
  87. wsContainer := restful.NewContainer()
  88. wsContainer.Router(restful.CurlyRouter{})
  89. u := UserResource{map[string]User{}}
  90. u.Register(wsContainer)
  91. log.Printf("start listening on localhost:8080")
  92. server := &http.Server{Addr: ":8080", Handler: wsContainer}
  93. log.Fatal(server.ListenAndServe())
  94. }