您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

150 行
4.7KB

  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(method string, pattern string, handler interface{}) {
  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(method).Name(name)
  47. case func(http.ResponseWriter, *http.Request):
  48. self.mux.HandleFunc(pattern, H).Methods(method).Name(name)
  49. default:
  50. Fatalf("Unsupported handler (%s) passed in.", name)
  51. }
  52. }
  53. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  54. // it also fetch the full function name of the handler (with package) to name the route.
  55. func (self *Server) Get(pattern string, handler interface{}) {
  56. self.register("GET", pattern, handler)
  57. }
  58. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  59. // it also fetch the full function name of the handler (with package) to name the route.
  60. func (self *Server) Head(pattern string, handler interface{}) {
  61. self.register("HEAD", pattern, handler)
  62. }
  63. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  64. // it also fetch the full function name of the handler (with package) to name the route.
  65. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  66. func (self *Server) Options(pattern string, handler interface{}) {
  67. self.register("OPTIONS", pattern, handler)
  68. }
  69. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  70. // it also fetch the full function name of the handler (with package) to name the route.
  71. func (self *Server) Post(pattern string, handler interface{}) {
  72. self.register("POST", pattern, handler)
  73. }
  74. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  75. // it also fetch the full function name of the handler (with package) to name the route.
  76. func (self *Server) Put(pattern string, handler interface{}) {
  77. self.register("PUT", pattern, handler)
  78. }
  79. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  80. // it also fetch the full function name of the handler (with package) to name the route.
  81. func (self *Server) Delete(pattern string, handler interface{}) {
  82. self.register("Delete", pattern, handler)
  83. }
  84. // Group creates a new application group under the given path prefix.
  85. func (self *Server) Group(prefix string) *Server {
  86. var middleware = new(middleware)
  87. self.mux.PathPrefix(prefix).Handler(middleware)
  88. var mux = self.mux.PathPrefix(prefix).Subrouter()
  89. server := &Server{middleware: middleware, mux: mux}
  90. self.subservers = append(self.subservers, server)
  91. return server
  92. }
  93. // Name returns route name for the given request, if any.
  94. func (self *Server) Name(r *http.Request) (name string) {
  95. var match mux.RouteMatch
  96. if self.mux.Match(r, &match) {
  97. name = match.Route.GetName()
  98. }
  99. return name
  100. }
  101. // Use add the middleware module into the stack chain.
  102. func (self *Server) Use(module func(http.Handler) http.Handler) {
  103. self.middleware.stack = append(self.middleware.stack, module)
  104. }
  105. // ServeHTTP dispatches the request to the handler whose
  106. // pattern most closely matches the request URL.
  107. func (self *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  108. self.build().ServeHTTP(w, r)
  109. }
  110. // Run starts the application server to serve incoming requests at the given address.
  111. func (self *Server) Run() {
  112. configure()
  113. runtime.GOMAXPROCS(maxprocs)
  114. go func() {
  115. time.Sleep(500 * time.Millisecond)
  116. Infof("Application server is listening at %d", port)
  117. }()
  118. if err := http.ListenAndServe(fmt.Sprintf(":%d", port), self); err != nil {
  119. Fatalf("Failed to start the server: %v", err)
  120. }
  121. }
  122. // Vars returns the route variables for the current request, if any.
  123. func (self *Server) Vars(r *http.Request) map[string]string {
  124. return mux.Vars(r)
  125. }