コード例 #1
0
ファイル: main.go プロジェクト: myafeier/GoCMS
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()
}
コード例 #2
0
ファイル: app.go プロジェクト: ioddly/meditations
func App() *macaron.Macaron {
	m := macaron.Classic()

	DBOpen()

	if Config.Development == true {
		macaron.Env = "development"
	} else {
		macaron.Env = "production"
	}
	m.Use(session.Sessioner())
	m.Use(csrf.Csrfer())
	m.Use(pongo2.Pongoer(pongo2.Options{
		Directory:  "templates",
		Extensions: []string{".htm"},
	}))

	// Serve static files from /assets
	m.Use(macaron.Static("assets", macaron.StaticOptions{Prefix: "assets"}))

	m.Use(func(c *macaron.Context) {
		c.Data["SiteTitle"] = Config.SiteTitle
		c.Data["Development"] = Config.Development
		c.Next()
	})

	// Routes
	m.Get("/favicon.ico", func(c *macaron.Context) {
		c.ServeFileContent("favicon.ico")
	})

	m.Get("/", func(c *macaron.Context) {
		c.Redirect("/habits")
	})

	init := func(x string, r func(m *macaron.Macaron)) { m.Group(x, func() { r(m) }) }

	init("/habits", habitsInit)
	init("/journal", journalInit)
	init("/log", logInit)

	return m
}
コード例 #3
0
ファイル: web.go プロジェクト: zhuharev/smoljanin.ru
func newMacaron() *macaron.Macaron {
	m := macaron.New()
	m.Use(macaron.Renderer(macaron.RenderOptions{Layout: "layout",
		Funcs: []template.FuncMap{{
			"markdown": base.Markdown,
			"raw":      func(s string) template.HTML { return template.HTML(s) },
			"momentDiff": func(t time.Time) string {
				return since.Since(t)
			},
		}}}))
	/*	m.Use(func(c *macaron.Context) {
		if strings.HasSuffix(c.Req.URL.Path, ".json") {
			color.Green("JSON")

			c.Req.Request.URL

			c.Req.URL.Path = strings.TrimSuffix(c.Req.URL.Path, ".json")
			c.Req.URL.RawPath = strings.TrimSuffix(c.Req.URL.RawPath, ".json")
			c.Req.RequestURI = c.Req.URL.RequestURI()

			c.Data["json"] = true
		}
		c.Next()
	})*/
	m.Use(cache.Cacher())
	m.Use(session.Sessioner())
	m.Use(csrf.Csrfer())
	m.Use(macaron.Static("static"))
	m.Use(macaron.Static("data/uploads"))
	m.Use(macaron.Static("data/public", macaron.StaticOptions{Prefix: "public"}))

	m.Use(i18n.I18n(i18n.Options{
		Langs: []string{"en-US", "ru-RU"},
		Names: []string{"English", "Русский"},
	}))

	m.Use(middleware.Contexter())

	return m
}
コード例 #4
0
ファイル: web.go プロジェクト: abhijitmamarde/gogs
// 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
}
コード例 #5
0
ファイル: web.go プロジェクト: kuuyee/gogs-learn
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
}