| once sync.Once | once sync.Once | ||||
| ) | ) | ||||
| type Server struct { | |||||
| type server struct { | |||||
| middleware *middleware | middleware *middleware | ||||
| mux *mux.Router | mux *mux.Router | ||||
| ready bool | ready bool | ||||
| subservers []*Server | |||||
| subservers []*server | |||||
| } | } | ||||
| func New() *Server { | |||||
| self := &Server{ | |||||
| func New() *server { | |||||
| self := &server{ | |||||
| middleware: new(middleware), | middleware: new(middleware), | ||||
| mux: mux.NewRouter().StrictSlash(true), | mux: mux.NewRouter().StrictSlash(true), | ||||
| } | } | ||||
| return self | return self | ||||
| } | } | ||||
| func (self *Server) configure() { | |||||
| func (self *server) configure() { | |||||
| once.Do(func() { | once.Do(func() { | ||||
| flag.BoolVar(&debug, "debug", env.Bool("DEBUG", true), "flag to toggle debug mode") | flag.BoolVar(&debug, "debug", env.Bool("DEBUG", true), "flag to toggle debug mode") | ||||
| flag.IntVar(&port, "port", env.Int("PORT", 5000), "port to run the application server") | flag.IntVar(&port, "port", env.Int("PORT", 5000), "port to run the application server") | ||||
| } | } | ||||
| // build constructs all server/subservers along with their middleware modules chain. | // build constructs all server/subservers along with their middleware modules chain. | ||||
| func (self *Server) build() http.Handler { | |||||
| func (self *server) build() http.Handler { | |||||
| if !self.ready { | if !self.ready { | ||||
| // * add server mux into middlware stack to serve as final http.Handler. | // * add server mux into middlware stack to serve as final http.Handler. | ||||
| self.Use(func(http.Handler) http.Handler { | self.Use(func(http.Handler) http.Handler { | ||||
| } | } | ||||
| // register adds the http.Handler/http.HandleFunc into Gorilla mux. | // register adds the http.Handler/http.HandleFunc into Gorilla mux. | ||||
| func (self *Server) register(pattern string, handler interface{}, methods ...string) { | |||||
| func (self *server) register(pattern string, handler interface{}, methods ...string) { | |||||
| // finds the full function name (with package) as its mappings. | // finds the full function name (with package) as its mappings. | ||||
| var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() | var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() | ||||
| // Any maps most common HTTP methods request to the given `http.Handler`. | // Any maps most common HTTP methods request to the given `http.Handler`. | ||||
| // Supports: GET | POST | PUT | DELETE | OPTIONS | HEAD | // Supports: GET | POST | PUT | DELETE | OPTIONS | HEAD | ||||
| func (self *Server) Any(pattern string, handler interface{}) { | |||||
| func (self *server) Any(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD") | self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD") | ||||
| } | } | ||||
| // Group creates a new application group under the given path prefix. | // Group creates a new application group under the given path prefix. | ||||
| func (self *Server) Group(prefix string) *Server { | |||||
| func (self *server) Group(prefix string) *server { | |||||
| var middleware = new(middleware) | var middleware = new(middleware) | ||||
| self.mux.PathPrefix(prefix).Handler(middleware) | self.mux.PathPrefix(prefix).Handler(middleware) | ||||
| var mux = self.mux.PathPrefix(prefix).Subrouter() | var mux = self.mux.PathPrefix(prefix).Subrouter() | ||||
| server := &Server{middleware: middleware, mux: mux} | |||||
| server := &server{middleware: middleware, mux: mux} | |||||
| self.subservers = append(self.subservers, server) | self.subservers = append(self.subservers, server) | ||||
| return server | return server | ||||
| } | } | ||||
| // Name returns route name for the given request, if any. | // Name returns route name for the given request, if any. | ||||
| func (self *Server) Name(r *http.Request) (name string) { | |||||
| func (self *server) Name(r *http.Request) (name string) { | |||||
| var match mux.RouteMatch | var match mux.RouteMatch | ||||
| if self.mux.Match(r, &match) { | if self.mux.Match(r, &match) { | ||||
| name = match.Route.GetName() | name = match.Route.GetName() | ||||
| // FileServer registers a handler to serve HTTP (GET|HEAD) requests | // FileServer registers a handler to serve HTTP (GET|HEAD) requests | ||||
| // with the contents of file system under the given directory. | // with the contents of file system under the given directory. | ||||
| func (self *Server) FileServer(prefix, dir string) { | |||||
| func (self *server) FileServer(prefix, dir string) { | |||||
| if abs, err := filepath.Abs(dir); err == nil { | if abs, err := filepath.Abs(dir); err == nil { | ||||
| fs := http.StripPrefix(prefix, http.FileServer(http.Dir(abs))) | fs := http.StripPrefix(prefix, http.FileServer(http.Dir(abs))) | ||||
| self.mux.PathPrefix(prefix).Handler(fs) | self.mux.PathPrefix(prefix).Handler(fs) | ||||
| } | } | ||||
| // Use add the middleware module into the stack chain. | // Use add the middleware module into the stack chain. | ||||
| func (self *Server) Use(module func(http.Handler) http.Handler) { | |||||
| func (self *server) Use(module func(http.Handler) http.Handler) { | |||||
| self.middleware.stack = append(self.middleware.stack, module) | self.middleware.stack = append(self.middleware.stack, module) | ||||
| } | } | ||||
| // GET is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | // GET is a shortcut for mux.HandleFunc(pattern, handler).Methods("GET"), | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) GET(pattern string, handler interface{}) { | |||||
| func (self *server) GET(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "GET") | self.register(pattern, handler, "GET") | ||||
| } | } | ||||
| // HEAD is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD") | // HEAD is a shortcut for mux.HandleFunc(pattern, handler).Methods("HEAD") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) HEAD(pattern string, handler interface{}) { | |||||
| func (self *server) HEAD(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "HEAD") | self.register(pattern, handler, "HEAD") | ||||
| } | } | ||||
| // OPTIONS is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS") | // OPTIONS is a shortcut for mux.HandleFunc(pattern, handler).Methods("OPTIONS") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do. | // NOTE method OPTIONS is **NOT** cachable, beware of what you are going to do. | ||||
| func (self *Server) OPTIONS(pattern string, handler interface{}) { | |||||
| func (self *server) OPTIONS(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "OPTIONS") | self.register(pattern, handler, "OPTIONS") | ||||
| } | } | ||||
| // POST is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST") | // POST is a shortcut for mux.HandleFunc(pattern, handler).Methods("POST") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) POST(pattern string, handler interface{}) { | |||||
| func (self *server) POST(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "POST") | self.register(pattern, handler, "POST") | ||||
| } | } | ||||
| // PUT is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT") | // PUT is a shortcut for mux.HandleFunc(pattern, handler).Methods("PUT") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) PUT(pattern string, handler interface{}) { | |||||
| func (self *server) PUT(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "PUT") | self.register(pattern, handler, "PUT") | ||||
| } | } | ||||
| // DELETE is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE") | // DELETE is a shortcut for mux.HandleFunc(pattern, handler).Methods("DELETE") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) DELETE(pattern string, handler interface{}) { | |||||
| func (self *server) DELETE(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "DELETE") | self.register(pattern, handler, "DELETE") | ||||
| } | } | ||||
| // TRACE is a shortcut for mux.HandleFunc(pattern, handler).Methods("TRACE") | // TRACE is a shortcut for mux.HandleFunc(pattern, handler).Methods("TRACE") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) TRACE(pattern string, handler interface{}) { | |||||
| func (self *server) TRACE(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "TRACE") | self.register(pattern, handler, "TRACE") | ||||
| } | } | ||||
| // CONNECT is a shortcut for mux.HandleFunc(pattern, handler).Methods("CONNECT") | // CONNECT is a shortcut for mux.HandleFunc(pattern, handler).Methods("CONNECT") | ||||
| // it also fetch the full function name of the handler (with package) to name the route. | // it also fetch the full function name of the handler (with package) to name the route. | ||||
| func (self *Server) CONNECT(pattern string, handler interface{}) { | |||||
| func (self *server) CONNECT(pattern string, handler interface{}) { | |||||
| self.register(pattern, handler, "CONNECT") | self.register(pattern, handler, "CONNECT") | ||||
| } | } | ||||
| // ServeHTTP dispatches the request to the handler whose | // ServeHTTP dispatches the request to the handler whose | ||||
| // pattern most closely matches the request URL. | // pattern most closely matches the request URL. | ||||
| func (self *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||||
| func (self *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||||
| self.build().ServeHTTP(w, r) | self.build().ServeHTTP(w, r) | ||||
| } | } | ||||
| // Run starts the application server to serve incoming requests at the given address. | // Run starts the application server to serve incoming requests at the given address. | ||||
| func (self *Server) Run() { | |||||
| func (self *server) Run() { | |||||
| runtime.GOMAXPROCS(maxprocs) | runtime.GOMAXPROCS(maxprocs) | ||||
| go func() { | go func() { | ||||
| } | } | ||||
| // Vars returns the route variables for the current request, if any. | // Vars returns the route variables for the current request, if any. | ||||
| func (self *Server) Vars(r *http.Request) map[string]string { | |||||
| func (self *server) Vars(r *http.Request) map[string]string { | |||||
| return mux.Vars(r) | return mux.Vars(r) | ||||
| } | } |