Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

221 linhas
7.1KB

  1. package rex
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "net/http"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "sync"
  12. "time"
  13. log "github.com/Sirupsen/logrus"
  14. "github.com/goanywhere/env"
  15. "github.com/gorilla/mux"
  16. )
  17. var (
  18. debug bool
  19. port int
  20. maxprocs int
  21. once sync.Once
  22. )
  23. type server struct {
  24. middleware *middleware
  25. mux *mux.Router
  26. ready bool
  27. subservers []*server
  28. }
  29. func New() *server {
  30. self := &server{
  31. middleware: new(middleware),
  32. mux: mux.NewRouter().StrictSlash(true),
  33. }
  34. self.configure()
  35. return self
  36. }
  37. func (self *server) configure() {
  38. once.Do(func() {
  39. flag.BoolVar(&debug, "debug", env.Bool("DEBUG", true), "flag to toggle debug mode")
  40. flag.IntVar(&port, "port", env.Int("PORT", 5000), "port to run the application server")
  41. flag.IntVar(&maxprocs, "maxprocs", env.Int("MAXPROCS", runtime.NumCPU()), "maximum cpu processes to run the server")
  42. flag.Parse()
  43. })
  44. }
  45. // build constructs all server/subservers along with their middleware modules chain.
  46. func (self *server) build() http.Handler {
  47. if !self.ready {
  48. // * add server mux into middlware stack to serve as final http.Handler.
  49. self.Use(func(http.Handler) http.Handler {
  50. return self.mux
  51. })
  52. // * add subservers into middlware stack to serve as final http.Handler.
  53. for index := 0; index < len(self.subservers); index++ {
  54. server := self.subservers[index]
  55. server.Use(func(http.Handler) http.Handler {
  56. return server.mux
  57. })
  58. }
  59. self.ready = true
  60. }
  61. return self.middleware
  62. }
  63. // register adds the http.Handler/http.HandleFunc into Gorilla mux.
  64. func (self *server) register(pattern string, handler interface{}, methods ...string) {
  65. var name = strings.Join(methods, "|") + ":" + pattern
  66. // finds the full function name (with package) as its mappings.
  67. //var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()
  68. switch H := handler.(type) {
  69. case http.Handler:
  70. self.mux.Handle(pattern, H).Methods(methods...).Name(name)
  71. case func(http.ResponseWriter, *http.Request):
  72. self.mux.HandleFunc(pattern, H).Methods(methods...).Name(name)
  73. default:
  74. panic("Unsupported handler: " + name)
  75. }
  76. }
  77. // Any maps most common HTTP methods request to the given `http.Handler`.
  78. // Supports: GET | POST | PUT | DELETE | OPTIONS | HEAD
  79. func (self *server) Any(pattern string, handler interface{}) {
  80. self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD")
  81. }
  82. // Group creates a new application group under the given path prefix.
  83. func (self *server) Group(prefix string) *server {
  84. var middleware = new(middleware)
  85. self.mux.PathPrefix(prefix).Handler(middleware)
  86. var mux = self.mux.PathPrefix(prefix).Subrouter()
  87. server := &server{middleware: middleware, mux: mux}
  88. self.subservers = append(self.subservers, server)
  89. return server
  90. }
  91. // Name returns route name for the given request, if any.
  92. func (self *server) Name(r *http.Request) (name string) {
  93. var match mux.RouteMatch
  94. if self.mux.Match(r, &match) {
  95. name = match.Route.GetName()
  96. }
  97. return name
  98. }
  99. // FileServer registers a handler to serve HTTP (GET|HEAD) requests
  100. // with the contents of file system under the given directory.
  101. func (self *server) FileServer(prefix, dir string) {
  102. if abs, err := filepath.Abs(dir); err == nil {
  103. fs := http.StripPrefix(prefix, http.FileServer(http.Dir(abs)))
  104. self.mux.PathPrefix(prefix).Handler(fs)
  105. } else {
  106. panic("Failed to setup file server: " + err.Error())
  107. }
  108. }
  109. // Use add the middleware module into the stack chain.
  110. func (self *server) Use(modules ...func(http.Handler) http.Handler) {
  111. self.middleware.stack = append(self.middleware.stack, modules...)
  112. }
  113. // Get is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"),
  114. // it also fetch the full function name of the handler (with package) to name the route.
  115. func (self *server) Get(pattern string, handler interface{}) {
  116. self.register(pattern, handler, "GET")
  117. }
  118. // Head is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD")
  119. // it also fetch the full function name of the handler (with package) to name the route.
  120. func (self *server) Head(pattern string, handler interface{}) {
  121. self.register(pattern, handler, "HEAD")
  122. }
  123. // Options is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS")
  124. // it also fetch the full function name of the handler (with package) to name the route.
  125. // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do.
  126. func (self *server) Options(pattern string, handler interface{}) {
  127. self.register(pattern, handler, "OPTIONS")
  128. }
  129. // POST is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST")
  130. // it also fetch the full function name of the handler (with package) to name the route.
  131. func (self *server) Post(pattern string, handler interface{}) {
  132. self.register(pattern, handler, "POST")
  133. }
  134. // Put is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT")
  135. // it also fetch the full function name of the handler (with package) to name the route.
  136. func (self *server) Put(pattern string, handler interface{}) {
  137. self.register(pattern, handler, "PUT")
  138. }
  139. // Delete is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE")
  140. // it also fetch the full function name of the handler (with package) to name the route.
  141. func (self *server) Delete(pattern string, handler interface{}) {
  142. self.register(pattern, handler, "DELETE")
  143. }
  144. // Trace is a shortcut for mux.HandleFunc(pattern, handler).Methods("TRACE")
  145. // it also fetch the full function name of the handler (with package) to name the route.
  146. func (self *server) Trace(pattern string, handler interface{}) {
  147. self.register(pattern, handler, "TRACE")
  148. }
  149. // Connect is a shortcut for mux.HandleFunc(pattern, handler).Methods("CONNECT")
  150. // it also fetch the full function name of the handler (with package) to name the route.
  151. func (self *server) Connect(pattern string, handler interface{}) {
  152. self.register(pattern, handler, "CONNECT")
  153. }
  154. // ServeHTTP dispatches the request to the handler whose
  155. // pattern most closely matches the request URL.
  156. func (self *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  157. self.build().ServeHTTP(w, r)
  158. }
  159. // Sends the HTTP response in JSON.
  160. func (self *server) Send(w http.ResponseWriter, v interface{}) {
  161. var buffer = new(bytes.Buffer)
  162. w.Header().Set("Content-type", "application/json; charset=utf-8")
  163. if err := json.NewEncoder(buffer).Encode(v); err == nil {
  164. w.Write(buffer.Bytes())
  165. } else {
  166. http.Error(w, err.Error(), http.StatusInternalServerError)
  167. }
  168. }
  169. // Renders a view (Pongo) and sends the rendered HTML string to the client.
  170. // Optional parameters: value, local variables for the view.
  171. // func (self *server) Render(filename string, v ...interface{}) {}
  172. // Run starts the application server to serve incoming requests at the given address.
  173. func (self *server) Run() {
  174. runtime.GOMAXPROCS(maxprocs)
  175. go func() {
  176. time.Sleep(500 * time.Millisecond)
  177. log.Infof("Application server is listening at %d", port)
  178. }()
  179. if err := http.ListenAndServe(fmt.Sprintf(":%d", port), self); err != nil {
  180. log.Fatalf("Failed to start the server: %v", err)
  181. }
  182. }
  183. // Vars returns the route variables for the current request, if any.
  184. func (self *server) Vars(r *http.Request) map[string]string {
  185. return mux.Vars(r)
  186. }