Documentation
¶
Overview ¶
Package handler holds the HTTP handlers; they contain no business logic and call services only.
Index ¶
- func NewDocs(fsys fs.FS) http.Handler
- type Auth
- func (h *Auth) AddUser(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Audit(w http.ResponseWriter, r *http.Request)
- func (h *Auth) CreateToken(w http.ResponseWriter, r *http.Request)
- func (h *Auth) DeleteUser(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Login(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Logout(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Me(w http.ResponseWriter, r *http.Request)
- func (h *Auth) RevokeToken(w http.ResponseWriter, r *http.Request)
- func (h *Auth) SetPassword(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Setup(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Tokens(w http.ResponseWriter, r *http.Request)
- func (h *Auth) Users(w http.ResponseWriter, r *http.Request)
- type AuthService
- type Config
- func (h *Config) DeleteObject(w http.ResponseWriter, r *http.Request)
- func (h *Config) Export(w http.ResponseWriter, r *http.Request)
- func (h *Config) Object(w http.ResponseWriter, r *http.Request)
- func (h *Config) Overview(w http.ResponseWriter, r *http.Request)
- func (h *Config) Probe(w http.ResponseWriter, r *http.Request)
- func (h *Config) Profiles(w http.ResponseWriter, _ *http.Request)
- func (h *Config) PutObject(w http.ResponseWriter, r *http.Request)
- func (h *Config) PutObjects(w http.ResponseWriter, r *http.Request)
- func (h *Config) PutSettings(w http.ResponseWriter, r *http.Request)
- func (h *Config) Settings(w http.ResponseWriter, r *http.Request)
- type ConfigService
- type Devices
- type DevicesService
- type Energy
- type EnergyService
- type Gauge
- type GaugeService
- type Health
- type HealthService
- type ProfileLister
- type Report
- type ReportInfo
- type ReportService
- type Site
- type SiteService
- type Static
- type Version
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth serves the session, setup and administration endpoints (spec 9.1).
func NewAuth ¶
func NewAuth(svc AuthService, publicRead bool, clock util.Clock, loc *time.Location, log zerolog.Logger, ) (*Auth, error)
NewAuth builds the handler; publicRead is auth.public_read, reported by /auth/me.
func (*Auth) AddUser ¶
func (h *Auth) AddUser(w http.ResponseWriter, r *http.Request)
AddUser serves POST /api/v1/users.
func (*Auth) Audit ¶
func (h *Auth) Audit(w http.ResponseWriter, r *http.Request)
Audit serves GET /api/v1/audit?from=&to= (local dates or RFC 3339, to inclusive as a date); default the last 30 days.
func (*Auth) CreateToken ¶
func (h *Auth) CreateToken(w http.ResponseWriter, r *http.Request)
CreateToken serves POST /api/v1/tokens; the secret is returned only here.
func (*Auth) DeleteUser ¶
func (h *Auth) DeleteUser(w http.ResponseWriter, r *http.Request)
DeleteUser serves DELETE /api/v1/users/{id}.
func (*Auth) Login ¶
func (h *Auth) Login(w http.ResponseWriter, r *http.Request)
Login serves POST /api/v1/auth/login and sets the session cookie.
func (*Auth) Logout ¶
func (h *Auth) Logout(w http.ResponseWriter, r *http.Request)
Logout serves POST /api/v1/auth/logout: deletes the session and the cookie.
func (*Auth) Me ¶
func (h *Auth) Me(w http.ResponseWriter, r *http.Request)
Me serves GET /api/v1/auth/me: who is logged in, whether setup is pending and whether reading is public; open so the UI can decide between login, setup and dashboard.
func (*Auth) RevokeToken ¶
func (h *Auth) RevokeToken(w http.ResponseWriter, r *http.Request)
RevokeToken serves DELETE /api/v1/tokens/{id}.
func (*Auth) SetPassword ¶
func (h *Auth) SetPassword(w http.ResponseWriter, r *http.Request)
SetPassword serves PUT /api/v1/users/{id}/password; the user's sessions end.
func (*Auth) Setup ¶
func (h *Auth) Setup(w http.ResponseWriter, r *http.Request)
Setup serves POST /api/v1/setup: creates the first admin and logs them in.
type AuthService ¶
type AuthService interface {
SetupRequired(ctx context.Context) (bool, error)
Setup(ctx context.Context, r auth.SetupRequest) (auth.Login, error)
Login(ctx context.Context, name, pw, ip string) (auth.Login, error)
Logout(ctx context.Context, p auth.Principal) error
Users(ctx context.Context) ([]auth.UserInfo, error)
AddUser(ctx context.Context, by auth.Principal, name, pw, lang string) (auth.UserInfo, error)
DeleteUser(ctx context.Context, by auth.Principal, id int64) error
SetPassword(ctx context.Context, by auth.Principal, id int64, pw string) error
Tokens(ctx context.Context) ([]auth.TokenInfo, error)
CreateToken(ctx context.Context, by auth.Principal, name string, expires time.Duration,
) (auth.NewToken, error)
RevokeToken(ctx context.Context, by auth.Principal, id string) error
Audit(ctx context.Context, from, to time.Time) ([]auth.AuditInfo, error)
}
AuthService is what the auth and admin handlers need.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config serves /api/v1/config/* and /api/v1/profiles (admin only).
func NewConfig ¶
func NewConfig(svc ConfigService, profiles ProfileLister, log zerolog.Logger) *Config
NewConfig builds the handlers.
func (*Config) DeleteObject ¶
func (h *Config) DeleteObject(w http.ResponseWriter, r *http.Request)
DeleteObject serves DELETE /api/v1/config/{kind}/{id}.
func (*Config) Export ¶
func (h *Config) Export(w http.ResponseWriter, r *http.Request)
Export serves GET /api/v1/config/export: the configured layers as YAML, secrets masked.
func (*Config) Object ¶
func (h *Config) Object(w http.ResponseWriter, r *http.Request)
Object serves GET /api/v1/config/{kind}/{id}.
func (*Config) Overview ¶
func (h *Config) Overview(w http.ResponseWriter, r *http.Request)
Overview serves GET /api/v1/config: every object and setting.
func (*Config) Probe ¶
func (h *Config) Probe(w http.ResponseWriter, r *http.Request)
Probe serves POST /api/v1/config/probe: reads the device once with the draft applied. An unreachable device is a 200 with error set; an invalid draft is a 422.
func (*Config) Profiles ¶
func (h *Config) Profiles(w http.ResponseWriter, _ *http.Request)
Profiles serves GET /api/v1/profiles.
func (*Config) PutObject ¶
func (h *Config) PutObject(w http.ResponseWriter, r *http.Request)
PutObject serves PUT /api/v1/config/{kind}/{id}; the body is the object in file form.
func (*Config) PutObjects ¶
func (h *Config) PutObjects(w http.ResponseWriter, r *http.Request)
PutObjects serves PUT /api/v1/config: {objects: [{kind, id, body}]} as one revision; a null body deletes the object. Answers like GET /api/v1/config.
func (*Config) PutSettings ¶
func (h *Config) PutSettings(w http.ResponseWriter, r *http.Request)
PutSettings serves PUT /api/v1/config/settings: {key: value}, null resets a key.
type ConfigService ¶
type ConfigService interface {
Overview(ctx context.Context) (configstore.Overview, error)
Object(ctx context.Context, kind, id string) (configstore.ObjectView, error)
PutObject(ctx context.Context, by, kind, id string, body map[string]any,
) (configstore.ObjectView, int64, error)
DeleteObject(ctx context.Context, by, kind, id string) (int64, error)
PutSettings(ctx context.Context, by string, set map[string]any,
) (configstore.Overview, error)
Export(ctx context.Context) ([]byte, error)
PutObjects(ctx context.Context, by string, cs []configstore.Change,
) (configstore.Overview, error)
Probe(ctx context.Context, by string, cs []configstore.Change, device string,
) (service.ProbeResult, error)
}
ConfigService is what the configuration handlers need. Writes validate the whole configuration and apply it without restart (spec 3).
type Devices ¶
type Devices struct {
// contains filtered or unexported fields
}
Devices serves GET /api/v1/devices, /api/v1/devices/{id} and its /status.
func NewDevices ¶
func NewDevices(svc DevicesService, clock util.Clock, loc *time.Location, log zerolog.Logger, ) (*Devices, error)
NewDevices builds the handlers.
func (*Devices) Get ¶
func (h *Devices) Get(w http.ResponseWriter, r *http.Request)
Get writes one device.
func (*Devices) List ¶
func (h *Devices) List(w http.ResponseWriter, _ *http.Request)
List writes every device.
type DevicesService ¶
type DevicesService interface {
List() []service.DeviceInfo
Get(id string) (service.DeviceDetail, error)
Status(ctx context.Context, id string, from, to time.Time,
) ([]service.StatusEventModel, error)
Overview(ctx context.Context, from, to time.Time) ([]service.DeviceStatus, error)
}
DevicesService is what the device handlers need.
type Energy ¶
type Energy struct {
// contains filtered or unexported fields
}
Energy serves GET /api/v1/energy.
type EnergyService ¶
type EnergyService interface {
Series(ctx context.Context, names []string, level string, from, to time.Time,
) (service.EnergyModel, error)
}
EnergyService is what the energy handler needs.
type Gauge ¶
type Gauge struct {
// contains filtered or unexported fields
}
Gauge serves GET /api/v1/gauge.
type GaugeService ¶
type GaugeService interface {
Series(ctx context.Context, names []string, from, to time.Time) (service.GaugeModel, error)
}
GaugeService is what the gauge handler needs.
type Health ¶
type Health struct {
// contains filtered or unexported fields
}
Health serves GET /health.
type HealthService ¶
type HealthService interface {
Report() (service.HealthReport, int)
}
HealthService is what the health handler needs.
type ProfileLister ¶
type ProfileLister interface {
List() []service.ProfileInfo
}
ProfileLister lists the built-in profiles.
type Report ¶
type Report struct {
// contains filtered or unexported fields
}
Report serves GET /api/v1/report (spec 9.1, 12).
func NewReport ¶
func NewReport(svc ReportService, info ReportInfo, clock util.Clock, log zerolog.Logger, ) (*Report, error)
NewReport builds the handler.
type ReportInfo ¶
ReportInfo is what the PDF header shows besides the report.
type ReportService ¶
type ReportService interface {
Build(ctx context.Context, q service.ReportQuery) (report.Report, error)
}
ReportService builds reports.
type Site ¶
type Site struct {
// contains filtered or unexported fields
}
Site serves GET /api/v1/site.
type SiteService ¶
SiteService is what the site handler needs.
type Static ¶
type Static struct {
// contains filtered or unexported fields
}
Static serves the embedded single-page UI: existing files as they are, every other path as index.html so client-side routes survive a reload. Unknown /api/ paths get the JSON 404 instead of the page.
type Version ¶
type Version struct {
// contains filtered or unexported fields
}
Version serves GET /api/v1/version.
func NewVersion ¶
NewVersion builds the handler from the build info.