Documentation ¶
Index ¶
- func ApplyLayout(layout string) func(http.Handler) http.Handler
- func Format(dt time.Time, layout string, locale string) string
- func WithLayout(r *http.Request, layout string) *http.Request
- type Core
- type DB
- type Form
- func (f *Form) CustomError(field, msg string)
- func (f *Form) Error(field string) string
- func (f *Form) IsDate(fields ...string)
- func (f *Form) IsEmail(fields ...string)
- func (f *Form) IsInteger(fields ...string)
- func (f *Form) IsTime(fields ...string)
- func (f *Form) MatchesPattern(field string, pattern *regexp.Regexp)
- func (f *Form) MaxLength(field string, d int)
- func (f *Form) MinLength(field string, d int)
- func (f *Form) PermittedValues(field string, opts ...string)
- func (f *Form) Required(fields ...string)
- func (f *Form) Valid() bool
- type Option
- func WithCSP(csp map[string]string) Option
- func WithDB(dsn string) Option
- func WithDebug(debug bool) Option
- func WithFuncs(funcs template.FuncMap) Option
- func WithGlobals(fn func(*http.Request) interface{}) Option
- func WithLogger(logger *log.Logger) Option
- func WithReqFuncs(funcs ReqFuncMap) Option
- func WithSession(key string) Option
- func WithTranslator(locale string) Option
- type ReqFuncMap
- type Translator
- type Views
- func (views *Views) ClientError(w http.ResponseWriter, status int)
- func (views *Views) Funcs(funcs template.FuncMap)
- func (views *Views) Parse(fsys fs.FS) error
- func (views *Views) Render(w http.ResponseWriter, r *http.Request, status int, name string, ...)
- func (views *Views) ReqFuncs(reqFuncs ReqFuncMap)
- func (views *Views) ServerError(w http.ResponseWriter, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyLayout ¶
ApplyLayout is a middleware that applies a specific layout for the rendering of the view. It returns a function which has the correct signature to be used with alice, but it can also be used without.
Types ¶
type Core ¶
type Core struct { Logger *log.Logger DB *DB Views *Views Session *sessions.Session // contains filtered or unexported fields }
Core holds the core logic to configure and run a simple web app. It is meant to be embedded in a parent web app structure.
func NewCore ¶
NewCore creates a core with sane defaults. Options can be used for specific configurations.
func (*Core) DynChain ¶
DynChain returns a chain of middleware that can be applied to all dynamic routes. It injects a CSRF cookie and enable sessions.
func (*Core) FileServer ¶
FileServer returns a handler for serving filesystem files. It enforces http cache by appending hashes to filenames. A hashName function is defined in templates to gather the hashed filename of a file.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents the database connection.
type Form ¶
Form validates form data against a particular set of rules. If an error occurs, it will store an error message associated with the field.
func (*Form) CustomError ¶
CustomError adds a specific error for a field.
func (*Form) MatchesPattern ¶
MatchesPattern checks that a specific field in the form matches a regular expression. If the check fails, then add the appropriate message to the form errors.
func (*Form) MaxLength ¶
MaxLength checks that a specific field in the form contains a maximum number of characters. If the check fails, then add the appropriate message to the form errors.
func (*Form) MinLength ¶
MinLength checks that a specific field in the form contains a minimum number of characters. If the check fails, then add the appropriate message to the form errors.
func (*Form) PermittedValues ¶
PermittedValues checks that a specific field in the form matches one of a set of specific permitted values. If the check fails, then add the appropriate message to the form errors.
type Option ¶
Option configures a core.
func WithCSP ¶
WithCSP is an option to set the csp rules that will be set on http responses. By default, only default-src : 'self' is defined.
func WithDebug ¶
WithDebug is an option to spit the server errors directly in http responses, instead of a generic 'Internal Server Error' message.
func WithFuncs ¶
WithFuncs is an option to configure default functions that will be injected into views.
func WithGlobals ¶
WithGlobals is an option that allows to define a function that is called at each rendering to inject data that can be retrieved using the "globals" helper template function.
func WithLogger ¶
WithLogger is an option to set the application logger.
func WithReqFuncs ¶
func WithReqFuncs(funcs ReqFuncMap) Option
WithReqFuncs is an option similar to WithFuncs, but with functions that are request-aware.
func WithSession ¶
WithSession is an option to enable cookie sessions. The key parameter is the secret you want to use to authenticate and encrypt sessions cookies, and should be 32 bytes long.
func WithTranslator ¶
WithTranslator is an option to enable and configure the translator. If the locale paramater value is "auto", the locale will be retrieved first from the "lang" cookie, then from the "Accept-Language" request header. If it cannot retrieve it, messages will be returned untranslated.
type ReqFuncMap ¶
ReqFuncMap is a dynamic version of template.FuncMap that is request-aware.
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator allows to translate a message from english to a predefined set of locales parsed from csv files. Il also deals with date and time formats.
func (*Translator) Parse ¶
func (tr *Translator) Parse(fsys fs.FS) error
Parse parses all the csv files in the translations folder and build dictionnary maps that will serve as databases for translations. The name of csv file should be a string representing a locale (e.g. en_US). When % is used in a csv translation, it will serve as a placeholder and its value won’t be altered during the translation.
func (*Translator) ReqLocale ¶
func (tr *Translator) ReqLocale(r *http.Request) string
ReqLocale tries to return the locale from the request. It tries to retrieve it first using the "lang" cookie and otherwise using the "Accept-Language" request header. If the locale is not recognized or not supported, it will return the default locale (en_US).
type Views ¶
type Views struct { Logger *log.Logger Debug bool // to display errors in http responses // contains filtered or unexported fields }
Views is an engine that will render Views from templates.
func (*Views) ClientError ¶
func (views *Views) ClientError(w http.ResponseWriter, status int)
ClientError sends a specific status code and corresponding description to the user. This should be used to send responses when there's a problem with the request that the user sent.
func (*Views) Funcs ¶
Funcs adds the elements of the argument map to the list of functions to inject into templates.
func (*Views) Parse ¶
Parse walks a filesystem from the root folder to discover and parse html files into views. Files starting with an underscore are partial views. Files in the layouts folder not starting with underscore are layouts. The rest of html files are full page views. The funcs parameter is a list of functions that is attached to views.
Views, layouts and partials will be referred to with their path, but without the root folder, and without the file extension.
Layouts will be referred to without the layouts folder neither.
Partials files are named with a leading underscore to distinguish them from regular views, but will be referred to without the underscore.
func (*Views) Render ¶
func (views *Views) Render(w http.ResponseWriter, r *http.Request, status int, name string, data interface{})
Render renders a given view or partial.
For page views, the layout can be set using the WithLayout function or using the ApplyLayout middleware. If no layout is defined, the "base" layout will be chosen. Partial views are rendered without any layout.
func (*Views) ReqFuncs ¶
func (views *Views) ReqFuncs(reqFuncs ReqFuncMap)
ReqFuncs adds the elements of the argument map to the list of request-aware functions to inject into templates.
func (*Views) ServerError ¶
func (views *Views) ServerError(w http.ResponseWriter, err error)
ServerError writes an error message and stack trace to the logger, then sends a generic 500 Internal Server Error response to the user.