本文整理汇总了Golang中github.com/urandom/webfw/context.Context.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Get方法的具体用法?Golang Context.Get怎么用?Golang Context.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/urandom/webfw/context.Context
的用法示例。
在下文中一共展示了Context.Get方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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)
}
示例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: 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
}
示例5: 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
}
示例6: 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
}
示例7: 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
}
示例8: GetMultiPatternIdentifier
// GetMultiPatternIdentifier returns the identifier for the current
// multi-pattern route.
func GetMultiPatternIdentifier(c context.Context, r *http.Request) string {
if val, ok := c.Get(r, context.BaseCtxKey("multi-pattern-identifier")); ok {
return val.(string)
}
return ""
}
示例9: GetNamedForward
// GetNamedForward returns a name, used by the dispatcher to lookup a route to
// forward to.
func GetNamedForward(c context.Context, r *http.Request) string {
if val, ok := c.Get(r, context.BaseCtxKey("named-forward")); ok {
return val.(string)
}
return ""
}