本文整理汇总了Golang中github.com/Unknwon/macaron.Recovery函数的典型用法代码示例。如果您正苦于以下问题:Golang Recovery函数的具体用法?Golang Recovery怎么用?Golang Recovery使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Recovery函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
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))
}
示例2: _StartMartini
func _StartMartini() {
WEB.Use(macaron.Recovery())
WEB.Use(macaron.Static("public",
macaron.StaticOptions{
FileSystem: bindata.Static(bindata.Options{
Asset: public.Asset,
AssetDir: public.AssetDir,
AssetNames: public.AssetNames,
Prefix: "",
}),
SkipLogging: true,
},
))
WEB.Use(macaron.Renderer(macaron.RenderOptions{
//Directory: "templates", //!!when package,escape this line. Specify what path to load the templates from.
Layout: "layout", // Specify a layout template. Layouts can call {{ yield }} to render the current template.
//Extensions: []string{".tmpl", ".html"}, //!!when package,escape this line. Specify extensions to load for templates.
//Charset: "UTF-8", //!!when package,escape this line. Sets encoding for json and html content-types. Default is "UTF-8".
TemplateFileSystem: bindata.Templates(bindata.Options{ //make templates files into bindata.
Asset: templates.Asset,
AssetDir: templates.AssetDir,
AssetNames: templates.AssetNames,
Prefix: ""}),
}))
LoadRoute()
WEB.Run(cfg.HOST, cfg.PORT)
}
示例3: newMacaron
func newMacaron() *macaron.Macaron {
macaron.Env = setting.Env
m := macaron.New()
m.Use(middleware.Logger())
m.Use(macaron.Recovery())
if setting.EnableGzip {
m.Use(middleware.Gziper())
}
mapStatic(m, "", "public")
mapStatic(m, "app", "app")
mapStatic(m, "css", "css")
mapStatic(m, "img", "img")
mapStatic(m, "fonts", "fonts")
mapStatic(m, "robots.txt", "robots.txt")
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
IndentJSON: macaron.Env != macaron.PROD,
Delims: macaron.Delims{Left: "[[", Right: "]]"},
}))
if setting.EnforceDomain {
m.Use(middleware.ValidateHostHeader(setting.Domain))
}
m.Use(middleware.GetContextHandler())
m.Use(middleware.Sessioner(&setting.SessionOptions))
return m
}
示例4: newInstance
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
}
示例5: SetMiddlewares
func SetMiddlewares(m *macaron.Macaron) {
m.Use(macaron.Static("static", macaron.StaticOptions{
Expires: func() string { return "max-age=0" },
}))
m.Map(Log)
m.Use(logger())
m.Use(macaron.Recovery())
}
示例6: newMacaron
// 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
}
示例7: SetMiddlewares
func SetMiddlewares(m *macaron.Macaron) {
m.Use(macaron.Static("static", macaron.StaticOptions{
Expires: func() string { return "max-age=0" },
}))
InitLog(setting.RunMode, setting.LogPath)
m.Map(Log)
m.Use(logger(setting.RunMode))
m.Use(macaron.Recovery())
}
示例8: newMacaron
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
}
示例9: SetMiddlewares
func SetMiddlewares(m *macaron.Macaron) {
//Set static file directory,static file access without log output
m.Use(macaron.Static("external", macaron.StaticOptions{
Expires: func() string { return "max-age=0" },
}))
InitLog(setting.RunMode, setting.LogPath)
//Set global Logger
m.Map(Log)
//Set logger handler function, deal with all the Request log output
m.Use(logger(setting.RunMode))
//Set the response header info
m.Use(setRespHeaders())
//Set recovery handler to returns a middleware that recovers from any panics
m.Use(macaron.Recovery())
}
示例10: SetMiddlewares
func SetMiddlewares(m *macaron.Macaron) {
m.Use(macaron.Static("external", macaron.StaticOptions{
Expires: func() string { return "max-age=0" },
}))
m.Map(Log)
//modify default template setting
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: "views",
Extensions: []string{".tmpl", ".html"},
Funcs: []template.FuncMap{},
Delims: macaron.Delims{"<<<", ">>>"},
Charset: "UTF-8",
IndentJSON: true,
IndentXML: true,
PrefixXML: []byte("macaron"),
HTMLContentType: "text/html",
}))
m.Use(macaron.Recovery())
}
示例11: newMacaron
func newMacaron() *macaron.Macaron {
macaron.Env = setting.Env
m := macaron.New()
m.Use(middleware.Logger())
m.Use(macaron.Recovery())
m.Use(toolbox.Toolboxer(m))
m.Use(func(ctx *macaron.Context) {
if ctx.Req.URL.Path == "/debug/vars" {
http.DefaultServeMux.ServeHTTP(ctx.Resp, ctx.Req.Request)
}
})
if setting.EnableGzip {
m.Use(middleware.Gziper())
}
mapStatic(m, "", "public")
mapStatic(m, "app", "app")
mapStatic(m, "css", "css")
mapStatic(m, "img", "img")
mapStatic(m, "fonts", "fonts")
mapStatic(m, "plugins", "plugins")
mapStatic(m, "robots.txt", "robots.txxt")
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
IndentJSON: macaron.Env != macaron.PROD,
Delims: macaron.Delims{Left: "[[", Right: "]]"},
}))
if setting.EnforceDomain {
m.Use(middleware.ValidateHostHeader(setting.Domain))
}
m.Use(middleware.GetContextHandler())
m.Use(middleware.Sessioner(&setting.SessionOptions))
return m
}
示例12: newMacaron
// 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
}
示例13: newMacaron
func newMacaron() *macaron.Macaron {
macaron.Env = setting.Env
m := macaron.New()
m.Use(middleware.Logger())
m.Use(macaron.Recovery())
if setting.EnableGzip {
m.Use(middleware.Gziper())
}
for _, route := range plugins.StaticRoutes {
pluginRoute := path.Join("/public/plugins/", route.Url)
log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Path)
mapStatic(m, route.Path, "", pluginRoute)
}
mapStatic(m, setting.StaticRootPath, "", "public")
mapStatic(m, setting.StaticRootPath, "app", "app")
mapStatic(m, setting.StaticRootPath, "css", "css")
mapStatic(m, setting.StaticRootPath, "img", "img")
mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
IndentJSON: macaron.Env != macaron.PROD,
Delims: macaron.Delims{Left: "[[", Right: "]]"},
}))
if setting.EnforceDomain {
m.Use(middleware.ValidateHostHeader(setting.Domain))
}
m.Use(middleware.GetContextHandler())
m.Use(middleware.Sessioner(&setting.SessionOptions))
return m
}
示例14: newMacaron
// 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
}
示例15: main
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", "[email protected]")
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
//.........这里部分代码省略.........