http

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: GPL-3.0 Imports: 40 Imported by: 0

Documentation

Overview

Package http implements the HTTP server handlers for different resource endpoints

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxQueryWindow     = errors.New("maximum query window exceeded")
	ErrMalformedTimeStamp = errors.New("malformed timestamp")
)

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

Passing a compression level of 5 is sensible value.

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 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

@title			CEEMS API
@version		1.0
@description	OpenAPI specification (OAS) for the CEEMS REST API.
@description
@description	See the Interactive Docs to try CEEMS API methods without writing code, and get
@description	the complete schema of resources exposed by the API.
@description
@description	If basic auth is enabled, all the endpoints require authentication.
@description
@description	All the endpoints, except `health`, `swagger`, `debug` and `demo`,
@description	must send a user-agent header.
@description
@description	A demo instance of CEEMS API server is provided for the users to test. This
@description	instance is running at `https://ceems-demo.myaddr.tools:7443` and it is the
@description	default server that will serve the requests originating from current OAS client.
@description
@description	Some of the valid users for this demo instance are:
@description	- arnold
@description	- betty
@description	- edna
@description	- gazoo
@description	- wilma
@description
@description	Every request must contain a `X-Grafana-User` header with one of the usernames
@description	above as the value to the header. This is how CEEMS API server recognise the user.
@description
@description	Some of the valid projects for this demo instance are:
@description	- bedrock
@description	- cornerstone
@description
@description	Demo instance have CORS enabled to allow cross-domain communication from the browser.
@description	All responses have a wildcard same-origin which makes them completely public and
@description	accessible to everyone, including any code on any site.
@description
@description	To test admin resources, users can use `admin` as `X-Grafana-User`.
@description
@description				Timestamps must be specified in milliseconds, unless otherwise specified.

@contact.name				Mahendra Paipuri
@contact.url				https://github.com/ceems-dev/ceems/issues
@contact.email				mahendra.paipuri@gmail.com

@license.name				GPL-3.0 license
@license.url				https://www.gnu.org/licenses/gpl-3.0.en.html

@securityDefinitions.basic	BasicAuth

@host						ceems-demo.myaddr.tools:7443
@BasePath					/api/v1

@schemes					https

@externalDocs.url			https://ceems-dev.github.io/ceems/

@x-logo						{"url": "https://raw.githubusercontent.com/ceems-dev/ceems/refs/heads/main/website/static/img/logo.png", "altText": "CEEMS logo"}

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
	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.

Directories

Path Synopsis
Package docs Code generated by swaggo/swag.
Package docs Code generated by swaggo/swag.

Jump to

Keyboard shortcuts

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