Documentation
¶
Overview ¶
Package server contains the implementation of the Cloudburst server.
Index ¶
- Constants
- Variables
- type Backend
- type BackendConfig
- type CoAPResponseWriter
- type EndpointConfig
- type Error
- type HTTPResponseWriter
- type Limiter
- type LimiterConfig
- type Listener
- type MaxBytesReadCloser
- type MaxBytesReadSeeker
- type MaxBytesReader
- type Metrics
- type Request
- func (r *Request) Measurement() (string, error)
- func (r *Request) PathHost() string
- func (r *Request) QueryLast() bool
- func (r *Request) QueryTime() (from, to time.Time, err error)
- func (r *Request) RequestHost() (string, error)
- func (r *Request) SenML() (measurements []senml.Measurement, err error)
- type ResponseWriter
- type Server
Constants ¶
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.
const DefaultFromTime = -1 * time.Hour
DefaultFromTime contains the value for the 'from' parameter.
const DefaultMeasurementName = "cloudburst"
DefaultMeasurementName contains the measurement name when none is given in the request.
const DefaultToTime = 0 * time.Second
DefaultToTime contains the value for the 'to' parameter.
Variables ¶
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 ¶
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 ¶
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 (*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 ¶
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.
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"`
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.
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.
type MaxBytesReader ¶
MaxBytesReader is an io.Reader that returns a 'request entity too large' error when too many bytes are read.
type Metrics ¶
type Metrics struct {
Info *prometheus.Desc
Requests *prometheus.CounterVec
Responses *prometheus.CounterVec
LastMeasurement *prometheus.GaugeVec
}
Metrics contains Server metrics.
func (*Metrics) AddRequest ¶
AddRequest increments the counters associated with a request.
func (*Metrics) AddResponse ¶
AddResponse increments the counters associated with a response.
func (*Metrics) UpdateLastMeasurement ¶
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 ¶
NewCoAPRequest creates Request based on a coap.Request.
func NewHTTPRequest ¶
NewHTTPRequest returns a Request based on an http.Request.
func (*Request) Measurement ¶
Measurement returns the measurement name for the request.
func (*Request) RequestHost ¶
RequestHost returns the `host` for this request from either the path, or the remote address.
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 ¶
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 ¶
ListenAndServe starts handling requests based on the given listeners.