api

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: Apache-2.0 Imports: 22 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAppHandler

func NewAppHandler(args ServerArgs) http.Handler

NewAppHandler returns a new application (root) http handler

func NewDevicesHandler

func NewDevicesHandler(db *db.Db, influx *db.Influx, v RequestValidator) http.Handler

NewDevicesHandler returns a new device handler

func NewGetCmd

func NewGetCmd(portalURL, deviceID string, timeout time.Duration, debug bool) func() (data.DeviceCmd, error)

NewGetCmd returns a function that can be used to get device commands from the portal.

func NewIndexHandler

func NewIndexHandler(getAsset func(string) []byte) http.Handler

NewIndexHandler returns a new Index handler

func NewSendSamples

func NewSendSamples(portalURL, deviceID string, timeout time.Duration, debug bool) func([]data.Sample) error

NewSendSamples returns a function that can be used to send samples to a SimpleIoT portal instance

func NewSetVersion

func NewSetVersion(portalURL, deviceID string, timeout time.Duration, debug bool) func(ver data.DeviceVersion) error

NewSetVersion sets the device version in the portal

func NewV1Handler

func NewV1Handler(db *db.Db, influx *db.Influx, auth Authorizer) http.Handler

NewV1Handler returns a handle for V1 API

func NewWebsocketHandler

func NewWebsocketHandler(wsTx <-chan []byte, wsRx chan<- []byte, newConn chan<- bool) http.Handler

NewWebsocketHandler returns a new websocket handler. The wsTx channel is used to send data out the websocket, and the newConn channel is used to signal back to the caller that a new client has connected, and the initial data set needs to be sent over.

func Server

func Server(args ServerArgs) error

Server starts a API server instance

func ShiftPath

func ShiftPath(p string) (head, tail string)

ShiftPath is used to extract on segement of URL for processing

Types

type AlwaysValid added in v0.0.5

type AlwaysValid struct{}

AlwaysValid is used to disable authentication

func (AlwaysValid) NewToken added in v0.0.5

func (AlwaysValid) NewToken(id uuid.UUID) (string, error)

NewToken stub

func (AlwaysValid) Valid added in v0.0.5

func (AlwaysValid) Valid(*http.Request) (bool, string)

Valid stub

type App

type App struct {
	PublicHandler http.Handler
	IndexHandler  http.Handler
	V1ApiHandler  http.Handler
}

App is a struct that implements http.Handler interface

func (*App) ServeHTTP

func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request)

Top level handler for http requests in the coap-server process

type Auth added in v0.0.5

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

Auth handles user authentication requests.

func NewAuthHandler added in v0.0.5

func NewAuthHandler(db *db.Db, key NewTokener) Auth

NewAuthHandler returns a new authentication handler using the given key.

func (Auth) ServeHTTP added in v0.0.5

func (auth Auth) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP serves requests to authenticate.

type Authorizer added in v0.0.5

type Authorizer interface {
	NewToken(id uuid.UUID) (string, error)
	Valid(req *http.Request) (bool, string)
}

Authorizer defines a mechanism needed to authorize stuff

type CoapServer

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

CoapServer manages all the coap requests for this platform

func NewCoapServer

func NewCoapServer(port string) *CoapServer

NewCoapServer creates a new coap server

func (*CoapServer) Start

func (cs *CoapServer) Start() error

Start the coap server

type Devices

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

Devices handles device requests

func (*Devices) ServeHTTP

func (h *Devices) ServeHTTP(res http.ResponseWriter, req *http.Request)

Top level handler for http requests in the coap-server process TODO need to add device auth

func (*Devices) ServeHTTPDevice added in v0.0.5

func (h *Devices) ServeHTTPDevice(res http.ResponseWriter, req *http.Request, id, opt string)

ServeHTTPDevice is used to process requests from device, currently no auth and following URIs: - POST samples - GET cmd - POST version we have already checked for req type, so we can skip that check here

type Groups added in v0.0.5

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

Groups handles group requests.

func NewGroupsHandler added in v0.0.5

func NewGroupsHandler(db *db.Db, v RequestValidator) Groups

NewGroupsHandler returns a new handler for group requests.

func (Groups) ServeHTTP added in v0.0.5

func (h Groups) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP serves group requests.

type HTTPLogger added in v0.0.5

type HTTPLogger struct {
	*log.Logger
	// contains filtered or unexported fields
}

HTTPLogger can be used to log http requests

func NewHTTPLogger added in v0.0.5

func NewHTTPLogger(prefix string) *HTTPLogger

NewHTTPLogger returns a http logger

func (*HTTPLogger) Handler added in v0.0.5

func (l *HTTPLogger) Handler(next http.Handler) http.Handler

Handler wraps an HTTP handler and logs the request as necessary.

type IndexHandler

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

IndexHandler is used to serve the index page

func (*IndexHandler) ServeHTTP

func (h *IndexHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Key added in v0.0.5

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

Key provides a key for signing authentication tokens.

func NewKey added in v0.0.5

func NewKey(size int) (key Key, err error)

NewKey returns a new Key of the given size.

func (Key) NewToken added in v0.0.5

func (k Key) NewToken(userID uuid.UUID) (string, error)

NewToken returns a new authentication token signed by the Key.

func (Key) Valid added in v0.0.5

func (k Key) Valid(req *http.Request) (bool, string)

Valid returns whether the given request bears an authorization token signed by the Key.

func (Key) ValidToken added in v0.0.5

func (k Key) ValidToken(str string) (bool, string)

ValidToken returns whether the given string is an authentication token signed by the Key.

type NewTokener added in v0.0.5

type NewTokener interface {
	NewToken(userID uuid.UUID) (string, error)
}

NewTokener provides a new authentication token.

type RequestValidator added in v0.0.5

type RequestValidator interface {
	Valid(req *http.Request) (bool, string)
}

RequestValidator validates an HTTP request.

type Sample added in v0.0.5

type Sample struct {
	// Type of sample (voltage, current, key, etc)
	Type string `json:"type,omitempty" influx:"type,tag"`

	// ID of the device that provided the sample
	ID string `json:"id,omitempty" influx:"id,tag"`

	// Average OR
	// Instantaneous analog or digital value of the sample.
	// 0 and 1 are used to represent digital values
	Value float64 `json:"value,omitempty" influx:"value"`

	// statistical values that may be calculated
	Min float64 `json:"min,omitempty" influx:"min"`
	Max float64 `json:"max,omitempty" influx:"max"`

	// Time the sample was taken
	Time *time.Time `json:"time,omitempty" boltholdKey:"Time" gob:"-" influx:"time"`

	// Duration over which the sample was taken
	Duration time.Duration `json:"duration,omitempty" influx:"duration"`

	// Tags are additional attributes used to describe the sample
	// You might add things like friendly name, etc.
	Tags map[string]string `json:"tags,omitempty" influx:"-"`

	// Attributes are additional numerical values
	Attributes map[string]float64 `json:"attributes,omitempty" influx:"-"`
}

Sample is a custom value of data.Sample with Time set to a pointer. This allows omitempty to work for zero timestamps to avoid bloating JSON packets.

func NewSample added in v0.0.5

func NewSample(s data.Sample) Sample

NewSample converts a data.Sample to Sample and rounds floating point values to 3 dec places.

func NewSamples added in v0.0.5

func NewSamples(samples []data.Sample) []Sample

NewSamples converts []data.Sample to []Sample

type SampleFilter added in v0.0.5

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

SampleFilter is used to send samples upstream. It only sends the data has changed, and at a max frequency

func NewSampleFilter added in v0.0.5

func NewSampleFilter(minSend, periodicSend time.Duration) *SampleFilter

NewSampleFilter is used to creat a new sample filter If samples have changed that get sent out at a minSend interval frequency of minSend. All samples are periodically sent at lastPeriodicSend interval. Set minSend to 0 for things like config settings where you want them to be sent whenever anything changes.

func (*SampleFilter) Add added in v0.0.5

func (sf *SampleFilter) Add(samples []data.Sample) []data.Sample

Add adds samples and returns samples that meet the filter criteria

type ServerArgs added in v0.0.5

type ServerArgs struct {
	Port       string
	DbInst     *db.Db
	Influx     *db.Influx
	GetAsset   func(string) []byte
	Filesystem http.FileSystem
	Debug      bool
	Auth       Authorizer
}

ServerArgs can be used to pass arguments to the server subsystem

type Users added in v0.0.5

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

Users handles user requests.

func NewUsersHandler added in v0.0.5

func NewUsersHandler(db *db.Db, v RequestValidator) Users

NewUsersHandler returns a new handler for user requests.

func (Users) ServeHTTP added in v0.0.5

func (h Users) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP serves user requests.

type V1

type V1 struct {
	GroupsHandler  http.Handler
	UsersHandler   http.Handler
	DevicesHandler http.Handler
	AuthHandler    http.Handler
}

V1 handles v1 api requests

func (*V1) ServeHTTP

func (h *V1) ServeHTTP(res http.ResponseWriter, req *http.Request)

Top level handler for http requests in the coap-server process

type WebsocketHandler

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

WebsocketHandler handles websocket connections

func (*WebsocketHandler) ServeHTTP

func (h *WebsocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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