| func main() { | func main() { | ||||
| app := rex.New() | app := rex.New() | ||||
| app.Use(livereload.Middleware) | app.Use(livereload.Middleware) | ||||
| app.Get("/", Index) | |||||
| app.GET("/", Index) | |||||
| api := app.Group("/v1/") | api := app.Group("/v1/") | ||||
| api.Use(JSON) | api.Use(JSON) | ||||
| api.Get("/", fetch) | |||||
| api.Post("/", create) | |||||
| api.Put("/", update) | |||||
| api.Delete("/", remove) | |||||
| api.GET("/", fetch) | |||||
| api.POST("/", create) | |||||
| api.PUT("/", update) | |||||
| api.DELETE("/", remove) | |||||
| app.Run() | app.Run() | ||||
| } | } |
| self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD") | self.register(pattern, handler, "GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD") | ||||
| } | } | ||||
| // 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| self.register(pattern, handler, "DELETE") | |||||
| } | |||||
| // 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.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"), | |||||
| // it also fetch the full function name of the handler (with package) to name the route. | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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{}) { | |||||
| 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) { |
| }) | }) | ||||
| } | } | ||||
| func TestGet(t *testing.T) { | |||||
| func TestGET(t *testing.T) { | |||||
| app := New() | app := New() | ||||
| app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
| w.Header().Set("Content-Type", "application/json") | w.Header().Set("Content-Type", "application/json") | ||||
| }) | }) | ||||
| Convey("rex.Get", t, func() { | |||||
| Convey("rex.GET", t, func() { | |||||
| request, _ := http.NewRequest("GET", "/", nil) | request, _ := http.NewRequest("GET", "/", nil) | ||||
| response := httptest.NewRecorder() | response := httptest.NewRecorder() | ||||
| }) | }) | ||||
| } | } | ||||
| func TestPost(t *testing.T) { | |||||
| func TestPOST(t *testing.T) { | |||||
| app := New() | app := New() | ||||
| app.Post("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.POST("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
| w.Header().Set("Content-Type", "application/json") | w.Header().Set("Content-Type", "application/json") | ||||
| }) | }) | ||||
| Convey("rex.Post", t, func() { | |||||
| Convey("rex.POST", t, func() { | |||||
| request, _ := http.NewRequest("POST", "/", nil) | request, _ := http.NewRequest("POST", "/", nil) | ||||
| response := httptest.NewRecorder() | response := httptest.NewRecorder() | ||||
| }) | }) | ||||
| } | } | ||||
| func TestPut(t *testing.T) { | |||||
| func TestPUT(t *testing.T) { | |||||
| app := New() | app := New() | ||||
| app.Put("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.PUT("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
| w.Header().Set("Content-Type", "application/json") | w.Header().Set("Content-Type", "application/json") | ||||
| }) | }) | ||||
| Convey("rex.Put", t, func() { | |||||
| Convey("rex.PUT", t, func() { | |||||
| request, _ := http.NewRequest("PUT", "/", nil) | request, _ := http.NewRequest("PUT", "/", nil) | ||||
| response := httptest.NewRecorder() | response := httptest.NewRecorder() | ||||
| }) | }) | ||||
| } | } | ||||
| func TestDelete(t *testing.T) { | |||||
| func TestDELETE(t *testing.T) { | |||||
| app := New() | app := New() | ||||
| app.Delete("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.DELETE("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
| w.Header().Set("Content-Type", "application/json") | w.Header().Set("Content-Type", "application/json") | ||||
| }) | }) | ||||
| Convey("rex.Delete", t, func() { | |||||
| Convey("rex.DELETE", t, func() { | |||||
| request, _ := http.NewRequest("DELETE", "/", nil) | request, _ := http.NewRequest("DELETE", "/", nil) | ||||
| response := httptest.NewRecorder() | response := httptest.NewRecorder() | ||||
| func TestGroup(t *testing.T) { | func TestGroup(t *testing.T) { | ||||
| app := New() | app := New() | ||||
| app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| io.WriteString(w, "index") | io.WriteString(w, "index") | ||||
| }) | }) | ||||
| user := app.Group("/users") | user := app.Group("/users") | ||||
| user.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| user.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
| }) | }) | ||||
| func TestUse(t *testing.T) { | func TestUse(t *testing.T) { | ||||
| app := New() | app := New() | ||||
| app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
| io.WriteString(w, "index") | io.WriteString(w, "index") | ||||
| }) | }) | ||||
| app.Use(func(next http.Handler) http.Handler { | app.Use(func(next http.Handler) http.Handler { | ||||
| func TestVars(t *testing.T) { | func TestVars(t *testing.T) { | ||||
| Convey("rex.Vars", t, func() { | Convey("rex.Vars", t, func() { | ||||
| app := New() | app := New() | ||||
| app.Get("/users/{id}", func(w http.ResponseWriter, r *http.Request) { | |||||
| app.GET("/users/{id}", func(w http.ResponseWriter, r *http.Request) { | |||||
| vars := app.Vars(r) | vars := app.Vars(r) | ||||
| So(vars["id"], ShouldEqual, "123") | So(vars["id"], ShouldEqual, "123") | ||||
| }) | }) |