webcore

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

* Copyright (c) 2016 The ConnectorDB Contributors Licensed under the MIT license. *

* Copyright (c) 2016 The ConnectorDB Contributors Licensed under the MIT license. *

* Copyright (c) 2016 The ConnectorDB Contributors Licensed under the MIT license. *

* Copyright (c) 2016 The ConnectorDB Contributors Licensed under the MIT license. *

* Copyright (c) 2016 The ConnectorDB Contributors Licensed under the MIT license. *

Index

Constants

View Source
const (
	DEBUG   = iota
	INFO    = iota
	WARNING = iota
	ERROR   = iota
)

Log levels supported by LogRequest

Variables

View Source
var (

	// ErrNoAuthentication is an error that is thrown when no authentication is given
	ErrNoAuthentication = errors.New("No authentication given with request")

	//CookieMonster is the handler for authentication based on cookies
	CookieMonster *securecookie.SecureCookie
)
View Source
var (
	//The following globals are atomically incremented/decremnted to give statistics
	StatsAuthFails   = uint32(0)
	StatsRESTQueries = uint32(0)
	StatsWebQueries  = uint32(0)
	StatsInserts     = uint32(0)
	StatsErrors      = uint32(0)
	StatsPanics      = uint32(0)
	StatsActive      = int32(0)

	QueryTimers = make(map[string]*QueryTimer)
)
View Source
var (
	//The name of the site.
	SiteName string
	//AllowCrossOrigin: Whether or not cross origin requests are permitted
	AllowCrossOrigin = false

	//IsActive - no need for sync, really. It specifies if the server should accept connections.
	IsActive = true

	//ShutdownChannel is a shared channel which is used when a shutdown is signalled.
	//Each goroutine that uses the ShutdownChannel is to IMMEDIATELY refire the channel before doing anything else,
	//so that the signal continues throughout the system
	ShutdownChannel = make(chan bool, 1)
)

Functions

func Authenticate

func Authenticate(db *connectordb.Database, request *http.Request) (o *authoperator.AuthOperator, err error)

Authenticate gets the authenticated device Operator given an http.Request

func CreateSessionCookie

func CreateSessionCookie(o *authoperator.AuthOperator, writer http.ResponseWriter, request *http.Request) error

CreateSessionCookie generates the authentication cookie from an authenticated user

func GetFuncName

func GetFuncName(apifunc APIHandler) string

GetFuncName returns the name of the function that is going to handle a request

func GetRequestLogger

func GetRequestLogger(request *http.Request, opname string) *log.Entry

GetRequestLogger returns a logrus log entry which has fields prepopulated for the request

func GetStreamPath

func GetStreamPath(request *http.Request) (username string, devicename string, streamname string, streampath string)

GetStreamPath returns the relevant parts of a stream path

func HandlePanic

func HandlePanic(logger *log.Entry)

HandlePanic is called in defer statements to handle a panic within a request. It is assumed that the connection is active

func HasSession

func HasSession(request *http.Request) bool

HasSession returns whether there is a session cookie with this request

func Initialize

func Initialize(c *config.Configuration) error

Initialize sets up the necessary global state of WebCore such that it fits the given configuration. Note that it is called in a config change callback, and does not use locking of state, so some weird bugs might be possible if config is reloaded frequently during heavy load

func LogRequest

func LogRequest(l *log.Entry, loglevel int, txt string, tdiff time.Duration)

LogRequest writes a log message given the log entry to use, a log level, optional text, and the query duration

func RunQueryTimers

func RunQueryTimers()

RunQueryTimers periodically gets and prints the query average runtime and variance

func RunStats

func RunStats()

RunStats periodically displays query amounts and relevant data. It does not display anything if there was no action within a time period.

func SetEnabled

func SetEnabled(v bool)

SetEnabled allows to enable and disable acceptance of connections in a simple way

func Shutdown

func Shutdown()

Shutdown shutd down the server

func StatsAddFail

func StatsAddFail(err error)

StatsAddFail adds an authentication failure to the statistics

func WriteAccessControlHeaders

func WriteAccessControlHeaders(writer http.ResponseWriter, request *http.Request)

WriteAccessControlHeaders writes the access control headers for the site

Types

type APIHandler

type APIHandler func(o *authoperator.AuthOperator, writer http.ResponseWriter, request *http.Request, logger *log.Entry) (int, string)

APIHandler is a function that handles some part of the REST API given a specific operator on the database.

type QueryTimer

type QueryTimer struct {
	sync.Mutex

	//TimeSum is the total sum of times that the given query ran
	TimeSum float64

	//TimeVarSum is the sum of squares of times that the given query ran
	TimeVarSum float64

	//NumQueries is the number of queries that were handled in the given time period
	NumQueries int32
}

QueryTimer holds timing statistics for a specific query

func GetQueryTimer

func GetQueryTimer(funcname string) *QueryTimer

GetQueryTimer gets a query timer. Simple

func (*QueryTimer) Add

func (qt *QueryTimer) Add(t time.Duration)

Add adds a new duration

func (*QueryTimer) Clear

func (qt *QueryTimer) Clear()

Clear resets the QueryTimer to reload data

func (*QueryTimer) Get

func (qt *QueryTimer) Get() (num int32, mean float64, variance float64)

Get gets the timer values for the given query

func (*QueryTimer) GetClear

func (qt *QueryTimer) GetClear() (num int32, mean float64, variance float64)

GetClear gets the internal variance, and then clears the values

type WebContext

type WebContext struct {
	Operator operator.Operator
	Writer   http.ResponseWriter
	Request  *http.Request
	Logger   *log.Entry
}

Provides utilities and information about the current web request.

func (*WebContext) GetDeviceName

func (wc *WebContext) GetDeviceName() string

Returns the device name submitted by the request

func (*WebContext) GetDevicePath

func (wc *WebContext) GetDevicePath() string

func (*WebContext) GetStreamName

func (wc *WebContext) GetStreamName() string

Returns the stream name submitted by the request

func (*WebContext) GetStreamPath

func (wc *WebContext) GetStreamPath() string

func (*WebContext) GetUsername

func (wc *WebContext) GetUsername() string

Returns the username submitted by the request

Jump to

Keyboard shortcuts

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