• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang context.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/urandom/webfw/context.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Context类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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{}
}
开发者ID:urandom,项目名称:webfw,代码行数:9,代码来源:context_util.go


示例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)
		}
	})
}
开发者ID:RavenB,项目名称:readeef,代码行数:26,代码来源:component.go


示例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{}
}
开发者ID:urandom,项目名称:webfw,代码行数:8,代码来源:context_util.go


示例4: GetConfig

func GetConfig(c context.Context) Config {
	if v, ok := c.GetGlobal(CtxKey("config")); ok {
		return v.(Config)
	}

	return Config{}
}
开发者ID:urandom,项目名称:readeef,代码行数:7,代码来源:context_util.go


示例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)
}
开发者ID:urandom,项目名称:webfw,代码行数:9,代码来源:context_util.go


示例6: GetRepo

func GetRepo(c context.Context) content.Repo {
	if v, ok := c.GetGlobal(CtxKey("repo")); ok {
		return v.(content.Repo)
	}

	return nil
}
开发者ID:urandom,项目名称:readeef,代码行数:7,代码来源:context_util.go


示例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...)
	})
}
开发者ID:urandom,项目名称:webfw,代码行数:10,代码来源:context_util.go


示例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")
	})
}
开发者ID:urandom,项目名称:readeef,代码行数:27,代码来源:app.go


示例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
}
开发者ID:urandom,项目名称:readeef,代码行数:7,代码来源:context_util.go


示例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)
}
开发者ID:urandom,项目名称:webfw,代码行数:9,代码来源:context_util.go


示例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")
}
开发者ID:urandom,项目名称:webfw,代码行数:8,代码来源:context_util.go


示例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
}
开发者ID:urandom,项目名称:readeef,代码行数:9,代码来源:web_socket.go


示例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)
		}
	}
}
开发者ID:yourchanges,项目名称:readeef,代码行数:10,代码来源:app.go


示例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)
		}
	}
}
开发者ID:yourchanges,项目名称:readeef,代码行数:15,代码来源:component.go


示例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
}
开发者ID:yourchanges,项目名称:readeef,代码行数:15,代码来源:context_util.go


示例16: Handler

func (con App) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rnd := webfw.GetRenderer(c)
		action := webfw.GetMultiPatternIdentifier(c, r)

		data := renderer.RenderData{}
		if action == "history" {
			params := webfw.GetParams(c, r)
			data["history"] = "/web/" + params["history"]
		}

		err := rnd.Render(w, data, c.GetAll(r), "app.tmpl")
		if err != nil {
			webfw.GetLogger(c).Print(err)
		}
	})
}
开发者ID:RavenB,项目名称:readeef,代码行数:17,代码来源:app.go


示例17: URL

// The URL function provides the functionality of the url template functions
// for use outside of the template context. The dispatcherPattern is the
// pattern used by the dispatcher responsible for handling the resulting url.
// In most cases it will probably be "/".
func URL(c context.Context, r *http.Request, dispatcherPattern string, parts []string) (string, error) {
	base, err := unlocalizedUrl(r, parts)

	if err != nil {
		return "", err
	}

	if lang, ok := c.Get(r, context.BaseCtxKey("lang")); ok && len(lang.(string)) > 0 {
		base = "/" + lang.(string) + base
	}
	if len(dispatcherPattern) > 1 {
		base = dispatcherPattern[:len(dispatcherPattern)-1] + base
	}

	if len(base) > 1 && base[len(base)-1] == '/' {
		base = base[:len(base)-1]
	}
	return base, nil
}
开发者ID:urandom,项目名称:webfw,代码行数:23,代码来源:url.go


示例18: Handler

func (con Component) Handler(c context.Context) http.Handler {
	i18nmw, i18nFound := con.dispatcher.Middleware("I18N")
	urlmw, urlFound := con.dispatcher.Middleware("Url")
	logger := webfw.GetLogger(c)
	cfg := readeef.GetConfig(c)

	rnd := renderer.NewRenderer(con.dispatcher.Config.Renderer.Dir, "raw.tmpl")
	rnd.Delims("{%", "%}")

	if cfg.Logger.Level == "debug" {
		rnd.SkipCache(true)
	}

	if i18nFound {
		if i18n, ok := i18nmw.(middleware.I18N); ok {
			rnd.Funcs(i18n.TemplateFuncMap())
		}
	} else {
		logger.Infoln("I18N middleware not found")
	}

	if urlFound {
		if url, ok := urlmw.(middleware.Url); ok {
			rnd.Funcs(url.TemplateFuncMap(c))
		}
	} else {
		logger.Infoln("Url middleware not found")
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := webfw.GetParams(c, r)

		if r.Method != "HEAD" {
			err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern, "config": cfg},
				c.GetAll(r), "components/"+params["name"]+".tmpl")

			if err != nil {
				webfw.GetLogger(c).Print(err)
			}
		}
	})
}
开发者ID:urandom,项目名称:readeef,代码行数:42,代码来源:component.go


示例19: GetSession

// GetSession returns the current session from the context,
// if the Session middleware is in use.
func GetSession(c context.Context, r *http.Request) context.Session {
	if val, ok := c.Get(r, context.BaseCtxKey("session")); ok {
		return val.(context.Session)
	}

	conf := GetConfig(c)
	var abspath string

	if filepath.IsAbs(conf.Session.Dir) {
		abspath = conf.Session.Dir
	} else {
		var err error
		abspath, err = filepath.Abs(path.Join(filepath.Dir(os.Args[0]), conf.Session.Dir))

		if err != nil {
			abspath = os.TempDir()
		}
	}

	sess := context.NewSession([]byte(conf.Session.Secret), []byte(conf.Session.Cipher), abspath)
	sess.SetName(util.UUID())
	return sess
}
开发者ID:urandom,项目名称:webfw,代码行数:25,代码来源:context_util.go


示例20: GetFallbackLanguage

// GetFallbackLanguage tries to obtain a requested language via the session,
// or the Accept-Language request header, or the LANG or LC_MESSAGES
// environment variables
func GetFallbackLanguage(c context.Context, r *http.Request, fallback ...string) string {
	if val, ok := c.Get(r, context.BaseCtxKey("session")); ok {
		sess := val.(context.Session)

		if language, ok := sess.Get("language"); ok {
			return language.(string)
		}
	}

	langs := lng.Parse(r.Header.Get("Accept-Language"))

	if len(langs) > 0 {
		return langs[0].String()
	}

	language := os.Getenv("LANG")

	if language == "" {
		language = os.Getenv("LC_MESSAGES")
		language = localeRegexp.ReplaceAllLiteralString(language, "")
	}

	if language == "" {
		if len(fallback) > 0 {
			language = fallback[0]
		} else {
			language = "en"
		}
	} else {
		langs := lng.Parse(language)
		if len(langs) > 0 {
			return langs[0].String()
		}
	}

	return language
}
开发者ID:urandom,项目名称:webfw,代码行数:40,代码来源:context_util.go



注:本文中的github.com/urandom/webfw/context.Context类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cli.NewApp函数代码示例发布时间:2022-05-28
下一篇:
Golang context.BaseCtxKey函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap