diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-09-03 14:26:40 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-09-03 14:26:40 -0700 |
commit | eee1f3190355224eef63a4dcdef8c36eb3ca3738 (patch) | |
tree | 009b7ca67b96d9be473d8ddf2c8c95f22a6749d1 /http | |
parent | 88e81d4d800ff6433518522954197d75203a25c2 (diff) |
Refactor HTTP context handling
Diffstat (limited to 'http')
-rw-r--r-- | http/context/context.go | 122 | ||||
-rw-r--r-- | http/request/context.go | 128 | ||||
-rw-r--r-- | http/request/doc.go | 10 |
3 files changed, 138 insertions, 122 deletions
diff --git a/http/context/context.go b/http/context/context.go deleted file mode 100644 index 7a434b5..0000000 --- a/http/context/context.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright 2018 Frédéric Guillot. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. - -package context // import "miniflux.app/http/context" - -import ( - "net/http" - - "miniflux.app/middleware" -) - -// Context contains helper functions related to the current request. -type Context struct { - request *http.Request -} - -// IsAdminUser checks if the logged user is administrator. -func (c *Context) IsAdminUser() bool { - return c.getContextBoolValue(middleware.IsAdminUserContextKey) -} - -// IsAuthenticated returns a boolean if the user is authenticated. -func (c *Context) IsAuthenticated() bool { - return c.getContextBoolValue(middleware.IsAuthenticatedContextKey) -} - -// UserID returns the UserID of the logged user. -func (c *Context) UserID() int64 { - return c.getContextIntValue(middleware.UserIDContextKey) -} - -// UserTimezone returns the timezone used by the logged user. -func (c *Context) UserTimezone() string { - value := c.getContextStringValue(middleware.UserTimezoneContextKey) - if value == "" { - value = "UTC" - } - return value -} - -// UserLanguage get the locale used by the current logged user. -func (c *Context) UserLanguage() string { - language := c.getContextStringValue(middleware.UserLanguageContextKey) - if language == "" { - language = "en_US" - } - return language -} - -// UserTheme get the theme used by the current logged user. -func (c *Context) UserTheme() string { - theme := c.getContextStringValue(middleware.UserThemeContextKey) - if theme == "" { - theme = "default" - } - return theme -} - -// CSRF returns the current CSRF token. -func (c *Context) CSRF() string { - return c.getContextStringValue(middleware.CSRFContextKey) -} - -// SessionID returns the current session ID. -func (c *Context) SessionID() string { - return c.getContextStringValue(middleware.SessionIDContextKey) -} - -// UserSessionToken returns the current user session token. -func (c *Context) UserSessionToken() string { - return c.getContextStringValue(middleware.UserSessionTokenContextKey) -} - -// OAuth2State returns the current OAuth2 state. -func (c *Context) OAuth2State() string { - return c.getContextStringValue(middleware.OAuth2StateContextKey) -} - -// FlashMessage returns the message message if any. -func (c *Context) FlashMessage() string { - return c.getContextStringValue(middleware.FlashMessageContextKey) -} - -// FlashErrorMessage returns the message error message if any. -func (c *Context) FlashErrorMessage() string { - return c.getContextStringValue(middleware.FlashErrorMessageContextKey) -} - -// PocketRequestToken returns the Pocket Request Token if any. -func (c *Context) PocketRequestToken() string { - return c.getContextStringValue(middleware.PocketRequestTokenContextKey) -} - -func (c *Context) getContextStringValue(key middleware.ContextKey) string { - if v := c.request.Context().Value(key); v != nil { - return v.(string) - } - - return "" -} - -func (c *Context) getContextBoolValue(key middleware.ContextKey) bool { - if v := c.request.Context().Value(key); v != nil { - return v.(bool) - } - - return false -} - -func (c *Context) getContextIntValue(key middleware.ContextKey) int64 { - if v := c.request.Context().Value(key); v != nil { - return v.(int64) - } - - return 0 -} - -// New creates a new Context. -func New(r *http.Request) *Context { - return &Context{r} -} diff --git a/http/request/context.go b/http/request/context.go new file mode 100644 index 0000000..78014e4 --- /dev/null +++ b/http/request/context.go @@ -0,0 +1,128 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +package request // import "miniflux.app/http/request" + +import "net/http" + +// ContextKey represents a context key. +type ContextKey int + +// List of context keys. +const ( + UserIDContextKey ContextKey = iota + UserTimezoneContextKey + IsAdminUserContextKey + IsAuthenticatedContextKey + UserSessionTokenContextKey + UserLanguageContextKey + UserThemeContextKey + SessionIDContextKey + CSRFContextKey + OAuth2StateContextKey + FlashMessageContextKey + FlashErrorMessageContextKey + PocketRequestTokenContextKey +) + +// IsAdminUser checks if the logged user is administrator. +func IsAdminUser(r *http.Request) bool { + return getContextBoolValue(r, IsAdminUserContextKey) +} + +// IsAuthenticated returns a boolean if the user is authenticated. +func IsAuthenticated(r *http.Request) bool { + return getContextBoolValue(r, IsAuthenticatedContextKey) +} + +// UserID returns the UserID of the logged user. +func UserID(r *http.Request) int64 { + return getContextInt64Value(r, UserIDContextKey) +} + +// UserTimezone returns the timezone used by the logged user. +func UserTimezone(r *http.Request) string { + value := getContextStringValue(r, UserTimezoneContextKey) + if value == "" { + value = "UTC" + } + return value +} + +// UserLanguage get the locale used by the current logged user. +func UserLanguage(r *http.Request) string { + language := getContextStringValue(r, UserLanguageContextKey) + if language == "" { + language = "en_US" + } + return language +} + +// UserTheme get the theme used by the current logged user. +func UserTheme(r *http.Request) string { + theme := getContextStringValue(r, UserThemeContextKey) + if theme == "" { + theme = "default" + } + return theme +} + +// CSRF returns the current CSRF token. +func CSRF(r *http.Request) string { + return getContextStringValue(r, CSRFContextKey) +} + +// SessionID returns the current session ID. +func SessionID(r *http.Request) string { + return getContextStringValue(r, SessionIDContextKey) +} + +// UserSessionToken returns the current user session token. +func UserSessionToken(r *http.Request) string { + return getContextStringValue(r, UserSessionTokenContextKey) +} + +// OAuth2State returns the current OAuth2 state. +func OAuth2State(r *http.Request) string { + return getContextStringValue(r, OAuth2StateContextKey) +} + +// FlashMessage returns the message message if any. +func FlashMessage(r *http.Request) string { + return getContextStringValue(r, FlashMessageContextKey) +} + +// FlashErrorMessage returns the message error message if any. +func FlashErrorMessage(r *http.Request) string { + return getContextStringValue(r, FlashErrorMessageContextKey) +} + +// PocketRequestToken returns the Pocket Request Token if any. +func PocketRequestToken(r *http.Request) string { + return getContextStringValue(r, PocketRequestTokenContextKey) +} + +func getContextStringValue(r *http.Request, key ContextKey) string { + if v := r.Context().Value(key); v != nil { + return v.(string) + } + + return "" +} + +func getContextBoolValue(r *http.Request, key ContextKey) bool { + if v := r.Context().Value(key); v != nil { + return v.(bool) + } + + return false +} + +func getContextInt64Value(r *http.Request, key ContextKey) int64 { + if v := r.Context().Value(key); v != nil { + return v.(int64) + } + + return 0 +} diff --git a/http/request/doc.go b/http/request/doc.go new file mode 100644 index 0000000..1fcaaa6 --- /dev/null +++ b/http/request/doc.go @@ -0,0 +1,10 @@ +// Copyright 2018 Frédéric Guillot. All rights reserved. +// Use of this source code is governed by the MIT license +// that can be found in the LICENSE file. + +/* + +Package request holds helper functions regarding the HTTP request. + +*/ +package request // import "miniflux.app/http/request" |