server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConfigDBURI     = "db_uri"
	ConfigDBMaxConn = "db_max_connections"
)

Config property names

View Source
const (
	ConfigPort         = "port"
	ConfigReadTimeout  = "readTimeout"
	ConfigWriteTimeout = "writeTimeout"
)

All config properties required for the server to run

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config Central access point to all config properties

func CreateConfig

func CreateConfig(path string, name string, properties []string) Config

CreateConfig creates a new Config object and initializes it with the given config file ref.

func (*Config) Get

func (c *Config) Get(property string) string

Get returns a cached property value. Panics if the property doesn't exist

func (*Config) GetDuration

func (c *Config) GetDuration(property string) (time.Duration, error)

GetDuration fetches the property as string and attempts at parsing it as duration string

func (*Config) GetInt

func (c *Config) GetInt(property string) int

GetInt fetches the property as string and attempts at parsing it as duration string

func (*Config) LoadProperties

func (c *Config) LoadProperties(properites []string)

LoadProperties attempts to load all provided properties from the config file into memory

type Context

type Context struct {
	Server            *Server
	Request           *http.Request
	Writer            http.ResponseWriter
	Config            *Config
	Repository        *Repository
	IsResponseSent    bool
	StatusInformation *StatusInformation

	RequestID uuid.UUID
	Subdomain string
	// contains filtered or unexported fields
}

Context intantiated for every request

func (*Context) GetRequestBody

func (ctx *Context) GetRequestBody() ([]byte, error)

GetRequestBody readx the full body and returns it

func (*Context) GetService

func (ctx *Context) GetService(name string) interface{}

GetService retrieves the specified service by name

func (*Context) GetWriter

func (ctx *Context) GetWriter() io.Writer

GetWriter provides response writer or testWriter if in test context

func (*Context) LogError

func (ctx *Context) LogError(msg string)

LogError logs an error

func (*Context) LogInfo

func (ctx *Context) LogInfo(msg string)

LogInfo logs an error

func (*Context) SendAndLogError

func (ctx *Context) SendAndLogError(code int, message string, data string)

SendAndLogError helper function that is specialized in sending back an error response to the client

func (*Context) SendGenericResponse

func (ctx *Context) SendGenericResponse(code int, response []byte, contentType string) error

SendGenericResponse sends the response using the context if the response was not sent already.

func (*Context) SendHTMLResponse

func (ctx *Context) SendHTMLResponse(code int, response []byte) error

SendHTMLResponse uses SendGenericResponse but sets the content type to text/html

func (*Context) SendJSONResponse

func (ctx *Context) SendJSONResponse(code int, response []byte) error

SendJSONResponse uses SendGenericResponse but sets the content type to application/json

func (*Context) SendResponseHeader

func (ctx *Context) SendResponseHeader(header string, value string)

SendResponseHeader sends the response header

func (*Context) SetRequestBodyManually

func (ctx *Context) SetRequestBodyManually(body []byte)

SetRequestBodyManually sets the body. Mainly used for testing

type Controller

type Controller struct {
	Name           string
	Metric         string
	Path           string
	Method         string
	IsSecured      bool
	ControllerFunc func(ctx *Context)
}

Controller base type of all controllers

var StatusController Controller = Controller{
	Name:      "StatusController",
	Metric:    "status",
	Path:      "/status",
	Method:    "GET",
	IsSecured: false,
	ControllerFunc: func(ctx *Context) {
		stats := &ctx.StatusInformation.Stats
		html := strings.Builder{}
		html.WriteString("<html>This is a status page. <br/><br/>")
		for _, ctr := range ctx.Server.GetControllers() {
			html.WriteString(fmt.Sprintf("Total number of %s: %d<br/>", ctr.Name, (*stats)[ctr.Metric]))
		}
		fmt.Fprintf(ctx.GetWriter(), html.String())
		return
	},
}

StatusController shows status page

func (*Controller) Execute

func (c *Controller) Execute(ctx *Context)

Execute executes the controller in the given context

type ControllerProvider

type ControllerProvider interface {
	GetControllers() []Controller
}

ControllerProvider Interfice providing access the list of controllers from another module

type DBconfig

type DBconfig struct {
	DbURI              string
	DbMaxDBConnections int
}

DBconfig holds all relevant db configurations

type JSONWebKeys

type JSONWebKeys struct {
	Kty string   `json:"kty"`
	Kid string   `json:"kid"`
	Use string   `json:"use"`
	N   string   `json:"n"`
	E   string   `json:"e"`
	X5c []string `json:"x5c"`
}

JSONWebKeys used to marshal the key response from Auth0

type Jwks

type Jwks struct {
	Keys []JSONWebKeys `json:"keys"`
}

Jwks used to marshal the key response from Auth0

type Metric

type Metric string

Metric enum of all available metrics

type Repository

type Repository struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

Repository provides access to the DB and prepared statements

func CreateRepository

func CreateRepository(c *Config) *Repository

CreateRepository intizializes a repository

func (*Repository) RunMigrations

func (r *Repository) RunMigrations(entities []interface{})

RunMigrations migrates all provided entities which must be gorm compatible entities

type Server

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

Server Generic server who is able to load a list of controllers from multiple ControllerProviders

func CreateServer

func CreateServer(config Config, ctrProviders []ControllerProvider) *Server

CreateServer Factory method to create a Server instance. This is meant to be used by main methods and provide the list of ControllerProviders the server instance is supposed to serve

func (*Server) GetControllers

func (s *Server) GetControllers() []Controller

GetControllers returns all controllers of the controller provider

func (*Server) RegisterService

func (s *Server) RegisterService(name string, service interface{})

RegisterService registers a service to the server

func (*Server) SetRepository

func (s *Server) SetRepository(repo *Repository)

SetRepository sets the repository if one is being used.

func (*Server) Start

func (s *Server) Start()

Start starts the previously initialized server

type StatusInformation

type StatusInformation struct {
	Stats map[string]int
	// contains filtered or unexported fields
}

StatusInformation struct holding all status metrics. Meant to be instantiated once per server

func CreateStatusInfo

func CreateStatusInfo() *StatusInformation

CreateStatusInfo factory to create a new status info and start thread to process incoming metrics

func (*StatusInformation) IncrementMetric

func (s *StatusInformation) IncrementMetric(m string)

IncrementMetric increments given metric by one. Threadsafe

Jump to

Keyboard shortcuts

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