Documentation
¶
Index ¶
- Constants
- type Config
- type Context
- func (ctx *Context) GetRequestBody() ([]byte, error)
- func (ctx *Context) GetService(name string) interface{}
- func (ctx *Context) GetWriter() io.Writer
- func (ctx *Context) LogError(msg string)
- func (ctx *Context) LogInfo(msg string)
- func (ctx *Context) SendAndLogError(code int, message string, data string)
- func (ctx *Context) SendGenericResponse(code int, response []byte, contentType string) error
- func (ctx *Context) SendHTMLResponse(code int, response []byte) error
- func (ctx *Context) SendJSONResponse(code int, response []byte) error
- func (ctx *Context) SendResponseHeader(header string, value string)
- func (ctx *Context) SetRequestBodyManually(body []byte)
- type Controller
- type ControllerProvider
- type DBconfig
- type JSONWebKeys
- type Jwks
- type Metric
- type Repository
- type Server
- type StatusInformation
Constants ¶
const ( ConfigDBURI = "db_uri" ConfigDBMaxConn = "db_max_connections" )
Config property names
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 ¶
CreateConfig creates a new Config object and initializes it with the given config file ref.
func (*Config) GetDuration ¶
GetDuration fetches the property as string and attempts at parsing it as duration string
func (*Config) GetInt ¶
GetInt fetches the property as string and attempts at parsing it as duration string
func (*Config) LoadProperties ¶
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 ¶
GetRequestBody readx the full body and returns it
func (*Context) GetService ¶
GetService retrieves the specified service by name
func (*Context) SendAndLogError ¶
SendAndLogError helper function that is specialized in sending back an error response to the client
func (*Context) SendGenericResponse ¶
SendGenericResponse sends the response using the context if the response was not sent already.
func (*Context) SendHTMLResponse ¶
SendHTMLResponse uses SendGenericResponse but sets the content type to text/html
func (*Context) SendJSONResponse ¶
SendJSONResponse uses SendGenericResponse but sets the content type to application/json
func (*Context) SendResponseHeader ¶
SendResponseHeader sends the response header
func (*Context) SetRequestBodyManually ¶
SetRequestBodyManually sets the body. Mainly used for testing
type Controller ¶
type Controller struct {
Name string
Metric string
Path string
Method string
IsSecured bool
AuthFunc func(ctx *Context, w http.ResponseWriter, r *http.Request) error
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 (ctr *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 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 Repository ¶
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 ¶
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.
type StatusInformation ¶
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