You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.0KB

  1. package rex
  2. import (
  3. "fmt"
  4. "net/http"
  5. "reflect"
  6. "runtime"
  7. "time"
  8. "github.com/gorilla/mux"
  9. )
  10. type Server struct {
  11. middleware *middleware
  12. mux *mux.Router
  13. ready bool
  14. subservers []*Server
  15. }
  16. func New() *Server {
  17. return &Server{
  18. middleware: new(middleware),
  19. mux: mux.NewRouter().StrictSlash(true),
  20. }
  21. }
  22. // build constructs all server/subservers along with their middleware modules chain.
  23. func (self *Server) build() http.Handler {
  24. if !self.ready {
  25. // * add server mux into middlware stack to serve as final http.Handler.
  26. self.Use(func(http.Handler) http.Handler {
  27. return self.mux
  28. })
  29. // * add subservers into middlware stack to serve as final http.Handler.
  30. for index := 0; index < len(self.subservers); index++ {
  31. server := self.subservers[index]
  32. server.Use(func(http.Handler) http.Handler {
  33. return server.mux
  34. })
  35. }
  36. self.ready = true
  37. }
  38. return self.middleware
  39. }
  40. // register adds the http.Handler/http.HandleFunc into Gorilla mux.
  41. func (self *Server) register(pattern string, handler interface{}, methods ...string) {
  42. // finds the full function name (with package) as its mappings.
  43. var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()
  44. switch H := handler.(type) {
  45. case http.Handler:
  46. self.mux.Handle(pattern, H).Methods(methods...).Name(name)
  47. case func(http.ResponseWriter, *http.Request):
  48. self.mux.HandleFunc(pattern, H).Methods(methods...).Name(name)
  49. default:
  50. Fatalf("Unsupported handler (%s) passed in.", name)
  51. }
  52. }
  53. // Any maps most common HTTP methods request to the given `http.Handler`.
  54. // Supports: GET | POST | PUT | DELETE | OPTIONS | HEAD
  55. func (self *Server) Any(pattern string, handler interface{}) {
  56. self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD")
  57. }
  58. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  59. // it also fetch the full function name of the handler (with package) to name the route.
  60. func (self *Server) Get(pattern string, handler interface{}) {
  61. self.register(pattern, handler, "GET")
  62. }
  63. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  64. // it also fetch the full function name of the handler (with package) to name the route.
  65. func (self *Server) Head(pattern string, handler interface{}) {
  66. self.register(pattern, handler, "HEAD")
  67. }
  68. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  69. // it also fetch the full function name of the handler (with package) to name the route.
  70. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  71. func (self *Server) Options(pattern string, handler interface{}) {
  72. self.register(pattern, handler, "OPTIONS")
  73. }
  74. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  75. // it also fetch the full function name of the handler (with package) to name the route.
  76. func (self *Server) Post(pattern string, handler interface{}) {
  77. self.register(pattern, handler, "POST")
  78. }
  79. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  80. // it also fetch the full function name of the handler (with package) to name the route.
  81. func (self *Server) Put(pattern string, handler interface{}) {
  82. self.register(pattern, handler, "PUT")
  83. }
  84. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  85. // it also fetch the full function name of the handler (with package) to name the route.
  86. func (self *Server) Delete(pattern string, handler interface{}) {
  87. self.register(pattern, handler, "DELETE")
  88. }
  89. // Group creates a new application group under the given path prefix.
  90. func (self *Server) Group(prefix string) *Server {
  91. var middleware = new(middleware)
  92. self.mux.PathPrefix(prefix).Handler(middleware)
  93. var mux = self.mux.PathPrefix(prefix).Subrouter()
  94. server := &Server{middleware: middleware, mux: mux}
  95. self.subservers = append(self.subservers, server)
  96. return server
  97. }
  98. // Name returns route name for the given request, if any.
  99. func (self *Server) Name(r *http.Request) (name string) {
  100. var match mux.RouteMatch
  101. if self.mux.Match(r, &match) {
  102. name = match.Route.GetName()
  103. }
  104. return name
  105. }
  106. // Use add the middleware module into the stack chain.
  107. func (self *Server) Use(module func(http.Handler) http.Handler) {
  108. self.middleware.stack = append(self.middleware.stack, module)
  109. }
  110. // ServeHTTP dispatches the request to the handler whose
  111. // pattern most closely matches the request URL.
  112. func (self *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  113. self.build().ServeHTTP(w, r)
  114. }
  115. // Run starts the application server to serve incoming requests at the given address.
  116. func (self *Server) Run() {
  117. configure()
  118. runtime.GOMAXPROCS(maxprocs)
  119. go func() {
  120. time.Sleep(500 * time.Millisecond)
  121. Infof("Application server is listening at %d", port)
  122. }()
  123. if err := http.ListenAndServe(fmt.Sprintf(":%d", port), self); err != nil {
  124. Fatalf("Failed to start the server: %v", err)
  125. }
  126. }
  127. // Vars returns the route variables for the current request, if any.
  128. func (self *Server) Vars(r *http.Request) map[string]string {
  129. return mux.Vars(r)
  130. }