handler

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package handler holds the HTTP handlers; they contain no business logic and call services only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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.

func (*Auth) Tokens

func (h *Auth) Tokens(w http.ResponseWriter, r *http.Request)

Tokens serves GET /api/v1/tokens.

func (*Auth) Users

func (h *Auth) Users(w http.ResponseWriter, r *http.Request)

Users serves GET /api/v1/users.

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.

func (*Config) Settings

func (h *Config) Settings(w http.ResponseWriter, r *http.Request)

Settings serves GET /api/v1/config/settings.

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.

func (*Devices) Overview

func (h *Devices) Overview(w http.ResponseWriter, r *http.Request)

Overview writes the generic level of every device over time (GET /api/v1/status); start/end as for Status.

func (*Devices) Status

func (h *Devices) Status(w http.ResponseWriter, r *http.Request)

Status writes the status changes of a device; start/end are RFC 3339 times or local dates (end inclusive), by default the last 90 days up to now.

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.

func NewEnergy

func NewEnergy(svc EnergyService, clock util.Clock, loc *time.Location, log zerolog.Logger,
) (*Energy, error)

NewEnergy builds the handler; loc is the site's location for local dates.

func (*Energy) ServeHTTP

func (h *Energy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP parses the query and writes the periods.

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.

func NewGauge

func NewGauge(svc GaugeService, loc *time.Location, log zerolog.Logger) (*Gauge, error)

NewGauge builds the handler; loc is the site's location for local dates.

func (*Gauge) ServeHTTP

func (h *Gauge) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP parses the query and writes the slots.

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.

func NewHealth

func NewHealth(svc HealthService, log zerolog.Logger) *Health

NewHealth builds the handler.

func (*Health) ServeHTTP

func (h *Health) ServeHTTP(w http.ResponseWriter, _ *http.Request)

ServeHTTP writes the health report with 200 or 503.

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.

func (*Report) ServeHTTP

func (h *Report) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ReportInfo

type ReportInfo struct {
	Site, Version string
	Location      *time.Location
}

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.

func NewSite

func NewSite(svc SiteService, log zerolog.Logger) *Site

NewSite builds the handler.

func (*Site) ServeHTTP

func (h *Site) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP writes the site model.

type SiteService

type SiteService interface {
	Model(ctx context.Context) (service.SiteModel, error)
}

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.

func NewStatic

func NewStatic(fsys fs.FS, log zerolog.Logger) *Static

NewStatic builds the handler for the UI files in fsys.

func (*Static) ServeHTTP

func (s *Static) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves a file or the page.

type Version

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

Version serves GET /api/v1/version.

func NewVersion

func NewVersion(b buildinfo.Info, log zerolog.Logger) *Version

NewVersion builds the handler from the build info.

func (*Version) ServeHTTP

func (v *Version) ServeHTTP(w http.ResponseWriter, _ *http.Request)

ServeHTTP writes the version.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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