| @@ -23,15 +23,15 @@ var ( | |||
| once sync.Once | |||
| ) | |||
| type Server struct { | |||
| type server struct { | |||
| middleware *middleware | |||
| mux *mux.Router | |||
| ready bool | |||
| subservers []*Server | |||
| subservers []*server | |||
| } | |||
| func New() *Server { | |||
| self := &Server{ | |||
| func New() *server { | |||
| self := &server{ | |||
| middleware: new(middleware), | |||
| mux: mux.NewRouter().StrictSlash(true), | |||
| } | |||
| @@ -39,7 +39,7 @@ func New() *Server { | |||
| return self | |||
| } | |||
| func (self *Server) configure() { | |||
| func (self *server) configure() { | |||
| once.Do(func() { | |||
| 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") | |||
| @@ -49,7 +49,7 @@ func (self *Server) configure() { | |||
| } | |||
| // 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 { | |||
| // * add server mux into middlware stack to serve as final http.Handler. | |||
| self.Use(func(http.Handler) http.Handler { | |||
| @@ -68,7 +68,7 @@ func (self *Server) build() http.Handler { | |||
| } | |||
| // 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. | |||
| var name = runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() | |||
| @@ -86,23 +86,23 @@ func (self *Server) register(pattern string, handler interface{}, methods ...str | |||
| // Any maps most common HTTP methods request to the given `http.Handler`. | |||
| // 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") | |||
| } | |||
| // 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) | |||
| self.mux.PathPrefix(prefix).Handler(middleware) | |||
| 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) | |||
| return server | |||
| } | |||
| // 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 | |||
| if self.mux.Match(r, &match) { | |||
| name = match.Route.GetName() | |||
| @@ -112,7 +112,7 @@ func (self *Server) Name(r *http.Request) (name string) { | |||
| // FileServer registers a handler to serve HTTP (GET|HEAD) requests | |||
| // 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 { | |||
| fs := http.StripPrefix(prefix, http.FileServer(http.Dir(abs))) | |||
| self.mux.PathPrefix(prefix).Handler(fs) | |||
| @@ -122,67 +122,67 @@ func (self *Server) FileServer(prefix, dir string) { | |||
| } | |||
| // 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) | |||
| } | |||
| // 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. | |||
| func (self *Server) GET(pattern string, handler interface{}) { | |||
| func (self *server) GET(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "GET") | |||
| } | |||
| // 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. | |||
| func (self *Server) HEAD(pattern string, handler interface{}) { | |||
| func (self *server) HEAD(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "HEAD") | |||
| } | |||
| // 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. | |||
| // 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") | |||
| } | |||
| // 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. | |||
| func (self *Server) POST(pattern string, handler interface{}) { | |||
| func (self *server) POST(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "POST") | |||
| } | |||
| // 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. | |||
| func (self *Server) PUT(pattern string, handler interface{}) { | |||
| func (self *server) PUT(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "PUT") | |||
| } | |||
| // 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. | |||
| func (self *Server) DELETE(pattern string, handler interface{}) { | |||
| func (self *server) DELETE(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "DELETE") | |||
| } | |||
| // 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. | |||
| func (self *Server) TRACE(pattern string, handler interface{}) { | |||
| func (self *server) TRACE(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "TRACE") | |||
| } | |||
| // 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. | |||
| func (self *Server) CONNECT(pattern string, handler interface{}) { | |||
| func (self *server) CONNECT(pattern string, handler interface{}) { | |||
| self.register(pattern, handler, "CONNECT") | |||
| } | |||
| // ServeHTTP dispatches the request to the handler whose | |||
| // 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) | |||
| } | |||
| // Run starts the application server to serve incoming requests at the given address. | |||
| func (self *Server) Run() { | |||
| func (self *server) Run() { | |||
| runtime.GOMAXPROCS(maxprocs) | |||
| go func() { | |||
| @@ -196,6 +196,6 @@ func (self *Server) Run() { | |||
| } | |||
| // 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) | |||
| } | |||