func Add(mux *web.Mux) { /* Endpoint to handler config */ mux.Get("/", home) mux.Get("/home", about) mux.Use(mux.Router) }
func rooter(m *web.Mux) http.Handler { m.Use(SuperSecure) m.Get("/index", UserRoot) m.Get("/user/index", UserIndex) m.Get("/user/new", UserNew) m.Post("/user/new", UserCreate) m.Get("/user/edit/:id", UserEdit) m.Post("/user/update/:id", UserUpdate) m.Get("/user/delete/:id", UserDelete) return m }
func route(m *web.Mux) { // Add routes to the global handler setGetHandler(m, "/", Root) // Use Sinatra-style patterns in your URLs setGetHandler(m, "/novel/:ncode", responseCache(getNovelInfo)) setGetHandler(m, "/novel_content/:ncode/:sublist_id", responseCache(getNovelContent)) // Middleware can be used to inject behavior into your app. The // middleware for this application are defined in middleware.go, but you // can put them wherever you like. m.Use(Json) }
func rooter(m *web.Mux) http.Handler { m.Use(SuperSecure) user := web.New() goji.Handle("/user/*", user) user.Use(middleware.SubRouter) user.Get("/index", UserIndex) user.Get("/new", UserNew) user.Post("/new", UserCreate) user.Get("/edit/:id", UserEdit) user.Post("/update/:id", UserUpdate) user.Get("/delete/:id", UserDelete) return m }
func (rm *RouterMold) Generate() *web.Mux { var mux *web.Mux if rm.SubRoutes == "" { mux = goji.DefaultMux mux.Abandon(middleware.Logger) } else { mux := web.New() mux.Use(middleware.RequestID) mux.Use(middleware.Recoverer) mux.Use(middleware.AutomaticOptions) goji.Handle(rm.SubRoutes, mux) } for _, m := range rm.Middlewares { mux.Use(m.MiddlewareFunc()) } var handlerFunc func(Route) interface{} if rm.HandlerFunc == nil { handlerFunc = func(r Route) interface{} { return r.Handler } } else { handlerFunc = rm.HandlerFunc } for _, r := range rm.Routes { var pattern interface{} if r.RegExp != "" { pattern = regexp.MustCompile(r.RegExp) } else { pattern = r.Path } switch r.Method { case "HEAD": mux.Head(pattern, handlerFunc(r)) case "GET": mux.Get(pattern, handlerFunc(r)) case "POST": mux.Post(pattern, handlerFunc(r)) case "PUT": mux.Put(pattern, handlerFunc(r)) case "PATCH": mux.Patch(pattern, handlerFunc(r)) case "DELETE": mux.Delete(pattern, handlerFunc(r)) } } return mux }
// This demo file shows two ways to test the handlers, it is not suggested to use both in a real // project, the two methods are provided here as a sample. // Tests that simply create a mux and exercise that by calling the Mux's ServeHTTP function // This is great for isolated tests, it becomes difficult when the middleware higher in the stack // is needed or other concurrent goroutines and other handlers al also needed for higher-level tests var _ = Describe("Mux-based settings tests", func() { var mx *web.Mux // mux with the handlers we're testing BeforeEach(func() { settings = make(map[string]string) mx = NewMux() gojiutil.AddCommon15(mx, log15.Root()) mx.Use(gojiutil.ParamsLogger(true)) // useful for troubleshooting }) It("gets what it sets", func() { // set a value req, _ := http.NewRequest("PUT", "http://example.com/settings/hello?value=world", bytes.NewReader([]byte{})) resp := httptest.NewRecorder() mx.ServeHTTP(resp, req) Ω(resp.Code).Should(Equal(200)) Ω(settings["hello"]).Should(Equal("world")) // get the value back req, _ = http.NewRequest("GET", "http://example.com/settings/hello", nil) resp = httptest.NewRecorder() mx.ServeHTTP(resp, req)