func initCaptch(m *macaron.Macaron) { m.Use(cache.Cacher()) width := 240 height := 80 expiration := int64(600) cfg := macaron.Config() capcfg, err := cfg.GetSection("captcha") if err != nil { width = capcfg.Key("width").MustInt(width) height = capcfg.Key("height").MustInt(height) expiration = capcfg.Key("expire").MustInt64(expiration) } m.Use(captcha.Captchaer(captcha.Options{ URLPrefix: "/captcha/img/", // 获取验证码图片的 URL 前缀,默认为 "/captcha/" FieldIdName: "captcha_id", // 表单隐藏元素的 ID 名称,默认为 "captcha_id" FieldCaptchaName: "captcha", // 用户输入验证码值的元素 ID,默认为 "captcha" ChallengeNums: 6, // 验证字符的个数,默认为 6 Width: width, // 验证码图片的宽度,默认为 240 像素 Height: height, // 验证码图片的高度,默认为 80 像素 Expiration: expiration, // 验证码过期时间,默认为 600 秒 CachePrefix: "captcha_", // 用于存储验证码正确值的 Cache 键名,默认为 "captcha_" })) }
func main() { m := macaron.Classic() m.Use(cache.Cacher()) // m.Use(session.Sessioner()) m.Use(session.Sessioner(session.Options{ Provider: "memory", ProviderConfig: "", CookieName: "Kfx", CookiePath: "/", Gclifetime: 3600, Maxlifetime: 3600, Secure: false, CookieLifeTime: 0, Domain: "/", IDLength: 16, Section: "session", })) m.Use(csrf.Csrfer()) m.Use(captcha.Captchaer(captcha.Options{ // 获取验证码图片的 URL 前缀,默认为 "/captcha/" URLPrefix: "/captcha/", // 表单隐藏元素的 ID 名称,默认为 "captcha_id" FieldIdName: "captcha_id", // 用户输入验证码值的元素 ID,默认为 "captcha" FieldCaptchaName: "captcha", // 验证字符的个数,默认为 6 ChallengeNums: 6, // 验证码图片的宽度,默认为 240 像素 Width: 240, // 验证码图片的高度,默认为 80 像素 Height: 80, // 验证码过期时间,默认为 600 秒 Expiration: 600, // 用于存储验证码正确值的 Cache 键名,默认为 "captcha_" CachePrefix: "captcha_", })) m.Use(renders.Renderer(renders.Options{ Directory: "templates", Extensions: []string{".html"}, Charset: "UTF-8", IndentJSON: true, IndentXML: true, HTMLContentType: "text/html", })) m.Get("/", index.Index) m.NotFound(func(r renders.Render) { r.HTML(200, "404.html", map[string]interface{}{"Title": "Home"}) }) m.Run() }
func main() { log.Debug("Starting server...") m := macaron.New() m.Use(macaron.Logger()) m.Use(macaron.Recovery()) m.Use(cache.Cacher()) m.Use(session.Sessioner(session.Options{CookieName: "s"})) m.Use(captcha.Captchaer(captcha.Options{Width: 120, Height: 40})) m.Use(macaron.Static("static", macaron.StaticOptions{Prefix: "/static"})) m.Use(pongo2.Pongoer()) //m.Use(i18n.I18n(i18n.Options{ // Langs: []string{"en-US", "zh-CN"}, // Names: []string{"English", "简体中文"}, //})) m.Use(spider.SpiderFunc()) m.Use(token.Tokener()) boot.BootStrap() router.Route(m) m.Run(boot.WebListenIP, boot.WebPort) }
// 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(gzip.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: []gotmpl.FuncMap{template.Funcs}, 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, DefaultLang: "en-US", 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, }, }, })) m.Use(middleware.Contexter()) return m }
func newMacaron() *macaron.Macaron { m := macaron.New() // DISABLE_ROUTER_LOG: 激活该选项来禁止打印路由日志 // 判断是否禁用,如果禁用则引入macaron日志 if !setting.DisableRouterLog { m.Use(macaron.Logger()) } // 引入macaron恢复机制 m.Use(macaron.Recovery()) 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"), AppendDirectories: []string{path.Join(setting.CustomPath, "templates")}, Funcs: template.NewFuncMap(), 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, DefaultLang: "en-US", 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, Cookie: setting.CSRFCookieName, 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(context.Contexter()) return m }