// InitRouter sets up the router (and subrouters). // It also includes the closure middleware where we load the global Settings reference into each request. func InitRouter(settings *helpers.Settings) *web.Router { router := web.New(Context{}) // A closure that effectively loads the Settings into every request. router.Middleware(func(c *Context, resp web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) { c.Settings = settings next(resp, req) }) // Backend Route Initialization // Initialize the Gocraft Router with the basic context and routes router.Get("/ping", (*Context).Ping) router.Get("/handshake", (*Context).LoginHandshake) router.Get("/oauth2callback", (*Context).OAuthCallback) // Setup the /api subrouter. apiRouter := router.Subrouter(APIContext{}, "/v2") apiRouter.Middleware((*APIContext).OAuth) // All routes accepted apiRouter.Get("/authstatus", (*APIContext).AuthStatus) apiRouter.Get("/logout", (*APIContext).Logout) apiRouter.Get("/profile", (*APIContext).UserProfile) apiRouter.Get("/:*", (*APIContext).Proxy) // Frontend Route Initialization // Set up static file serving to load from the static folder. router.Middleware(web.StaticMiddleware("static", web.StaticOption{IndexFile: "index.html"})) return router }
// Helper function to handle all the weird work of creating a test server. func startServer() (*httptest.Server, AcceptanceTestEnvVars) { // Load the environment variables to conduct the tests. testEnvVars := AcceptanceTestEnvVars{} testEnvVars.LoadTestEnvVars() var TimeoutConstant = time.Second * 10 SetDefaultEventuallyTimeout(TimeoutConstant) var err error // Attempt to initial routers app, settings, err := controllers.InitApp(testEnvVars.EnvVars) if err != nil { fmt.Println(err.Error()) os.Exit(1) } // Since we are running in a separate folder from the main package, we need to change the location of the static folder. app.Middleware(web.StaticMiddleware("../static", web.StaticOption{IndexFile: "index.html"})) // Create the httptest server server := httptest.NewUnstartedServer(app) server.Start() // Change config values to fit the URL of the httptest server that is created on a random port. testEnvVars.Hostname = server.URL settings.OAuthConfig.RedirectURL = server.URL + "/oauth2callback" return server, testEnvVars }
// InitRouter sets up the router (and subrouters). // It also includes the closure middleware where we load the global Settings reference into each request. func InitRouter(settings *helpers.Settings) *web.Router { if settings == nil { return nil } router := web.New(Context{}) // A closure that effectively loads the Settings into every request. router.Middleware(func(c *Context, resp web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) { c.Settings = settings next(resp, req) }) // Backend Route Initialization // Initialize the Gocraft Router with the basic context and routes router.Get("/ping", (*Context).Ping) router.Get("/handshake", (*Context).LoginHandshake) router.Get("/oauth2callback", (*Context).OAuthCallback) // Secure all the other routes secureRouter := router.Subrouter(SecureContext{}, "/") // Setup the /api subrouter. apiRouter := secureRouter.Subrouter(APIContext{}, "/v2") apiRouter.Middleware((*APIContext).OAuth) // All routes accepted apiRouter.Get("/authstatus", (*APIContext).AuthStatus) apiRouter.Get("/logout", (*APIContext).Logout) apiRouter.Get("/profile", (*APIContext).UserProfile) apiRouter.Get("/:*", (*APIContext).APIProxy) apiRouter.Put("/:*", (*APIContext).APIProxy) apiRouter.Post("/:*", (*APIContext).APIProxy) apiRouter.Delete("/:*", (*APIContext).APIProxy) // Setup the /uaa subrouter. uaaRouter := secureRouter.Subrouter(UAAContext{}, "/uaa") uaaRouter.Middleware((*UAAContext).OAuth) uaaRouter.Get("/userinfo", (*UAAContext).UserInfo) uaaRouter.Post("/Users", (*UAAContext).QueryUser) if settings.PProfEnabled { // Setup the /pprof subrouter. pprofRouter := secureRouter.Subrouter(PProfContext{}, "/debug/pprof") pprofRouter.Get("/", (*PProfContext).Index) pprofRouter.Get("/heap", (*PProfContext).Heap) pprofRouter.Get("/goroutine", (*PProfContext).Goroutine) pprofRouter.Get("/threadcreate", (*PProfContext).Threadcreate) pprofRouter.Get("/block", (*PProfContext).Threadcreate) pprofRouter.Get("/profile", (*PProfContext).Profile) pprofRouter.Get("/symbol", (*PProfContext).Symbol) } // Frontend Route Initialization // Set up static file serving to load from the static folder. router.Middleware(web.StaticMiddleware("static", web.StaticOption{IndexFile: "index.html"})) return router }
// InitRouter sets up the router (and subrouters). // It also includes the closure middleware where we load the global Settings reference into each request. func InitRouter(settings *helpers.Settings, templates *template.Template) *web.Router { if settings == nil { return nil } router := web.New(Context{}) // A closure that effectively loads the Settings into every request. router.Middleware(func(c *Context, resp web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) { c.Settings = settings c.templates = templates next(resp, req) }) router.Get("/", (*Context).Index) // Backend Route Initialization // Initialize the Gocraft Router with the basic context and routes router.Get("/ping", (*Context).Ping) router.Get("/handshake", (*Context).LoginHandshake) router.Get("/oauth2callback", (*Context).OAuthCallback) // Secure all the other routes secureRouter := router.Subrouter(SecureContext{}, "/") // Setup the /api subrouter. apiRouter := secureRouter.Subrouter(APIContext{}, "/v2") apiRouter.Middleware((*APIContext).OAuth) // All routes accepted apiRouter.Get("/authstatus", (*APIContext).AuthStatus) apiRouter.Get("/logout", (*APIContext).Logout) apiRouter.Get("/profile", (*APIContext).UserProfile) apiRouter.Get("/:*", (*APIContext).APIProxy) apiRouter.Put("/:*", (*APIContext).APIProxy) apiRouter.Post("/:*", (*APIContext).APIProxy) apiRouter.Delete("/:*", (*APIContext).APIProxy) // Setup the /uaa subrouter. uaaRouter := secureRouter.Subrouter(UAAContext{}, "/uaa") uaaRouter.Middleware((*UAAContext).OAuth) uaaRouter.Get("/userinfo", (*UAAContext).UserInfo) // Setup the /log subrouter. logRouter := secureRouter.Subrouter(LogContext{}, "/log") logRouter.Middleware((*LogContext).OAuth) logRouter.Get("/recent", (*LogContext).RecentLogs) // Add auth middleware router.Middleware((*Context).LoginRequired) // Frontend Route Initialization // Set up static file serving to load from the static folder. router.Middleware(web.StaticMiddleware("static")) return router }
func NewStaticServer(root string, port int) *Server { server := Server{} server.Root = root server.Port = port server.Router = web.New(emptyContext{}) // NOTE: the middleware does NOT list folder, it is said to avoid content length problem server.Router.Middleware(web.LoggerMiddleware) server.Router.Middleware(web.StaticMiddleware(server.Root, web.StaticOption{IndexFile: "index.html"})) return &server }
func main() { schemaDir = getSchemaDir() router := web.New(Context{}). Middleware(web.LoggerMiddleware). // Use some included middleware Middleware(web.ShowErrorsMiddleware). // ... Middleware(web.StaticMiddleware("www")). Post("/api/v0/keys", (*Context).createKeyValue) http.ListenAndServe("localhost:8080", router) }
func initRouter() *web.Router { rootRouter := web.New(Context{}) rootRouter.Middleware(web.LoggerMiddleware) rootRouter.Middleware(web.ShowErrorsMiddleware) rootRouter.Middleware(web.StaticMiddleware("./media/public", web.StaticOption{Prefix: "/public"})) // "public" is a directory to serve files from.) rootRouter.Middleware((*Context).AssignStorageMiddleware) rootRouter.Middleware((*Context).AssignTemplatesAndSessionsMiddleware) rootRouter.Middleware((*Context).LoadUserMiddleware) rootRouter.Middleware((*Context).GetErrorMessagesMiddleware) rootRouter.Middleware((*Context).GetNotificationMessagesMiddleware) //rootRouter web paths rootRouter.Get(HomeUrl.String(), (*Context).HomeHandler) //sign up, sign in, etc rootRouter.Get(SignUpUrl.String(), (*Context).SignUpHandler) rootRouter.Post(SignUpUrl.String(), (*Context).DoSignUpHandler) rootRouter.Post(SignInUrl.String(), (*Context).DoSignInRequestHandler) rootRouter.Get(VerificationUrl.String(), (*Context).DoVerificationRequestHandler) //password reset handlers rootRouter.Get(RequestPasswordResetUrl.String(), (*Context).BeginPasswordResetRequestHandler) rootRouter.Post(RequestPasswordResetUrl.String(), (*Context).DoBeginPasswordResetRequestHandler) rootRouter.Get(ResetPasswordUrl.String(), (*Context).PasswordResetRequestHandler) rootRouter.Post(ResetPasswordUrl.String(), (*Context).DoPasswordResetRequestHandler) //viewing and listing facts handlers rootRouter.Get(ViewFactUrl.String(), (*Context).ViewFactHandler) rootRouter.Get(ListFactUrl.String(), (*Context).ListFactsHandler) //must be logged in for some handlers... loggedInRouter := rootRouter.Subrouter(LoggedInContext{}, "/") loggedInRouter.Middleware((*LoggedInContext).RequireAccountMiddleware) //sign out handler loggedInRouter.Post(SignOutUrl.String(), (*LoggedInContext).DoSignOutRequestHandler) //vote, moderate fact handlers loggedInRouter.Post(VoteOnFactUrl.String(), (*LoggedInContext).VoteOnFactHandler) loggedInRouter.Post(ModerateFactUrl.String(), (*LoggedInContext).ModerateFactHandler) //create, delete fact handlers loggedInRouter.Get(CreateFactUrl.String(), (*LoggedInContext).CreateFactHandler) loggedInRouter.Post(CreateFactUrl.String(), (*LoggedInContext).DoCreateFactHandler) loggedInRouter.Get(DeleteFactUrl.String(), (*LoggedInContext).DeleteFactHandler) loggedInRouter.Post(DeleteFactUrl.String(), (*LoggedInContext).DoDeleteFactHandler) return rootRouter }
func main() { // Setup router rootRouter := web.New(Context{}) rootRouter.Middleware(web.LoggerMiddleware) rootRouter.Middleware(web.StaticMiddleware("assets")) // Routes rootRouter.Get("/", (*Context).convertStruct) rootRouter.Post("/", (*Context).convertStruct) // Serve port := "3334" fmt.Printf("\x1b[32;1m --------- ConvertStructs [listening on port %s]\x1b[0m", port) http.ListenAndServe("localhost:"+port, rootRouter) }
func initRouter() *web.Router { router := web.New(Context{}) router.Middleware(LogMiddleware) router.Middleware(web.ShowErrorsMiddleware) router.Middleware(web.StaticMiddleware(config.AppConfig().LocalTarballDir)) // OR use 'router.Get("/", FileServer)' router.Middleware((*Context).OutputJson) // API router.Get("/v1/config/reload", (*Context).ConfigReload) router.Post("/v1/transfer", (*Context).Transfer) router.Get("/v1/status", (*Context).Status) router.Post("/v1/destory", (*Context).Destory) // Internal communicate. router.Post("/heartbeat", (*Context).Heartbeat) return router }
func NewRouter() *web.Router { router := web.New(Context{}) // Middleware router. Middleware(web.LoggerMiddleware). Middleware(SafeShowErrorsMiddleware). Middleware(web.StaticMiddleware("public")). Middleware((*Context).SetDatabase) // Routes router. Get("/", (*Context).Home) // Get("/fail", (*Context).Fail). // Get("/login", (*Context).Login). // Get("/signup", (*Context).SignupGet). // Post("/signup", (*Context).SignupPost) return router }
func main() { router := web.New(Context{}) router.Middleware(web.LoggerMiddleware). Middleware(web.ShowErrorsMiddleware). Middleware(web.StaticMiddleware("public")). Middleware((*Context).SetRequestIdentifier) router.Get("/signin", (*Context).Signin) adminRouter := router.Subrouter(AdminContext{}, "/admin") adminRouter.Get("/users", (*AdminContext).UsersList) adminRouter.Get("/exception", (*AdminContext).Exception) adminRouter.Get("/forums/:forum_id:\\d+/suggestions/:suggestion_id:[a-z]+", (*AdminContext).SuggestionView) err := http.ListenAndServe(":8080", router) if err != nil { log.Fatal(err) } }