func main() { | func main() { | ||||
app := rex.New() | app := rex.New() | ||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "Hello World") | io.WriteString(w, "Hello World") | ||||
}) | }) | ||||
app.Run() | app.Run() | ||||
env.Set("PORT", 9394) | env.Set("PORT", 9394) | ||||
app := rex.New() | app := rex.New() | ||||
app.GET("/", index) | |||||
app.Get("/", index) | |||||
app.Run() | app.Run() | ||||
} | } | ||||
``` | ``` | ||||
```go | ```go | ||||
app := rex.New() | app := rex.New() | ||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "index page") | io.WriteString(w, "index page") | ||||
}) | }) | ||||
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() | ||||
} | } |
func TestNoCache(t *testing.T) { | func TestNoCache(t *testing.T) { | ||||
app := rex.New() | app := rex.New() | ||||
app.Use(NoCache) | app.Use(NoCache) | ||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | io.WriteString(w, "app") | ||||
}) | }) | ||||
app := rex.New() | app := rex.New() | ||||
app.Use(Compress) | app.Use(Compress) | ||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | io.WriteString(w, "app") | ||||
}) | }) | ||||
app := rex.New() | app := rex.New() | ||||
app.Use(Header(values)) | app.Use(Header(values)) | ||||
app.GET("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/", func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | io.WriteString(w, "app") | ||||
}) | }) | ||||
app := rex.New() | app := rex.New() | ||||
app.Use(Static(tempdir)) | app.Use(Static(tempdir)) | ||||
prefix := path.Join("/", path.Base(path.Dir(filename))) | prefix := path.Join("/", path.Base(path.Dir(filename))) | ||||
app.GET(prefix, func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get(prefix, func(w http.ResponseWriter, r *http.Request) { | |||||
io.WriteString(w, "app") | io.WriteString(w, "app") | ||||
}) | }) | ||||
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") | ||||
} | } | ||||
func TestName(t *testing.T) { | func TestName(t *testing.T) { | ||||
Convey("rex.Name", t, func() { | Convey("rex.Name", t, func() { | ||||
app := New() | app := New() | ||||
app.GET("/login", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Get("/login", func(w http.ResponseWriter, r *http.Request) { | |||||
w.WriteHeader(http.StatusOK) | w.WriteHeader(http.StatusOK) | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
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") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
func TestHEAD(t *testing.T) { | |||||
func TestHead(t *testing.T) { | |||||
app := New() | app := New() | ||||
app.HEAD("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Head("/", func(w http.ResponseWriter, r *http.Request) { | |||||
w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
func TestOPTIONS(t *testing.T) { | |||||
func TestOptions(t *testing.T) { | |||||
app := New() | app := New() | ||||
app.OPTIONS("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Options("/", func(w http.ResponseWriter, r *http.Request) { | |||||
w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
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") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
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") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
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") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
func TestCONNECT(t *testing.T) { | |||||
func TestConnect(t *testing.T) { | |||||
app := New() | app := New() | ||||
app.CONNECT("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Connect("/", func(w http.ResponseWriter, r *http.Request) { | |||||
w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
}) | }) | ||||
}) | }) | ||||
} | } | ||||
func TestTRACE(t *testing.T) { | |||||
func TestTrace(t *testing.T) { | |||||
app := New() | app := New() | ||||
app.TRACE("/", func(w http.ResponseWriter, r *http.Request) { | |||||
app.Trace("/", func(w http.ResponseWriter, r *http.Request) { | |||||
w.Header().Set("X-Powered-By", "rex") | w.Header().Set("X-Powered-By", "rex") | ||||
}) | }) | ||||
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") | ||||
}) | }) |