Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

194 Zeilen
6.4KB

  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package rex
  24. import (
  25. "fmt"
  26. "net/http"
  27. "reflect"
  28. "runtime"
  29. "time"
  30. "github.com/gorilla/mux"
  31. )
  32. type middleware struct {
  33. cache http.Handler
  34. stack []func(http.Handler) http.Handler
  35. }
  36. // build sets up the whole middleware modules in a FIFO chain.
  37. func (self *middleware) build() http.Handler {
  38. if self.cache == nil {
  39. var next http.Handler = http.DefaultServeMux
  40. // Activate modules in FIFO order.
  41. for index := len(self.stack) - 1; index >= 0; index-- {
  42. next = self.stack[index](next)
  43. }
  44. self.cache = next
  45. }
  46. return self.cache
  47. }
  48. // Use add the middleware module into the stack chain.
  49. func (self *middleware) Use(modules ...func(http.Handler) http.Handler) {
  50. self.stack = append(self.stack, modules...)
  51. }
  52. // Implements the net/http Handler interface and calls the middleware stack.
  53. func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  54. self.build().ServeHTTP(w, r)
  55. }
  56. type Router struct {
  57. middleware *middleware
  58. mux *mux.Router
  59. ready bool
  60. subrouters []*Router
  61. }
  62. func New() *Router {
  63. return &Router{
  64. middleware: new(middleware),
  65. mux: mux.NewRouter().StrictSlash(true),
  66. }
  67. }
  68. // build constructs all router/subrouters along with their middleware modules chain.
  69. func (self *Router) build() http.Handler {
  70. if !self.ready {
  71. self.ready = true
  72. // * activate router's middleware modules.
  73. self.middleware.Use(func(http.Handler) http.Handler {
  74. return self.mux
  75. })
  76. // * activate subrouters's middleware modules.
  77. for index := 0; index < len(self.subrouters); index++ {
  78. sr := self.subrouters[index]
  79. sr.middleware.Use(func(http.Handler) http.Handler {
  80. return sr.mux
  81. })
  82. }
  83. }
  84. return self.middleware
  85. }
  86. // register adds the http.Handler/http.HandleFunc into Gorilla mux.
  87. func (self *Router) register(method string, pattern string, handler interface{}) {
  88. // finds the full function name (with package) as its mappings.
  89. var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()
  90. switch H := handler.(type) {
  91. case http.Handler:
  92. self.mux.Handle(pattern, H).Methods(method).Name(name)
  93. case func(http.ResponseWriter, *http.Request):
  94. self.mux.HandleFunc(pattern, H).Methods(method).Name(name)
  95. default:
  96. Fatalf("Unsupported handler (%s) passed in.", name)
  97. }
  98. }
  99. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  100. // it also fetch the full function name of the handler (with package) to name the route.
  101. func (self *Router) Get(pattern string, handler interface{}) {
  102. self.register("GET", pattern, handler)
  103. }
  104. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  105. // it also fetch the full function name of the handler (with package) to name the route.
  106. func (self *Router) Head(pattern string, handler interface{}) {
  107. self.register("HEAD", pattern, handler)
  108. }
  109. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  110. // it also fetch the full function name of the handler (with package) to name the route.
  111. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  112. func (self *Router) Options(pattern string, handler interface{}) {
  113. self.register("OPTIONS", pattern, handler)
  114. }
  115. // Post is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  116. // it also fetch the full function name of the handler (with package) to name the route.
  117. func (self *Router) Post(pattern string, handler interface{}) {
  118. self.register("POST", pattern, handler)
  119. }
  120. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  121. // it also fetch the full function name of the handler (with package) to name the route.
  122. func (self *Router) Put(pattern string, handler interface{}) {
  123. self.register("PUT", pattern, handler)
  124. }
  125. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  126. // it also fetch the full function name of the handler (with package) to name the route.
  127. func (self *Router) Delete(pattern string, handler interface{}) {
  128. self.register("Delete", pattern, handler)
  129. }
  130. // Group creates a new application group under the given path prefix.
  131. func (self *Router) Group(prefix string) *Router {
  132. var middleware = new(middleware)
  133. self.mux.PathPrefix(prefix).Handler(middleware)
  134. var mux = self.mux.PathPrefix(prefix).Subrouter()
  135. router := &Router{middleware: middleware, mux: mux}
  136. self.subrouters = append(self.subrouters, router)
  137. return router
  138. }
  139. // Name returns route name for the given request, if any.
  140. func (self *Router) Name(r *http.Request) (name string) {
  141. var match mux.RouteMatch
  142. if self.mux.Match(r, &match) {
  143. name = match.Route.GetName()
  144. }
  145. return name
  146. }
  147. // Use add the middleware module into the stack chain.
  148. func (self *Router) Use(module func(http.Handler) http.Handler) {
  149. self.middleware.Use(module)
  150. }
  151. // ServeHTTP dispatches the request to the handler whose
  152. // pattern most closely matches the request URL.
  153. func (self *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  154. self.build().ServeHTTP(w, r)
  155. }
  156. // Run starts the application server to serve incoming requests at the given address.
  157. func (self *Router) Run() {
  158. runtime.GOMAXPROCS(config.maxprocs)
  159. go func() {
  160. time.Sleep(500 * time.Millisecond)
  161. Infof("Application server is listening at %d", config.port)
  162. }()
  163. if err := http.ListenAndServe(fmt.Sprintf(":%d", config.port), self); err != nil {
  164. Fatalf("Failed to start the server: %v", err)
  165. }
  166. }