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

170 行
5.4KB

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