本文整理汇总了Golang中github.com/urandom/webfw/context.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetConfig
// GetConfig is a helper function for getting the current config
// from the request context.
func GetConfig(c context.Context) Config {
if val, ok := c.GetGlobal(context.BaseCtxKey("config")); ok {
return val.(Config)
}
return Config{}
}
示例2: Handler
func (con Component) Handler(c context.Context) http.Handler {
mw, i18nFound := con.dispatcher.Middleware("I18N")
logger := webfw.GetLogger(c)
rnd := renderer.NewRenderer(con.dispatcher.Config.Renderer.Dir, "raw.tmpl")
rnd.Delims("{%", "%}")
if i18nFound {
if i18n, ok := mw.(middleware.I18N); ok {
rnd.Funcs(i18n.TemplateFuncMap())
}
} else {
logger.Infoln("I18N middleware not found")
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
params := webfw.GetParams(c, r)
err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern},
c.GetAll(r), "components/"+params["name"]+".tmpl")
if err != nil {
webfw.GetLogger(c).Print(err)
}
})
}
示例3: GetParams
// GetParams returns the current request path parameters from the context.
func GetParams(c context.Context, r *http.Request) RouteParams {
if val, ok := c.Get(r, context.BaseCtxKey("params")); ok {
return val.(RouteParams)
}
return RouteParams{}
}
示例4: GetConfig
func GetConfig(c context.Context) Config {
if v, ok := c.GetGlobal(CtxKey("config")); ok {
return v.(Config)
}
return Config{}
}
示例5: GetLanguage
// GetLanguage returns the current request language, such as "en", or "bg-BG"
// from the context, if the I18N middleware is in use.
func GetLanguage(c context.Context, r *http.Request) string {
if val, ok := c.Get(r, context.BaseCtxKey("lang")); ok {
return val.(string)
}
return GetFallbackLanguage(c, r)
}
示例6: GetRepo
func GetRepo(c context.Context) content.Repo {
if v, ok := c.GetGlobal(CtxKey("repo")); ok {
return v.(content.Repo)
}
return nil
}
示例7: GetRenderCtx
// GetRenderCtx returns a RenderCtx wrapper around the current raw renderer
// The wrapper automatically adds the current request ContextData to the
// renderer's Render method call.
func GetRenderCtx(c context.Context, r *http.Request) renderer.RenderCtx {
rnd := GetRenderer(c)
return renderer.RenderCtx(func(w io.Writer, data renderer.RenderData, names ...string) error {
return rnd.Render(w, data, c.GetAll(r), names...)
})
}
示例8: Handler
func (con App) Handler(c context.Context) http.Handler {
cfg := readeef.GetConfig(c)
rnd := webfw.GetRenderer(c)
if cfg.Logger.Level == "debug" {
rnd.SkipCache(true)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
action := webfw.GetMultiPatternIdentifier(c, r)
data := renderer.RenderData{}
if action == "history" {
params := webfw.GetParams(c, r)
data["history"] = "/web/" + params["history"]
}
if r.Method != "HEAD" {
err := rnd.Render(w, data, c.GetAll(r), "app.tmpl")
if err != nil {
webfw.GetLogger(c).Print(err)
}
}
w.Header().Set("X-Readeef", "1")
})
}
示例9: GetUser
func GetUser(c context.Context, r *http.Request) content.User {
if v, ok := c.Get(r, context.BaseCtxKey("user")); ok {
return v.(content.User)
}
return nil
}
示例10: GetLogger
// GetLogger returns the error logger, to be used if an error occurs during
// a request.
func GetLogger(c context.Context) Logger {
if val, ok := c.GetGlobal(context.BaseCtxKey("logger")); ok {
return val.(Logger)
}
return NewStandardLogger(os.Stderr, "", 0)
}
示例11: GetRenderer
// GetRenderer returns the current raw renderer from the context.
func GetRenderer(c context.Context) renderer.Renderer {
if val, ok := c.GetGlobal(context.BaseCtxKey("renderer")); ok {
return val.(renderer.Renderer)
}
return renderer.NewRenderer("template", "base.tmpl")
}
示例12: forbidden
func forbidden(c context.Context, r *http.Request) bool {
if v, ok := c.Get(r, readeef.CtxKey("forbidden")); ok {
if f, ok := v.(bool); ok {
return f
}
}
return false
}
示例13: Handler
func (con App) Handler(c context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rnd := webfw.GetRenderer(c)
err := rnd.Render(w, nil, c.GetAll(r), "app.tmpl")
if err != nil {
webfw.GetLogger(c).Print(err)
}
}
}
示例14: Handler
func (con Component) Handler(c context.Context) http.HandlerFunc {
rnd := renderer.NewRenderer(con.dir, "raw.tmpl")
rnd.Delims("{%", "%}")
return func(w http.ResponseWriter, r *http.Request) {
params := webfw.GetParams(c, r)
err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern},
c.GetAll(r), "components/"+params["name"]+".tmpl")
if err != nil {
webfw.GetLogger(c).Print(err)
}
}
}
示例15: GetDB
func GetDB(c context.Context) DB {
if v, ok := c.GetGlobal(CtxKey("db")); ok {
return v.(DB)
}
conf := GetConfig(c)
db := NewDB(conf.DB.Driver, conf.DB.Connect)
if err := db.Connect(); err != nil {
panic(err)
}
return db
}