http

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: GPL-3.0 Imports: 42 Imported by: 0

Documentation

Overview

Package http implements the HTTP server handlers for different resource endpoints

Index

Constants

View Source
const (
	ErrorNone          errorType = ""
	ErrorUnauthorized  errorType = "unauthorized"
	ErrorForbidden     errorType = "forbidden"
	ErrorTimeout       errorType = "timeout"
	ErrorCanceled      errorType = "canceled"
	ErrorExec          errorType = "execution"
	ErrorBadData       errorType = "bad_data"
	ErrorInternal      errorType = "internal"
	ErrorUnavailable   errorType = "unavailable"
	ErrorNotFound      errorType = "not_found"
	ErrorNotAcceptable errorType = "not_acceptable"
)

List of predefined errors.

Variables

View Source
var (
	ErrMaxQueryWindow     = errors.New("maximum query window exceeded")
	ErrMalformedTimeStamp = errors.New("malformed timestamp")
)
View Source
var (
	ErrNoUser            = errors.New("no user identified")
	ErrNoPrivs           = errors.New("current user does not have admin privileges")
	ErrInvalidRequest    = errors.New("invalid request")
	ErrInvalidQueryField = errors.New("invalid query fields")
	ErrMissingData       = errors.New("missing data in the request")
	ErrNoAuth            = errors.New("user do not have permissions to view metrics of this job/pod/vm")
	ErrNoAccess          = errors.New("user do not have permissions to access this resource")
	ErrInvalidClusterID  = errors.New("invalid ceems cluster id")
	ErrUnavailable       = errors.New("tsdb/pyroscope unavailable")
)

Custom errors.

Functions

func AdminUserNames

func AdminUserNames(ctx context.Context, dbConn *sql.DB) ([]string, error)

AdminUserNames returns a slice of admin users names.

func AdminUsers

func AdminUsers(ctx context.Context, dbConn *sql.DB) ([]models.User, error)

AdminUsers returns a slice of admin users fetched from DB. Errors must always be checked to ensure no row scanning has failed.

func Compress

func Compress(level int, types ...string) func(next http.Handler) http.Handler

Compress returns a new handler with compression. Passing a compression level of 5 is sensible value.

func ErrorResponse added in v0.12.0

func ErrorResponse[T any](w http.ResponseWriter, apiErr *APIError, logger *slog.Logger, data []T)

ErrorResponse returns error response for by setting errorString and errorType in response.

func Querier

func Querier[T any](ctx context.Context, dbConn *sql.DB, query Query, logger *slog.Logger) ([]T, error)

Querier queries the DB and return the response.

func VerifyOwnership

func VerifyOwnership(
	ctx context.Context,
	user string,
	clusterIDs []string,
	uuids []string,
	db *sql.DB,
	logger *slog.Logger,
) bool

VerifyOwnership returns true if user is the owner of queried units.

Types

type APIError added in v0.12.0

type APIError struct {
	Typ errorType
	Err error
}

APIError response.

func (*APIError) Error added in v0.12.0

func (e *APIError) Error() string

type CEEMSServer

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

CEEMSServer struct implements HTTP server for stats.

func New

func New(c *Config) (*CEEMSServer, error)

New creates new CEEMSServer struct instance.

func (*CEEMSServer) Shutdown

func (s *CEEMSServer) Shutdown(ctx context.Context) error

Shutdown server.

func (*CEEMSServer) Start

func (s *CEEMSServer) Start(_ context.Context) error

Start launches CEEMS HTTP server godoc.

type Compressor

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

Compressor represents a set of encoding configurations.

func NewCompressor

func NewCompressor(level int, types ...string) *Compressor

NewCompressor creates a new Compressor that will handle encoding responses.

The level should be one of the ones defined in the flate package. The types are the content types that are allowed to be compressed.

func (*Compressor) Handler

func (c *Compressor) Handler(next http.Handler) http.Handler

Handler returns a new middleware that will compress the response based on the current Compressor.

func (*Compressor) SetEncoder

func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc)

SetEncoder can be used to set the implementation of a compression algorithm.

The encoding should be a standardised identifier. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

For example, add the Brotli algorithm:

import brotli_enc "gopkg.in/kothar/brotli-go.v0/enc"

compressor := middleware.NewCompressor(5, "text/html")
compressor.SetEncoder("br", func(w io.Writer, level int) io.Writer {
	params := brotli_enc.NewBrotliParams()
	params.SetQuality(level)
	return brotli_enc.NewBrotliWriter(params, w)
})

type Config

type Config struct {
	Logger *slog.Logger
	Web    WebConfig
	DB     db.Config
}

Config makes a server config.

type EncoderFunc

type EncoderFunc func(w io.Writer, level int) io.Writer

An EncoderFunc is a function that wraps the provided io.Writer with a streaming compression algorithm and returns it.

In case of failure, the function should return nil.

type Query

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

Query builder struct.

type Response

type Response[T any] struct {
	Status    string    `json:"status"`
	Data      []T       `extensions:"x-nullable,x-omitempty" json:"data"`
	ErrorType errorType `extensions:"x-nullable,x-omitempty" json:"errorType,omitempty"`
	Error     string    `extensions:"x-nullable,x-omitempty" json:"error,omitempty"`
	Warnings  []string  `extensions:"x-nullable,x-omitempty" json:"warnings,omitempty"`
}

Response defines the response model of CEEMSAPIServer.

type WebConfig

type WebConfig struct {
	Addresses         []string
	WebSystemdSocket  bool
	WebConfigFile     string
	LandingConfig     *web.LandingConfig
	EnableDebugServer bool
	UserHeaderNames   []string
	ExternalURL       *url.URL
	RoutePrefix       string
	MaxQueryPeriod    model.Duration
	TrustedIPPrefixes []string
	RequestsLimit     int
	CORSOrigin        *regexp.Regexp
	EnableCompression bool
	CompressionLevel  int
	URL               string                  `yaml:"url"`
	HTTPClientConfig  config.HTTPClientConfig `yaml:",inline"`
}

WebConfig makes HTTP web config from CLI args.

func (*WebConfig) UnmarshalYAML

func (c *WebConfig) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

Jump to

Keyboard shortcuts

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