server

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package server provides the HTTP server for the hostcheck service.

Index

Constants

View Source
const (
	RFC952LabelMaxLength    = 63
	RFC952HostnameMaxLength = 253
	RFC952MinLabels         = 2
)

Variables

View Source
var (
	ErrInvalidRequest = errors.New("invalid request")
	ErrFallthrough    = errors.New("request parsing fallthrough")
)

Functions

func IsAlphanumeric

func IsAlphanumeric(r rune) bool

IsAlphanumeric checks if a rune is an ASCII letter or digit.

func IsValidURL

func IsValidURL(input string) bool

IsValidURL checks if the input is a valid URL with a hostname.

func ParseHostname

func ParseHostname(input string) (string, error)

ParseHostname extracts and validates a hostname from the input string. Returns the cleaned hostname or an error.

func ValidateHostname

func ValidateHostname(input string) error

ValidateHostname validates a hostname according to RFC 952/1123. It strips http:// or https:// prefixes, trailing paths, and ports before validation. Returns an error with a descriptive message if validation fails.

func ValidateLabel

func ValidateLabel(label string, index int) error

ValidateLabel validates a single label according to RFC 952/1123.

Types

type CheckInfo

type CheckInfo struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type CheckRequest

type CheckRequest struct {
	Hostname string   `json:"hostname"`
	Checks   []string `json:"checks"`
	Timeout  int      `json:"timeout,omitempty"` // timeout in seconds
}

CheckRequest represents the JSON request body for check endpoints.

type CheckResponse

type CheckResponse struct {
	Hostname string          `json:"hostname"`
	Results  []check.Result  `json:"results"`
	Summary  SummaryResponse `json:"summary"`
}

type Config

type Config struct {
	RateLimit     float64       // Requests per second (default: 10)
	MaxConcurrent int           // Max concurrent checks (default: 4 or NumCPU)
	MaxTimeout    time.Duration // Maximum allowed timeout per request (default: 300s)
}

Config holds configuration options for the server.

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

type Metrics

type Metrics struct {
	Registry      *prometheus.Registry
	RequestsTotal *prometheus.CounterVec
	ChecksTotal   *prometheus.CounterVec
	CheckDuration *prometheus.HistogramVec
}

func NewMetrics

func NewMetrics() *Metrics

type Server

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

Server represents the HTTP server.

func NewServer

func NewServer(
	registry *plugin.Registry,
	logger *slog.Logger,
	hc healthcheck.Health,
	cfg Config,
	logmgr slogtool.LogManager,
) *Server

NewServer creates a new server instance.

func (*Server) HandleCheck

func (s *Server) HandleCheck(w http.ResponseWriter, r *http.Request)

HandleCheck runs checks and returns JSON response. Supports both GET (query parameters) and POST (JSON body) requests.

func (*Server) HandleCheckList

func (s *Server) HandleCheckList(w http.ResponseWriter, _ *http.Request)

HandleCheckList returns the list of available checks.

func (*Server) HandleCheckSSE

func (s *Server) HandleCheckSSE(w http.ResponseWriter, r *http.Request)

HandleCheckSSE runs checks with Server-Sent Events streaming. Supports both GET (query parameters) and POST (JSON body) requests.

func (*Server) HandleIndex

func (s *Server) HandleIndex(w http.ResponseWriter, r *http.Request)

HandleIndex serves the main web UI.

func (*Server) HandleLogLevel

func (s *Server) HandleLogLevel(w http.ResponseWriter, r *http.Request)

HandleLogLevel handles PUT requests to change log level at runtime.

func (*Server) HandleVersion

func (s *Server) HandleVersion(w http.ResponseWriter, _ *http.Request)

HandleVersion returns the service version.

func (*Server) ParseCheckRequest

func (s *Server) ParseCheckRequest(r *http.Request) (*CheckRequest, error)

ParseCheckRequest extracts check parameters from either POST JSON body or GET query parameters.

func (*Server) RateLimitMiddleware

func (s *Server) RateLimitMiddleware(next http.HandlerFunc) http.HandlerFunc

RateLimitMiddleware wraps a handler with rate limiting.

func (*Server) Run

func (s *Server) Run(ctx context.Context, addr string) error

Run starts the HTTP server.

type SummaryResponse

type SummaryResponse struct {
	Passed  int `json:"passed"`
	Partial int `json:"partial"`
	Failed  int `json:"failed"`
	Warned  int `json:"warned"`
	Skipped int `json:"skipped"`
}

Jump to

Keyboard shortcuts

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