apiwidgets

package
v0.4.136 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 27 Imported by: ? 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 apiwidgets keeps the dashboard widgets that show values read from a JSON address. A recipe says what to read and how to show it, and can be shared: it never chooses the host, and its secrets are inputs that stay on the box.

Index

Constants

View Source
const ConfigFile = "/var/lib/casaos/api_widgets.json"

ConfigFile holds the widgets and their secrets (root, 0600).

Variables

View Source
var (
	// ErrNotFound is an id no widget has.
	ErrNotFound = errors.New("no such widget")
	// ErrRead is a read of a widget's address that failed, in Test: the text of
	// such an error is its reason, and nothing else.
	ErrRead = errors.New("the address could not be read")
	// ErrSecretAgain is a change of what is sent, or of where, that came without
	// every secret: the ones kept were given for another request.
	ErrSecretAgain = fmt.Errorf("%w: the address or the request changed, every input must be filled again", ErrInvalid)
)
View Source
var Default = New("/")

Default is the box's own.

View Source
var ErrInvalid = errors.New("invalid widget")

ErrInvalid is a recipe that is refused; the message names the field.

Functions

This section is empty.

Types

type Basic

type Basic struct {
	User     Value `json:"user"`
	Password Value `json:"password"`
}

Basic is HTTP basic authentication.

type Card

type Card struct {
	Title   string  `json:"title"`
	Icon    string  `json:"icon,omitempty"`
	Layout  string  `json:"layout"`
	Status  *Status `json:"status,omitempty"`
	Rows    []Row   `json:"rows"`
	More    int     `json:"more"`
	Link    string  `json:"link,omitempty"`
	Refresh int     `json:"refresh"`
	Error   string  `json:"error,omitempty"`
}

Card is what the dashboard draws. More counts the rows left out.

func Evaluate

func Evaluate(r Recipe, doc any) Card

Evaluate reads a recipe's values in doc, a JSON answer decoded with UseNumber. Nothing in the answer is an error: what cannot be read is shown as missing. Link and Error are left to the caller.

type Change

type Change struct {
	Base        string            `json:"base"`
	InsecureTLS bool              `json:"insecure_tls"`
	Recipe      json.RawMessage   `json:"recipe"`
	Secrets     map[string]string `json:"secrets"`
}

Change is what creates or replaces a widget. A secret left out, or empty, is one that is not given.

type Hub

type Hub struct {
	Root string
	Now  func() time.Time
	// contains filtered or unexported fields
}

Hub is the API widgets of one box.

func New

func New(root string) *Hub

New keeps api_widgets.json under root and reads through the guarded client.

func (*Hub) Card

func (h *Hub) Card(ctx context.Context, id string) (Card, error)

Card is the card of a widget: read when it is asked for, kept refresh seconds, or 60 s when the read failed (Error then says why, and there is no row). There is one read at a time for a widget; who asks meanwhile waits and gets its card.

func (*Hub) Create

func (h *Hub) Create(c Change) (View, error)

Create adds a widget. Every input of its recipe needs its secret.

func (*Hub) Delete

func (h *Hub) Delete(id string) error

Delete removes a widget, its secrets and its card.

func (*Hub) List

func (h *Hub) List() []View

List is the widgets, in the order they were created.

func (*Hub) Preview

func (h *Hub) Preview(recipe []byte, response []byte) (Card, error)

Preview is the card a recipe, which may still be a draft, makes of an answer the caller already has. It sends nothing and needs no secret.

func (*Hub) Test

func (h *Hub) Test(ctx context.Context, id string, c Change) (TestResult, error)

Test reads the address of a change and answers what it gave, with the card the recipe, which may still be a draft, makes of it. With the id of a widget, the inputs left out take its secrets, by the rule of Update. Nothing is written and nothing is kept. An error of the read shows its reason only.

func (*Hub) Update

func (h *Hub) Update(id string, c Change) (View, error)

Update replaces a widget, and forgets its card.

type Input

type Input struct {
	ID    string `json:"id"`
	Label string `json:"label"`
}

Input is a secret the box asks for.

type Recipe

type Recipe struct {
	Version  int              `json:"recasaos_widget"`
	Name     string           `json:"name"`
	Icon     string           `json:"icon,omitempty"`
	Path     string           `json:"path"`
	Query    map[string]Value `json:"query,omitzero"`
	Headers  map[string]Value `json:"headers,omitzero"`
	Basic    *Basic           `json:"basic,omitempty"`
	Inputs   []Input          `json:"inputs,omitzero"`
	BaseHint string           `json:"base_hint,omitempty"`
	Layout   string           `json:"layout"`
	Rows     []RowSpec        `json:"rows"`
	Status   *StatusSpec      `json:"status,omitempty"`
	Refresh  int              `json:"refresh"`
}

Recipe is the shareable half of a widget.

func ParseRecipe

func ParseRecipe(data []byte, draft bool) (Recipe, error)

ParseRecipe reads and checks a recipe. A draft, one still being built, may have no row yet. No refresh is 300 seconds, no layout is "rows".

type Row

type Row struct {
	Label string   `json:"label"`
	Value Shown    `json:"value"`
	Level string   `json:"level,omitempty"`
	Bar   *float64 `json:"bar,omitempty"`
}

Row is one line of the card. Bar is 0 to 100.

type RowSpec

type RowSpec struct {
	Label  string  `json:"label"`
	Value  string  `json:"value"`
	Each   *string `json:"each,omitempty"` // "" is the root, so presence is what counts
	Format string  `json:"format,omitempty"`
	Unit   string  `json:"unit,omitempty"`
	Rules  []Rule  `json:"rules,omitzero"`
	Limit  int     `json:"limit,omitempty"`
	Bar    bool    `json:"bar,omitempty"`
}

RowSpec is one line of the card. With Each, it is one line per element of that array, and Label and Value are pointers inside the element.

type Rule

type Rule struct {
	Op    string `json:"op"`
	To    any    `json:"to"`
	Level string `json:"level"`
	Text  string `json:"text,omitempty"`
}

Rule gives a level, and maybe another text, to a value; the first true one wins.

type Shown

type Shown struct {
	Kind   string   `json:"kind"` // text, number, bytes, percent, duration, date (Unix seconds) or missing
	Text   string   `json:"text,omitempty"`
	Number *float64 `json:"number,omitempty"`
	Unit   string   `json:"unit,omitempty"`
}

Shown is a value ready for the card. The browser writes numbers in the reader's language, so only a text comes written.

type Status

type Status struct {
	Value Shown  `json:"value"`
	Level string `json:"level,omitempty"`
}

Status is the card's chip.

type StatusSpec

type StatusSpec struct {
	Value  string `json:"value"`
	Format string `json:"format,omitempty"`
	Unit   string `json:"unit,omitempty"`
	Rules  []Rule `json:"rules,omitzero"`
}

StatusSpec fills the card's chip.

type TestResult

type TestResult struct {
	Response json.RawMessage `json:"response"`
	Card     Card            `json:"card"`
}

TestResult is what the address answered and the card the recipe makes of it.

type Value

type Value struct{ Text, Input string }

Value is a header, query or basic value: a fixed text, or the input that fills it.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON writes "text" or {"input":"id"}.

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(data []byte) error

UnmarshalJSON is as strict as the rest of the recipe: the decoder's own strictness does not reach here.

type View

type View struct {
	ID          string          `json:"id"`
	Base        string          `json:"base"`
	InsecureTLS bool            `json:"insecure_tls"`
	Recipe      Recipe          `json:"recipe"`
	InputsSet   map[string]bool `json:"inputs_set"`
	// why the last read failed, as long as that card is the one kept
	Error string `json:"error"`
}

View is a widget as it is shown: of its secrets, only whether each is filled.

Jump to

Keyboard shortcuts

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