Documentation
¶
Index ¶
- Constants
- func ContextWithUser(ctx context.Context, user User) context.Context
- func TypeFromString(raw string, t interface{}) error
- func TypeToString(t interface{}) (string, error)
- type App
- func (app App) Authenticate(destination string) mux.MiddlewareFunc
- func (app App) Cors() mux.MiddlewareFunc
- func (app App) Logging() mux.MiddlewareFunc
- func (app App) NewTemplatingEngine() TemplatingEngine
- func (app App) Recoverer(destination string) mux.MiddlewareFunc
- func (app App) RedirectIfAuthenticated(destination string) mux.MiddlewareFunc
- func (app App) ServeFiles(notFound http.Handler) http.Handler
- func (app App) StoreUserToContext() mux.MiddlewareFunc
- type FlashMessage
- type InMemoryUser
- type Locale
- type Session
- func (s Session) Delete(w http.ResponseWriter, r *http.Request, key string) error
- func (s Session) Destroy(w http.ResponseWriter, r *http.Request) error
- func (s Session) Flash(w http.ResponseWriter, r *http.Request, flashMessage FlashMessage) error
- func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []FlashMessage
- func (s Session) Get(r *http.Request, key string) (string, error)
- func (s Session) GetUserID(r *http.Request) (string, error)
- func (s Session) GetUserLocale(r *http.Request) string
- func (s Session) IsGuest(r *http.Request) bool
- func (s Session) Put(w http.ResponseWriter, r *http.Request, key string, value string) error
- func (s Session) SetUserID(w http.ResponseWriter, r *http.Request, id string) error
- func (s Session) SetUserLocale(w http.ResponseWriter, r *http.Request, id string) error
- type TemplatingEngine
- type User
- type UserRepository
- type ViewHelperFunc
Constants ¶
const ContentTypeHTML = "text/html; charset=utf-8"
ContentTypeHTML is a standard Content-Type header for HTML response.
const UserIDKey = "_user_id"
UserIDKey is used as session key to store the current's user unique ID.
const UserLocaleKey = "_user_locale"
UserLocaleKey is used as session key for the current's user locale settings.
Variables ¶
This section is empty.
Functions ¶
func ContextWithUser ¶
ContextWithUser returns a context with a User value inside
func TypeFromString ¶
TypeFromString converts JSON-encoded value into the type t.
func TypeToString ¶
TypeToString converts any type t to JSON-encoded string value. This is used because app's session can only hold basic string values.
Types ¶
type App ¶
type App struct {
Logger *log.Logger
Router *mux.Router
Static fs.FS
StaticPrefix string
ViewFiles fs.FS
ViewHelpers []ViewHelperFunc
Session *Session
UserRepository UserRepository
Locale Locale
}
App is the main app dependency store. This is where all the app's dependencies are configured.
func (App) Authenticate ¶
func (app App) Authenticate(destination string) mux.MiddlewareFunc
Authenticate middleware will redirect all guests to the destination. This is used to guard user-only routes and to force guests to login.
func (App) Cors ¶
func (app App) Cors() mux.MiddlewareFunc
Cors middleware will add CORS headers to every request.
func (App) Logging ¶
func (app App) Logging() mux.MiddlewareFunc
Logging middleware is used to log every requests to the app's Logger.
func (App) NewTemplatingEngine ¶
func (app App) NewTemplatingEngine() TemplatingEngine
NewTemplatingEngine is a constructor that returns a native HTML templating engine.
func (App) Recoverer ¶
func (app App) Recoverer(destination string) mux.MiddlewareFunc
Recoverer middleware is used to recover the app from panics, to log the incident, and to redirect user to the error page.
func (App) RedirectIfAuthenticated ¶
func (app App) RedirectIfAuthenticated(destination string) mux.MiddlewareFunc
RedirectIfAuthenticated middleware will redirect authenticated users to the destination. This is used to deflect logged in users from guest-only pages like login or register page back to the app.
func (App) ServeFiles ¶
ServeFiles returns a Handler that serves a HTTP requests with the file contents.
You can pass 404 Handler to be served when the file is not found.
It will not list directories, instead, the 404 Handler will be used.
func (App) StoreUserToContext ¶
func (app App) StoreUserToContext() mux.MiddlewareFunc
StoreUserToContext middleware will retrieve the current logged in user from the UserRepository and store it into the request context for further use.
type FlashMessage ¶
FlashMessage represents a flash message.
func NewFlash ¶
func NewFlash(message string) FlashMessage
NewFlash is a constructor for new flash message.
type InMemoryUser ¶
InMemoryUser is a sample User entity implementation with some sample fields provided like Name and Email.
Password is stored as plain-text for simplicity. In real life, you should probably use crypto/bcrypt library and store hashed representation and compare passwords in Authenticate method with bcrypt.CompareHashAndPassword
func (InMemoryUser) GetID ¶
func (u InMemoryUser) GetID() string
GetID will return a unique ID for every specific user.
type Locale ¶
type Locale interface {
T(language string, s string, p ...interface{}) string
TP(language string, s string, n int, p ...interface{}) string
}
Locale is the main i18n interface.
type Session ¶
Session is the main application session store.
func NewSession ¶
NewSession returns a new application session with requested store engine.
func (Session) Flash ¶
func (s Session) Flash(w http.ResponseWriter, r *http.Request, flashMessage FlashMessage) error
Flash will store flash message into the session. These messages are primarily used to inform the user during a subsequesnt request about some status update.
func (Session) Flashes ¶
func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []FlashMessage
Flashes returns all messages from the session and removes them.
func (Session) GetUserLocale ¶
GetUserLocale retrieves the current user's locale string from the session.
func (Session) SetUserLocale ¶
SetUserLocale will store the user's locale string into the session.
type TemplatingEngine ¶
type TemplatingEngine interface {
Render(w http.ResponseWriter, r *http.Request, patterns ...string)
Set(key string, data interface{}) TemplatingEngine
SetError(errorKey, description string) TemplatingEngine
GetErrors() map[string]string
}
TemplatingEngine used for rendering response.
type User ¶
type User interface {
GetID() string
}
The User interface for providing the standard user entity.
type UserRepository ¶
type UserRepository interface {
GetUserByID(id string) (User, error)
Authenticate(w http.ResponseWriter, r *http.Request) (User, error)
}
The UserRepository will provide a way to retrieve and authenticate users.
The user repository is database-agnostic. You can keep your users in MySQL, MongoDB, LDAP or in a simple JSON file. By implementing this interface, you are providing the way for the app to authenticate, and retrieve the specific user entity by using the unique user ID.
func NewInMemoryUserRepository ¶
func NewInMemoryUserRepository(users []InMemoryUser) UserRepository
NewInMemoryUserRepository returns a sample in-memory UserRepository implementation which can be used in tests or sample apps.
type ViewHelperFunc ¶
type ViewHelperFunc func(w http.ResponseWriter, r *http.Request) (string, interface{})
ViewHelperFunc is a view helper that can be used in any template file.