func main() { log.Info("Peach %s", APP_VER) m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Statics(macaron.StaticOptions{ SkipLogging: setting.ProdMode, }, "custom/public", "public")) m.Use(i18n.I18n(i18n.Options{ Files: setting.Docs.Locales, })) tplDir := "templates" if setting.Page.UseCustomTpl { tplDir = "custom/templates" } m.Use(pongo2.Pongoer(pongo2.Options{ Directory: tplDir, })) m.Use(middleware.Contexter()) m.Get("/", routers.Home) m.Get("/docs", routers.Docs) m.Get("/docs/images/*", routers.DocsStatic) m.Get("/docs/*", routers.Docs) m.Post("/hook", routers.Hook) m.Get("/search", routers.Search) m.Get("/*", routers.Pages) m.NotFound(routers.NotFound) listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HTTPPort) log.Info("%s Listen on %s", setting.Site.Name, listenAddr) log.Fatal("Fail to start Peach: %v", http.ListenAndServe(listenAddr, m)) }
func newInstance() *macaron.Macaron { m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Static("static")) m.Use(pongo2.Pongoer(pongo2.Options{ Directory: "views", IndentJSON: macaron.Env != macaron.PROD, IndentXML: macaron.Env != macaron.PROD, })) m.Use(cache.Cacher()) m.Use(session.Sessioner()) //DoXXX 表示GET请求; //OnXXX 表示POST请求; //AnyXXX 表示GET、POST混合请求 m.Any("/", AnyValidate) m.Get("/dogs", DoDogs) m.Get("/pups", DoPups) m.Get("/about", DoAbout) m.Get("/comment", DoComment) m.Get("/signin", DoSignin) m.Get("/dogDetail", DoDogDetail) m.Get("/pupDetail", DoPupDetail) m.Post("/onComment", OnComment) m.Post("/onSignin", OnSignin) return m }
// newMacaron initializes Macaron instance. func newMacaron() *macaron.Macaron { m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) if setting.EnableGzip { m.Use(macaron.Gziper()) } m.Use(macaron.Static( path.Join(setting.StaticRootPath, "public"), macaron.StaticOptions{ SkipLogging: !setting.DisableRouterLog, }, )) m.Use(macaron.Renderer(macaron.RenderOptions{ Directory: path.Join(setting.StaticRootPath, "templates"), Funcs: []template.FuncMap{base.TemplateFuncs}, IndentJSON: macaron.Env != macaron.PROD, })) m.Use(i18n.I18n(i18n.Options{ SubURL: setting.AppSubUrl, Directory: path.Join(setting.ConfRootPath, "locale"), CustomDirectory: path.Join(setting.CustomPath, "conf/locale"), Langs: setting.Langs, Names: setting.Names, Redirect: true, })) m.Use(cache.Cacher(cache.Options{ Adapter: setting.CacheAdapter, Interval: setting.CacheInternal, Conn: setting.CacheConn, })) m.Use(captcha.Captchaer(captcha.Options{ SubURL: setting.AppSubUrl, })) m.Use(session.Sessioner(session.Options{ Provider: setting.SessionProvider, Config: *setting.SessionConfig, })) m.Use(csrf.Generate(csrf.Options{ Secret: setting.SecretKey, SetCookie: true, Header: "X-Csrf-Token", CookiePath: setting.AppSubUrl, })) m.Use(toolbox.Toolboxer(m, toolbox.Options{ HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{ &toolbox.HealthCheckFuncDesc{ Desc: "Database connection", Func: models.Ping, }, }, })) m.Use(middleware.Contexter()) return m }
func newMacaron() *macaron.Macaron { m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Static("static")) m.Use(session.Sessioner(session.Options{ Provider: "mysql", ProviderConfig: beego.AppConfig.String("mysqlstring"), })) m.Use(middleware.Contexter()) m.Use(pongo2.Pongoer(pongo2.Options{ Directory: "views", })) return m }
// newMacaron initializes Macaron instance. func newMacaron() *macaron.Macaron { m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Static("public", macaron.StaticOptions{ SkipLogging: setting.ProdMode, }, )) m.Use(macaron.Static("raw", macaron.StaticOptions{ Prefix: "raw", SkipLogging: setting.ProdMode, })) m.Use(pongo2.Pongoer(pongo2.Options{ IndentJSON: !setting.ProdMode, })) m.Use(i18n.I18n()) m.Use(session.Sessioner()) m.Use(middleware.Contexter()) return m }
// newMacaron initializes Macaron instance. func newMacaron() *macaron.Macaron { m := macaron.New() if !setting.DisableRouterLog { m.Use(macaron.Logger()) } m.Use(macaron.Recovery()) if setting.EnableGzip { m.Use(macaron.Gziper()) } if setting.Protocol == setting.FCGI { m.SetURLPrefix(setting.AppSubUrl) } m.Use(macaron.Static( path.Join(setting.StaticRootPath, "public"), macaron.StaticOptions{ SkipLogging: setting.DisableRouterLog, }, )) m.Use(macaron.Static( setting.AvatarUploadPath, macaron.StaticOptions{ Prefix: "avatars", SkipLogging: setting.DisableRouterLog, }, )) m.Use(macaron.Renderer(macaron.RenderOptions{ Directory: path.Join(setting.StaticRootPath, "templates"), Funcs: []template.FuncMap{base.TemplateFuncs}, IndentJSON: macaron.Env != macaron.PROD, })) localeNames, err := bindata.AssetDir("conf/locale") if err != nil { log.Fatal(4, "Fail to list locale files: %v", err) } localFiles := make(map[string][]byte) for _, name := range localeNames { localFiles[name] = bindata.MustAsset("conf/locale/" + name) } m.Use(i18n.I18n(i18n.Options{ SubURL: setting.AppSubUrl, Files: localFiles, CustomDirectory: path.Join(setting.CustomPath, "conf/locale"), Langs: setting.Langs, Names: setting.Names, Redirect: true, })) m.Use(cache.Cacher(cache.Options{ Adapter: setting.CacheAdapter, AdapterConfig: setting.CacheConn, Interval: setting.CacheInternal, })) m.Use(captcha.Captchaer(captcha.Options{ SubURL: setting.AppSubUrl, })) m.Use(session.Sessioner(setting.SessionConfig)) m.Use(csrf.Csrfer(csrf.Options{ Secret: setting.SecretKey, SetCookie: true, Header: "X-Csrf-Token", CookiePath: setting.AppSubUrl, })) m.Use(toolbox.Toolboxer(m, toolbox.Options{ HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{ &toolbox.HealthCheckFuncDesc{ Desc: "Database connection", Func: models.Ping, }, }, })) // OAuth 2. if setting.OauthService != nil { for _, info := range setting.OauthService.OauthInfos { m.Use(oauth2.NewOAuth2Provider(info.Options, info.AuthUrl, info.TokenUrl)) } } m.Use(middleware.Contexter()) return m }
func main() { m := macaron.Classic() // New permissions middleware perm, err := permissionHSTORE.New() if err != nil { log.Fatalln(err) } // Blank slate, no default permissions //perm.Clear() // Logging middleware m.Use(macaron.Logger()) // Renderer middleware m.Use(macaron.Renderer()) // Set up a middleware handler for Macaron, with a custom "permission denied" message. permissionHandler := func(ctx *macaron.Context) { // Check if the user has the right admin/user rights if perm.Rejected(ctx.Resp, ctx.Req.Request) { fmt.Fprintf(ctx.Resp, "Permission denied!") // Deny the request ctx.Error(http.StatusForbidden) // Don't call other middleware handlers return } // Call the next middleware handler ctx.Next() } // Enable the permissions middleware, must come before recovery m.Use(permissionHandler) // Recovery middleware m.Use(macaron.Recovery()) // Get the userstate, used in the handlers below userstate := perm.UserState() m.Get("/", func(ctx *macaron.Context) (msg string) { msg += fmt.Sprintf("Has user bob: %v\n", userstate.HasUser("bob")) msg += fmt.Sprintf("Logged in on server: %v\n", userstate.IsLoggedIn("bob")) msg += fmt.Sprintf("Is confirmed: %v\n", userstate.IsConfirmed("bob")) msg += fmt.Sprintf("Username stored in cookies (or blank): %v\n", userstate.Username(ctx.Req.Request)) msg += fmt.Sprintf("Current user is logged in, has a valid cookie and *user rights*: %v\n", userstate.UserRights(ctx.Req.Request)) msg += fmt.Sprintf("Current user is logged in, has a valid cookie and *admin rights*: %v\n", userstate.AdminRights(ctx.Req.Request)) msg += fmt.Sprintln("\nTry: /register, /confirm, /remove, /login, /logout, /makeadmin, /clear, /data and /admin") return // msg }) m.Get("/register", func(ctx *macaron.Context) string { userstate.AddUser("bob", "hunter1", "*****@*****.**") return fmt.Sprintf("User bob was created: %v\n", userstate.HasUser("bob")) }) m.Get("/confirm", func(ctx *macaron.Context) string { userstate.MarkConfirmed("bob") return fmt.Sprintf("User bob was confirmed: %v\n", userstate.IsConfirmed("bob")) }) m.Get("/remove", func(ctx *macaron.Context) string { userstate.RemoveUser("bob") return fmt.Sprintf("User bob was removed: %v\n", !userstate.HasUser("bob")) }) m.Get("/login", func(ctx *macaron.Context) string { // Headers will be written, for storing a cookie userstate.Login(ctx.Resp, "bob") return fmt.Sprintf("bob is now logged in: %v\n", userstate.IsLoggedIn("bob")) }) m.Get("/logout", func(ctx *macaron.Context) string { userstate.Logout("bob") return fmt.Sprintf("bob is now logged out: %v\n", !userstate.IsLoggedIn("bob")) }) m.Get("/makeadmin", func(ctx *macaron.Context) string { userstate.SetAdminStatus("bob") return fmt.Sprintf("bob is now administrator: %v\n", userstate.IsAdmin("bob")) }) m.Get("/clear", func(ctx *macaron.Context) string { userstate.ClearCookie(ctx.Resp) return "Clearing cookie" }) m.Get("/data", func(ctx *macaron.Context) string { return "user page that only logged in users must see!" }) m.Get("/admin", func(ctx *macaron.Context) { fmt.Fprintf(ctx.Resp, "super secret information that only logged in administrators must see!\n\n") if usernames, err := userstate.AllUsernames(); err == nil { fmt.Fprintf(ctx.Resp, "list of all users: "+strings.Join(usernames, ", ")) } }) // Serve m.Run(3000) }
func daemon(c *cobra.Command, a []string) { m := macaron.New() setting.NewContext(c) err := models.NewEngine() m.Use(middleware.Contexter()) m.Use(macaron.Recovery()) m.Use(macaron.Logger()) m.Use(macaron.Renderer( macaron.RenderOptions{ Directory: "templates", TemplateFileSystem: bindata.Templates(bindata.Options{ Asset: templates.Asset, AssetDir: templates.AssetDir, AssetNames: templates.AssetNames, Prefix: "", }), }, )) m.Use(macaron.Static("web/images", macaron.StaticOptions{ Prefix: "images", FileSystem: bindata.Static(bindata.Options{ Asset: web.Asset, AssetDir: web.AssetDir, AssetNames: web.AssetNames, Prefix: "web/images", }), }, )) m.Use(macaron.Static("web/template", macaron.StaticOptions{ Prefix: "template", FileSystem: bindata.Static(bindata.Options{ Asset: web.Asset, AssetDir: web.AssetDir, AssetNames: web.AssetNames, Prefix: "web/template", }), }, )) m.Use(macaron.Static("web", macaron.StaticOptions{ FileSystem: bindata.Static(bindata.Options{ Asset: web.Asset, AssetDir: web.AssetDir, AssetNames: web.AssetNames, Prefix: "web", }), Prefix: viper.GetString("version"), }, )) m.Get("/assets/html/user/views/oauth.html", user.OauthHandler) m.Combo("/api/oauth"). Get(user.OauthUrl). Post(binding.Json(auth.Oauth2{}), user.OauthLogin) m.Post("/api/login", binding.Json(auth.SignIn{}), user.SignIn) m.Post("/api/register", binding.Json(auth.SignUp{}), user.SignUp) m.Group("/api", func() { m.Get("/boards", board.ListBoards) m.Post("/boards/configure", binding.Json(models.BoardRequest{}), board.Configure) m.Get("/board", board.ItemBoard) m.Get("/labels", board.ListLabels) m.Get("/cards", board.ListCards) m.Get("/milestones", board.ListMilestones) m.Get("/users", board.ListMembers) m.Combo("/comments"). Get(board.ListComments). Post(binding.Json(models.CommentRequest{}), board.CreateComment) m.Combo("/card"). Post(binding.Json(models.CardRequest{}), board.CreateCard). Put(binding.Json(models.CardRequest{}), board.UpdateCard). Delete(binding.Json(models.CardRequest{}), board.DeleteCard) m.Put("/card/move", binding.Json(models.CardRequest{}), board.MoveToCard) }, middleware.Auther()) m.Get("/*", routers.Home) m.Get("/ws/", sockets.Messages(), ws.ListenAndServe) listen := viper.GetString("server.listen") log.Printf("Listen: %s", listen) err = http.ListenAndServe(listen, m) if err != nil { log.Fatalf("Failed to start: %s", err) } }
func main() { log.Info("%s %s", setting.AppName, APP_VER) log.Info("Run Mode: %s", strings.Title(macaron.Env)) m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Static("public", macaron.StaticOptions{ SkipLogging: true, })) m.Use(pongo2.Pongoers(pongo2.Options{ Directory: "templates/web", IndentJSON: macaron.Env != macaron.PROD, }, "templates/admin")) m.Use(i18n.I18n()) m.Use(session.Sessioner()) m.Use(middleware.Contexter()) // Routes. m.Get("/", routers.Home) m.Route("/download", "GET,POST", routers.Download) m.Get("/favicon.ico", func(ctx *middleware.Context) { ctx.Redirect("/img/favicon.png") }) // m.Get("/search", routers.Search) // m.Get("/about", routers.About) // Package. m.Get("/*", routers.Package) m.Get("/badge/*", routers.Badge) // Admin. m.Post("/admin/auth", admin.AuthPost) m.Group("/admin", func() { m.Get("", admin.Dashboard) m.Group("/packages", func() { m.Get("", admin.Revisions) m.Get("/larges", admin.LargeRevisions) }) m.Group("/blocks", func() { m.Get("", admin.Blocks) m.Combo("/new").Get(admin.BlockPackage).Post(admin.BlockPackagePost) m.Get("/:id:int/delete", admin.UnblockPackage) m.Group("/rules", func() { m.Get("", admin.BlockRules) m.Combo("/new").Get(admin.NewBlockRule).Post(admin.NewBlockRulePost) m.Get("/:id:int/run", admin.RunRule) m.Get("/:id:int/delete", admin.DeleteBlockRule) }) }) }, admin.Auth) // API. m.Group("/api", func() { m.Group("/v1", func() { m.Group("", func() { m.Get("/download", v1.Download) m.Get("/revision", v1.GetRevision) }, v1.PackageFilter()) }) }) // Robots.txt m.Get("/robots.txt", func() string { return `User-agent: * Disallow: /api/ Disallow: /download` }) m.NotFound(routers.NotFound) listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HttpPort) log.Info("Listen: http://%s", listenAddr) if err := http.ListenAndServe(listenAddr, m); err != nil { log.Fatal(4, "Fail to start server: %v", err) } }
func runWeb(c *cli.Context) { if err := models.InitDb(); err != nil { log.Fatal("Fail to init DB: %v", err) } if c.IsSet("port") { setting.HTTPPort = c.Int("port") } bindIgnErr := binding.BindIgnErr m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(macaron.Renderer(macaron.RenderOptions{ IndentJSON: !setting.ProdMode, })) m.Use(web.Contexter()) group := func() { m.Group("/flows", func() { m.Combo("").Get(web.Flows). Post(bindIgnErr(api.CreateFlowOptions{}), web.CreateFlow) m.Combo("/:uuid").Get(web.GetFlow). Post(bindIgnErr(api.CreateFlowOptions{}), web.UpdateFlow). Delete(web.DeleteFlow) }) m.Group("/pipelines", func() { m.Combo("").Get(web.Pipelines). Post(bindIgnErr(api.CreatePipelineOptions{}), web.CreatePipeline) m.Combo("/:uuid").Get(web.GetPipeline). Post(bindIgnErr(api.CreatePipelineOptions{}), web.UpdatePipeline). Delete(web.DeletePipeline) }) m.Group("/stages", func() { m.Combo("").Get(web.Stages). Post(bindIgnErr(api.CreateStageOptions{}), web.CreateStage) m.Combo("/:uuid").Get(web.GetStage). Post(bindIgnErr(api.CreateStageOptions{}), web.UpdateStage). Delete(web.DeleteStage) }) m.Group("/jobs", func() { m.Combo("").Get(web.Jobs). Post(bindIgnErr(api.CreateJobOptions{}), web.CreateJob) m.Combo("/:uuid").Get(web.GetJob). Post(bindIgnErr(api.CreateJobOptions{}), web.UpdateJob). Delete(web.DeleteJob) }) m.Post("/build", web.Build) } m.Group("", group) m.Group("/v1", group) listenAddr := fmt.Sprintf("0.0.0.0:%d", setting.HTTPPort) log.Info("Vessel %s %s", setting.AppVer, listenAddr) if err := http.ListenAndServe(listenAddr, m); err != nil { log.Fatal("Fail to start web server: %v", err) } }