Documentation
¶
Overview ¶
Package web provides HTTP server components for the onWatch dashboard.
Index ¶
- func AuthMiddleware(username, password string) func(http.Handler) http.Handler
- func GetEmbeddedStatic() embed.FS
- func GetEmbeddedTemplates() embed.FS
- func HashPassword(password string) string
- func RequireAuth(username, password string) func(http.Handler) http.Handler
- func SessionAuthMiddleware(sessions *SessionStore, logger ...*slog.Logger) func(http.Handler) http.Handler
- type Handler
- func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Current(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Cycles(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request)
- func (h *Handler) GetSettings(w http.ResponseWriter, r *http.Request)
- func (h *Handler) History(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Insights(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Login(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Logout(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Providers(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Sessions(w http.ResponseWriter, r *http.Request)
- func (h *Handler) SetVersion(v string)
- func (h *Handler) Summary(w http.ResponseWriter, r *http.Request)
- func (h *Handler) UpdateSettings(w http.ResponseWriter, r *http.Request)
- type Server
- type SessionStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶
AuthMiddleware returns an http.Handler that enforces Basic Auth. Kept for backwards compatibility with tests.
func GetEmbeddedStatic ¶
GetEmbeddedStatic returns the embedded static files filesystem
func GetEmbeddedTemplates ¶
GetEmbeddedTemplates returns the embedded templates filesystem
func HashPassword ¶
HashPassword returns the SHA-256 hex hash of a password.
func RequireAuth ¶
RequireAuth is an alias for AuthMiddleware.
func SessionAuthMiddleware ¶
func SessionAuthMiddleware(sessions *SessionStore, logger ...*slog.Logger) func(http.Handler) http.Handler
SessionAuthMiddleware uses session cookies for browser requests and Basic Auth for API.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles HTTP requests for the web dashboard
func NewHandler ¶
func NewHandler(store *store.Store, tracker *tracker.Tracker, logger *slog.Logger, sessions *SessionStore, cfg *config.Config, zaiTracker ...*tracker.ZaiTracker) *Handler
NewHandler creates a new Handler instance
func (*Handler) ChangePassword ¶
func (h *Handler) ChangePassword(w http.ResponseWriter, r *http.Request)
ChangePassword handles password change requests.
func (*Handler) Current ¶
func (h *Handler) Current(w http.ResponseWriter, r *http.Request)
Current returns current quota status (API endpoint)
func (*Handler) Cycles ¶
func (h *Handler) Cycles(w http.ResponseWriter, r *http.Request)
Cycles returns reset cycle data (API endpoint)
func (*Handler) Dashboard ¶
func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request)
Dashboard renders the main dashboard page
func (*Handler) GetSettings ¶
func (h *Handler) GetSettings(w http.ResponseWriter, r *http.Request)
GetSettings returns current settings as JSON.
func (*Handler) History ¶
func (h *Handler) History(w http.ResponseWriter, r *http.Request)
History returns usage history (API endpoint)
func (*Handler) Insights ¶
func (h *Handler) Insights(w http.ResponseWriter, r *http.Request)
Insights returns computed deep analytics (API endpoint)
func (*Handler) Login ¶
func (h *Handler) Login(w http.ResponseWriter, r *http.Request)
Login handles GET (show form) and POST (authenticate).
func (*Handler) Logout ¶
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request)
Logout clears the session and redirects to login.
func (*Handler) Providers ¶
func (h *Handler) Providers(w http.ResponseWriter, r *http.Request)
Providers returns available providers configuration
func (*Handler) Sessions ¶
func (h *Handler) Sessions(w http.ResponseWriter, r *http.Request)
Sessions returns session data (API endpoint)
func (*Handler) SetVersion ¶
SetVersion sets the version string for display in the dashboard.
func (*Handler) Summary ¶
func (h *Handler) Summary(w http.ResponseWriter, r *http.Request)
Summary returns usage summary (API endpoint)
func (*Handler) UpdateSettings ¶
func (h *Handler) UpdateSettings(w http.ResponseWriter, r *http.Request)
UpdateSettings updates settings from JSON body (partial updates supported).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server wraps an HTTP server with graceful shutdown capabilities
func NewServer ¶
func NewServer(port int, handler *Handler, logger *slog.Logger, username, passwordHash string) *Server
NewServer creates a new Server instance. passwordHash should be a SHA-256 hex hash of the admin password.
type SessionStore ¶
type SessionStore struct {
// contains filtered or unexported fields
}
SessionStore manages session tokens with SQLite persistence and in-memory cache.
func NewSessionStore ¶
func NewSessionStore(username, passwordHash string, db *store.Store) *SessionStore
NewSessionStore creates a session store with the given credentials. passwordHash should be a SHA-256 hex hash of the password. If a store is provided, tokens are persisted in SQLite.
func (*SessionStore) Authenticate ¶
func (s *SessionStore) Authenticate(username, password string) (string, bool)
Authenticate validates credentials and returns a session token if valid.
func (*SessionStore) Invalidate ¶
func (s *SessionStore) Invalidate(token string)
Invalidate removes a session token.
func (*SessionStore) InvalidateAll ¶
func (s *SessionStore) InvalidateAll()
InvalidateAll removes all session tokens (used after password change).
func (*SessionStore) UpdatePassword ¶
func (s *SessionStore) UpdatePassword(newHash string)
UpdatePassword updates the stored password hash.
func (*SessionStore) ValidateToken ¶
func (s *SessionStore) ValidateToken(token string) bool
ValidateToken checks if a session token is valid and not expired.