Documentation
¶
Overview ¶
Package admin is a platform module: the admin panel of the project, running inside the same binary as the application.
It brings the parts every project needs anyway — sign in, roles, audit log, business settings pages — and an extension point for the project's own pages.
Index ¶
- Constants
- func AddPage(app *platform.App, p Page)
- func Auth(app *platform.App) *adminx.Auth
- func CSRFToken(r *http.Request) string
- func EnsureSchema(ctx context.Context, pool *pgxpool.Pool) error
- func NewAuditRepo(pool *pgxpool.Pool) adminx.AuditRepo
- func NewServer(cfg Config, service string, auth *adminx.Auth, users adminx.UserRepo, ...) http.Handler
- func NewSessionRepo(pool *pgxpool.Pool) adminx.SessionRepo
- func NewUserRepo(pool *pgxpool.Pool) adminx.UserRepo
- func Record(r *http.Request, action, target, details string)
- func Render(w http.ResponseWriter, r *http.Request, title string, ...)
- func SetTheme(app *platform.App, t Theme)
- func UserFrom(ctx context.Context) (adminx.User, bool)
- type Config
- type Module
- func (m *Module) Addr() string
- func (m *Module) Health(context.Context) error
- func (m *Module) Init(ctx context.Context, app *platform.App) error
- func (m *Module) Name() string
- func (m *Module) Start(context.Context) error
- func (m *Module) Stop(ctx context.Context) error
- func (m *Module) StopPhases() []platform.StopPhase
- type Page
- type Registry
- type Theme
Constants ¶
const CSRFField = "csrf"
CSRFField is the form field every POST form of the panel carries.
const DefaultAddr = "127.0.0.1:8081"
DefaultAddr is the address of the admin panel. It is a separate port so the panel can be kept off the public network.
Variables ¶
This section is empty.
Functions ¶
func Auth ¶
Auth returns the sign in service from the container: project pages use it to know who is signed in.
func CSRFToken ¶ added in v0.5.0
CSRFToken returns the value of the CSRF field for a form on a project page.
func EnsureSchema ¶
EnsureSchema brings the tables of the admin panel to the current version.
func NewAuditRepo ¶
NewAuditRepo returns the audit log stored in PostgreSQL.
func NewServer ¶
func NewServer(cfg Config, service string, auth *adminx.Auth, users adminx.UserRepo, aud adminx.AuditRepo, settings *settingsx.Store, pages []Page, log *slog.Logger) http.Handler
NewServer builds the panel handler. Everything it needs is passed in, so tests run it on in memory storage.
func NewSessionRepo ¶
func NewSessionRepo(pool *pgxpool.Pool) adminx.SessionRepo
NewSessionRepo returns the sessions stored in PostgreSQL.
func NewUserRepo ¶
NewUserRepo returns the accounts stored in PostgreSQL.
func Record ¶ added in v0.5.0
Record writes an action of a project page to the audit log: who, what, on what, and what changed, the way the panel records its own actions.
func Render ¶ added in v0.5.0
Render draws a project page inside the panel: the menu, the account, the banners of ?ok= and ?error=, and the body the project renders.
Types ¶
type Config ¶
type Config struct {
Addr string // listen address
SessionTTL time.Duration // how long a session lives
// Insecure allows the session cookie over plain HTTP. It is meant for local
// development: in production the panel is behind TLS and the cookie is Secure.
Insecure bool
// BootstrapEmail and BootstrapPassword create the first account when the table is
// empty. Without them a fresh installation has nobody to log in as.
BootstrapEmail string
BootstrapPassword string
// Language of the panel: en or ru.
Language string
// Theme is the look of the panel. Load leaves it empty: the project sets it in code
// with SetTheme.
Theme Theme
}
Config holds module settings. Load fills it from the environment; the platform generator writes the Load call into the project's config.gen.go.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module implements platform.Module.
func (*Module) Addr ¶
Addr is the address the panel actually listens on. With port 0 in the settings it is the port the system picked, which is what tests use.
func (*Module) Init ¶
Init prepares the storage and the sign in, and puts the page registry into the container so that wireDomain can add the project pages.
func (*Module) StopPhases ¶ added in v0.9.0
StopPhases stops the panel together with the other HTTP servers in a phased shutdown.
type Page ¶
type Page struct {
Title string // name in the menu
Path string // path inside the panel, "/orders"
Roles []string // who may open it; empty means any signed in user
Handler http.Handler // the page itself
}
Page is a project page in the admin panel. It is the extension point: the platform gives the layout, the sign in and the roles, the project gives the content.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the project pages. It lives in the container, and AddPage fills it from wireDomain, before the modules start.
type Theme ¶ added in v0.7.0
type Theme struct {
Brand string // the name in the menu and on the sign in card; the service name when empty
Caption string // a small word next to the name, "admin"
Background string // the page behind the content
Surface string // cards, tables, inputs
Text string // the text of the content
Muted string // secondary text
Line string // borders and separators of the content
Accent string // links, focus and checkboxes
AccentHover string // a hovered link colour of buttons that have no Button colour
Menu string // background of the menu
MenuText string // its items
MenuMuted string // the account and the caption in the menu
MenuHover string // background of a hovered item
MenuLine string // the separators in the menu
Active string // background of the current item
ActiveText string // text of the current item
BrandColor string // the name in the menu
Button string // background of the primary buttons
ButtonHover string // and when hovered
ButtonText string // their text
Radius string // cards, tables and blocks, "14px"
ControlRadius string // buttons, inputs and menu items, "10px"
// PagesFirst puts the project pages at the top of the menu and the panel's own
// pages — overview, settings, accounts, audit — under a separator after them: in a
// project panel the project pages are the everyday ones.
PagesFirst bool
// CSS holds the project's own rules for its pages, added after the panel's.
CSS string
}
Theme is the look of the panel in the colours of the project: the name and a caption in the menu, the palette, the radii and the project's own rules for its pages.
The zero Theme is the panel as it is: every empty field keeps the panel's own value, and a project without a theme gets exactly the same pages as before themes existed. The project sets it with SetTheme in wireDomain, next to AddPage: a theme is part of the code, not of the environment, so Load does not read it.
A theme with any colour pins the panel to the light scheme: a brand palette is drawn for one background, and the dark one of the panel would mix with it.
Every value is a CSS value — "#343A4D", "rgba(247, 147, 30, .18)", "14px". The rules go into the stylesheet of the page, the same <style> block the panel already has, so a theme needs no extra request and no inline style attributes.