選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

server.go 6.5KB

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