Documentation
¶
Overview ¶
Package ui serves taskmill's dashboard as an HTTP handler.
The dashboard shows the queues, tasks, schedules, and workers of an installation. Its actions pause and resume queues and schedules, run a schedule now, and retry, cancel, and delete tasks, one at a time or all that match the task list's filters.
New returns the handler, and Config sets its path, its login, and what it allows. Mount it in an application, behind the application's own authentication:
dashboard, err := ui.New(client, &ui.Config{Prefix: "/taskmill"})
// ...
mux.Handle("/taskmill/", auth(dashboard))
Or protect it with Config.BasicAuth, for example when the command github.com/code19m/taskmill/cmd/taskmill-ui serves it as a dashboard of its own. Another web application can show it in a frame once its origin is listed in Config.FrameAncestors.
Every action is a POST request, and a GET request never changes anything, so an application in front of the dashboard can allow or log the actions by their method. Actions are protected against cross-site request forgery with http.CrossOriginProtection, and Config.ReadOnly disables them entirely.
The dashboard is rendered on the server with the standard library. Two small scripts, served with it, keep pages live, show another page or the result of an action without loading the page anew, apply filters as they change, show times in the reader's time zone, confirm actions in dialogs, and apply the chosen theme. Every page also works without them, with its times in UTC, and nothing needs a JavaScript build step. The dashboard reads only through the bounded queries of taskmill.Client, so it is safe to point at a busy production database.
The dashboard guide shows how to run and protect it. The manual of the command lists the dashboard's routes and status codes, for the allow-list of a proxy.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶ added in v0.3.0
New returns the dashboard for client. cfg may be nil. It returns an error that wraps taskmill.ErrInvalidArgument if client is nil, if cfg.Prefix does not start with a slash, if cfg.BasicAuth has an empty username or password, a username with a colon, or a control character in either, or if an entry of cfg.FrameAncestors is empty or holds a space, comma, or semicolon.
The handler has nothing to close: it starts no goroutine, and it reaches the database only through client. That is why New returns a plain http.Handler, which the caller mounts, and may wrap in middleware of its own.
The handler gives every request 10 seconds, or the time until the deadline of the request's context if the application set an earlier one, and then answers 503 with a page that says so. Serve it from an http.Server whose WriteTimeout is longer than that time, or zero: a shorter one closes the connection before the answer.
Example ¶
This example mounts the dashboard under /taskmill, behind the application's own authentication.
package main
import (
"log"
"net/http"
"time"
"github.com/code19m/taskmill"
"github.com/code19m/taskmill/ui"
)
func requireAdmin(next http.Handler) http.Handler { return next }
func main() {
var client *taskmill.Client // from taskmill.NewClient
dashboard, err := ui.New(client, &ui.Config{Prefix: "/taskmill"})
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.Handle("/taskmill/", requireAdmin(dashboard))
server := &http.Server{Addr: "localhost:8080", Handler: mux, ReadHeaderTimeout: 10 * time.Second}
log.Fatal(server.ListenAndServe())
}
Output:
Example (Dashboard) ¶
This example serves the dashboard on its own, with a password, and lets the company's admin panel show it in a frame.
package main
import (
"log"
"net/http"
"time"
"github.com/code19m/taskmill"
"github.com/code19m/taskmill/ui"
)
func main() {
var client *taskmill.Client // from taskmill.NewClient
handler, err := ui.New(client, &ui.Config{
BasicAuth: &ui.BasicAuth{Username: "ops", Password: "a long random password"},
FrameAncestors: []string{"https://admin.example.com"},
})
if err != nil {
log.Fatal(err)
}
server := &http.Server{Addr: ":8443", Handler: handler, ReadHeaderTimeout: 10 * time.Second}
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}
Output:
Types ¶
type BasicAuth ¶
type BasicAuth struct {
// Username is the user's name. Required. It must not hold a colon:
// basic authentication ends the name at the first one.
Username string
// Password is the user's password. Required.
Password string
}
BasicAuth is the one user that may use the dashboard. Set it in Config.BasicAuth. Neither field may hold a control character, such as a line break at the end of a value read from a file: a browser cannot send one.
type Config ¶
type Config struct {
// Prefix is the path the handler is mounted at, such as "/taskmill". It
// must start with a slash. The handler strips it from requests and adds
// it to links and redirects, so do not wrap the handler in
// http.StripPrefix. Default: none, the root.
Prefix string
// ReadOnly hides and refuses every action that changes state.
ReadOnly bool
// BasicAuth, if set, requires every request to log in with HTTP basic
// authentication. Serve the dashboard over HTTPS when it is set: basic
// authentication sends the password with every request. Default: none;
// the application in front of the handler authenticates.
BasicAuth *BasicAuth
// FrameAncestors lists the origins of the pages that may show the
// dashboard in a frame, such as "https://admin.example.com", or "'self'"
// for the application's own pages. An entry must not be empty, or hold a
// space, a comma, or a semicolon. Default: none; the dashboard cannot be
// framed, which protects it from clickjacking.
FrameAncestors []string
// Logger receives every action taken, and the errors that the dashboard
// shows as a server error. Default: logs are discarded.
Logger *slog.Logger
}
Config configures the dashboard. The zero value is valid. New reports a field outside its limits as an error that wraps taskmill.ErrInvalidArgument.