admin

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

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

View Source
const CSRFField = "csrf"

CSRFField is the form field every POST form of the panel carries.

View Source
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 AddPage

func AddPage(app *platform.App, p Page)

AddPage registers a project page in the admin panel.

func Auth

func Auth(app *platform.App) *adminx.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

func CSRFToken(r *http.Request) string

CSRFToken returns the value of the CSRF field for a form on a project page.

func EnsureSchema

func EnsureSchema(ctx context.Context, pool *pgxpool.Pool) error

EnsureSchema brings the tables of the admin panel to the current version.

func NewAuditRepo

func NewAuditRepo(pool *pgxpool.Pool) adminx.AuditRepo

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

func NewUserRepo(pool *pgxpool.Pool) adminx.UserRepo

NewUserRepo returns the accounts stored in PostgreSQL.

func Record added in v0.5.0

func Record(r *http.Request, action, target, details string)

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

func Render(w http.ResponseWriter, r *http.Request, title string, body func(w io.Writer) error)

Render draws a project page inside the panel: the menu, the account, the banners of ?ok= and ?error=, and the body the project renders.

func SetTheme added in v0.7.0

func SetTheme(app *platform.App, t Theme)

SetTheme gives the admin panel the look of the project: call it in wireDomain, next to AddPage.

func UserFrom

func UserFrom(ctx context.Context) (adminx.User, bool)

UserFrom returns the signed in user of the request: project pages use it to know who is acting and what they may do.

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.

func Load

func Load(l *confx.Loader) Config

Load reads the module settings from environment variables.

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module implements platform.Module.

func New

func New(cfg Config) *Module

New creates the module from ready settings.

func (*Module) Addr

func (m *Module) Addr() string

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) Health

func (m *Module) Health(context.Context) error

Health reports whether the panel is serving.

func (*Module) Init

func (m *Module) Init(ctx context.Context, app *platform.App) error

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) Name

func (m *Module) Name() string

func (*Module) Start

func (m *Module) Start(context.Context) error

Start serves the panel and begins removing expired sessions.

func (*Module) Stop

func (m *Module) Stop(ctx context.Context) error

Stop shuts the panel down.

func (*Module) StopPhases added in v0.9.0

func (m *Module) StopPhases() []platform.StopPhase

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.

func Pages

func Pages(app *platform.App) *Registry

Pages returns the page registry from the container.

func (*Registry) Add

func (r *Registry) Add(p Page)

Add registers a page.

func (*Registry) Pages

func (r *Registry) Pages() []Page

Pages returns the registered pages.

func (*Registry) SetTheme added in v0.7.0

func (r *Registry) SetTheme(t Theme)

SetTheme sets the look of the panel. A value that is not a plain CSS value panics, like a bad page path: both are mistakes in the code, found on the first 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.

func (Theme) Validate added in v0.7.0

func (t Theme) Validate() error

Validate reports a value that could break out of its declaration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL