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 }
// 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) }
// 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{} }
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 }
// 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 }
// 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 }
// 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 }
// 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 "" }
// 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 "" }