server

package
v0.0.0-...-227a949 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: EUPL-1.2 Imports: 36 Imported by: 0

Documentation

Overview

Package server contains the implementation of the Cloudburst server.

Index

Constants

View Source
const (
	TypeAny         = "*/*"
	TypePlainText   = "text/plain"
	TypeApplication = "application/*"
	TypeCBOR        = "application/cbor"
	TypeJSON        = "application/json"
	TypeXML         = "application/xml"
	TypeSenMLCBOR   = "application/senml+cbor"
	TypeSenMLJSON   = "application/senml+json"
	TypeSenMLXML    = "application/senml+xml"
)

Supported MIME types.

View Source
const DefaultFromTime = -1 * time.Hour

DefaultFromTime contains the value for the 'from' parameter.

View Source
const DefaultMeasurementName = "cloudburst"

DefaultMeasurementName contains the measurement name when none is given in the request.

View Source
const DefaultToTime = 0 * time.Second

DefaultToTime contains the value for the 'to' parameter.

Variables

View Source
var MeasurementNameRegex = regexp.MustCompile(`^\w+$`)

MeasurementNameRegex contains the regular expression used for validating measurements.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Name returns the name of the backend.
	Name() string

	// Query retrieves SenML data from the backend with the given parameters.
	Query(name, host string, from, to time.Time, last bool) (measurements []senml.Measurement, err error)

	// Write writes SenML data to the backend.
	Write(name, host string, data ...senml.Measurement) error

	// Close closes the connection to the backend.
	Close() error
}

Backend represents a backends for the Cloudburst server.

func NewBackend

func NewBackend(config *BackendConfig) (backend Backend, err error)

NewBackend returns a configured backends from the given configuration. The first non-nil field of the config is used to determine the chosen backends.

type BackendConfig

type BackendConfig struct {
	// InfluxDB configuration
	InfluxDB *influxdb.ClientConfig `yaml:"influxdb"`

	// InfluxDB2 configuration.
	InfluxDB2 *influxdb2.ClientConfig `yaml:"influxdb2"`

	// PostgreSQL configuration.
	Postgres *postgres.ClientConfig `yaml:"postgres"`

	// TimescaleDB configuration.
	Timescale *postgres.ClientConfig `yaml:"timescale"`
}

BackendConfig contains the backends configuration. Note that the first non-nil field of this config is used to determine the chosen backends.

type CoAPResponseWriter

type CoAPResponseWriter struct {
	mux.ResponseWriter
}

CoAPResponseWriter is a CoAP based ResponseWriter.

func NewCoAPResponseWriter

func NewCoAPResponseWriter(w mux.ResponseWriter) *CoAPResponseWriter

NewCoAPResponseWriter creates a CoAP based ResponseWriter.

func (*CoAPResponseWriter) SetResponse

func (r *CoAPResponseWriter) SetResponse(code codes.Code, contentType string, data []byte) error

SetResponse sets the response code, content type and data.

func (*CoAPResponseWriter) WriteErrors

func (r *CoAPResponseWriter) WriteErrors() bool

WriteErrors returns true if errors should be written to the client.

type EndpointConfig

type EndpointConfig struct {
	Enabled bool   `yaml:"enabled"`
	Path    string `yaml:"path"`
}

EndpointConfig contains a generic endpoint configuration.

type Error

type Error struct {
	// The error causing this error.
	Err error

	// The corresponding response code.
	Code codes.Code
}

Error is a generic request/response error.

func NewError

func NewError(code codes.Code, err error) *Error

NewError returns a new Error.

func NewErrorf

func NewErrorf(code codes.Code, format string, a ...interface{}) *Error

NewErrorf returns a new error with a custom description.

func (*Error) Error

func (e *Error) Error() string

Error returns the error text.

func (*Error) Write

func (e *Error) Write(w ResponseWriter, req *Request)

Write writes the for a Request to a ResponseWriter.

type HTTPResponseWriter

type HTTPResponseWriter struct {
	http.ResponseWriter
}

HTTPResponseWriter contains an HTTP based ResponseWriter.

func NewHTTPResponseWriter

func NewHTTPResponseWriter(w http.ResponseWriter) *HTTPResponseWriter

NewHTTPResponseWriter returns ResponseWriter based on a http.ResponseWriter.

func (*HTTPResponseWriter) SetResponse

func (r *HTTPResponseWriter) SetResponse(code codes.Code, contentType string, data []byte) error

SetResponse sets the response code, content type and data.

func (*HTTPResponseWriter) WriteErrors

func (r *HTTPResponseWriter) WriteErrors() bool

WriteErrors returns true if errors should be written to the client.

type Limiter

type Limiter struct {
	LimiterConfig
	sync.Mutex
	// contains filtered or unexported fields
}

Limiter implements a rate limiter. It also contains other (non-interactive) limiting settings to check.

func NewLimiter

func NewLimiter(config LimiterConfig) (*Limiter, error)

NewLimiter returns an initialized limiter for the given config.

func (*Limiter) CheckRateLimit

func (l *Limiter) CheckRateLimit(ip net.IP, v int) bool

CheckRateLimit returns true if the given IP is allowed to send v items.

type LimiterConfig

type LimiterConfig struct {
	Body     int64   `yaml:"body"`
	Rate     float64 `yaml:"rate"`
	Burst    float64 `yaml:"burst"`
	IPv4Mask int     `yaml:"ipv4_mask"`
	IPv6Mask int     `yaml:"ipv6_mask"`
}

LimiterConfig contains the limit configuration.

type Listener

type Listener struct {
	Type         string `yaml:"type"`
	Address      string `yaml:"address"`
	CertFile     string `yaml:"tls_cert"`
	KeyFile      string `yaml:"tls_key"`
	PreSharedKey string `yaml:"psk"`
	Metrics      bool   `yaml:"metrics"`
	MetricsPath  string `yaml:"metrics_path"`
	Profile      bool   `yaml:"profile"`
	ProfilePath  string `yaml:"profile_path"`
}

Listener represents the configuration for an HTTP/CoAP listener.

func (*Listener) MetricsEndpoint

func (l *Listener) MetricsEndpoint() *EndpointConfig

MetricsEndpoint returns the endpoint configuration for the metrics endpoint.

func (*Listener) ProfileEndpoint

func (l *Listener) ProfileEndpoint() *EndpointConfig

ProfileEndpoint returns the endpoint configuration for the profiling endpoint.

type MaxBytesReadCloser

type MaxBytesReadCloser struct {
	MaxBytesReader
	Reader io.ReadCloser
}

MaxBytesReadCloser is an io.ReadCloser that returns a 'request entity too large' error when too many bytes are read.

func NewMaxBytesReadCloser

func NewMaxBytesReadCloser(r io.ReadCloser, n int64) *MaxBytesReadCloser

NewMaxBytesReadCloser returns an initialized io.ReadCloser.

func (*MaxBytesReadCloser) Close

func (l *MaxBytesReadCloser) Close() error

Close the reader.

type MaxBytesReadSeeker

type MaxBytesReadSeeker struct {
	MaxBytesReader
	Reader io.ReadSeeker
}

MaxBytesReadSeeker is an io.ReadSeeker that returns a 'request entity too large' error when too many bytes are read.

func NewMaxBytesReadSeeker

func NewMaxBytesReadSeeker(r io.ReadSeeker, n int64) *MaxBytesReadSeeker

NewMaxBytesReadSeeker returns an initialized io.ReadSeeker.

func (*MaxBytesReadSeeker) Seek

func (l *MaxBytesReadSeeker) Seek(offset int64, whence int) (int64, error)

Seek to a point in the reader.

type MaxBytesReader

type MaxBytesReader struct {
	Reader io.Reader
	Remain int64
}

MaxBytesReader is an io.Reader that returns a 'request entity too large' error when too many bytes are read.

func (*MaxBytesReader) Read

func (l *MaxBytesReader) Read(p []byte) (n int, err error)

Read bytes from the reader.

type Metrics

type Metrics struct {
	Info            *prometheus.Desc
	Requests        *prometheus.CounterVec
	Responses       *prometheus.CounterVec
	LastMeasurement *prometheus.GaugeVec
}

Metrics contains Server metrics.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics returns an initialized Metrics object.

func (*Metrics) AddRequest

func (m *Metrics) AddRequest(req *Request)

AddRequest increments the counters associated with a request.

func (*Metrics) AddResponse

func (m *Metrics) AddResponse(req *Request, code codes.Code)

AddResponse increments the counters associated with a response.

func (*Metrics) UpdateLastMeasurement

func (m *Metrics) UpdateLastMeasurement(host, measurement string)

UpdateLastMeasurement updates the timestamp of the last update of a host in a measurement.

type Request

type Request struct {
	Protocol    string
	Method      string
	RemoteAddr  string
	Path        []string
	Query       url.Values
	ContentType string
	Accept      string
	Body        io.Reader
	Confirmable bool
}

Request represents a generic API request.

func NewCoAPRequest

func NewCoAPRequest(r *mux.Message, c mux.Conn) *Request

NewCoAPRequest creates Request based on a coap.Request.

func NewHTTPRequest

func NewHTTPRequest(r *http.Request) *Request

NewHTTPRequest returns a Request based on an http.Request.

func (*Request) Measurement

func (r *Request) Measurement() (string, error)

Measurement returns the measurement name for the request.

func (*Request) PathHost

func (r *Request) PathHost() string

PathHost returns the `host` value in the request path.

func (*Request) QueryLast

func (r *Request) QueryLast() bool

QueryLast returns the `last` value in the query.

func (*Request) QueryTime

func (r *Request) QueryTime() (from, to time.Time, err error)

QueryTime returns the `from` and `to` (time) values in the query.

func (*Request) RequestHost

func (r *Request) RequestHost() (string, error)

RequestHost returns the `host` for this request from either the path, or the remote address.

func (*Request) SenML

func (r *Request) SenML() (measurements []senml.Measurement, err error)

SenML returns the SenML data encoded in the body.

type ResponseWriter

type ResponseWriter interface {
	// SetResponse sets the response code, content type and data.
	SetResponse(code codes.Code, contentType string, data []byte) error

	// WriteErrors returns true if errors should be written to the client.
	WriteErrors() bool
}

ResponseWriter is a generic response writer.

type Server

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

Server represents a Cloudburst server.

func NewServer

func NewServer(limits *Limiter, backends ...Backend) (server *Server, err error)

NewServer creates a server backed by a certain InfluxDB client and set of listeners.

func (*Server) CoAPHandler

func (s *Server) CoAPHandler(w mux.ResponseWriter, r *mux.Message)

CoAPHandler handles CoAP requests.

func (*Server) HTTPHandler

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

HTTPHandler handles HTTP requests.

func (*Server) Handler

func (s *Server) Handler(w ResponseWriter, req *Request)

Handler handles generic API requests.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(listeners ...*Listener) error

ListenAndServe starts handling requests based on the given listeners.

Jump to

Keyboard shortcuts

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