service

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 16 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxRetries = 10

MaxRetries defines the number of times wait tries to connect to the database. Zero means unlimited retries.

View Source
var Wait = 2 * time.Second

Wait defines the time to wait between retries for the database connection.

Functions

func Close

func Close()

Close closes the database pool.

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, s int, f string, args ...interface{})

ErrorResponse writes an error response. It sets the according response header and sends a json-formatted response object.

func GZIPJSONResponse added in v0.2.9

func GZIPJSONResponse(w http.ResponseWriter, data interface{})

GZIPJSONResponse writes a gzipped json-formatted response. Any errors are being logged.

func Init

func Init(dsn string) error

Init sets up the database connection pool using the supplied DSN `user:pass@proto(host/dbname`. Init waits for the databsase to be online. It is not save to call Init from different go routines.

func InitDebug added in v0.4.3

func InitDebug(dsn string, debug bool) error

InitDebug sets up the database connection pool using the supplied DSN `user:pass@proto(host/dbname` and sets the log level to debug if debug=true. It then calls Init(dsn) and returns its result.

func IsValidStatus

func IsValidStatus(r *http.Response, codes ...int) bool

IsValidStatus returns true if the given response has any of the given status codes.

func JSONResponse

func JSONResponse(w http.ResponseWriter, data interface{})

JSONResponse writes a json-formatted response. Any errors are being logged.

func LinkOrCopy added in v0.2.5

func LinkOrCopy(src, dest string) (err error)

LinkOrCopy tries to hard link dest to src. If the file or link already exists nothing is done and no error is returned. If the linking fails, LinkOrCopy tries to copy src to dest.

func ParseIDs

func ParseIDs(url string, re *regexp.Regexp, ids ...*int) int

ParseIDs parses the numeric fields of the given regex into the given id pointers. It returns the number of ids parsed.

func Pool

func Pool() db.DB

Pool returns the database connection pool that was initialized with Init.

func WithLog

func WithLog(f http.HandlerFunc) http.HandlerFunc

WithLog wraps logging around the handling of the request.

func WithMethods

func WithMethods(args ...interface{}) http.HandlerFunc

WithMethods dispatches a given pair of the request methods to the given HandlerFunc with an empty data context. The first element must be of type string, the second argument must be of type HandlerFunc. The function panics if it encounteres an invalid type.

Types

type Data

type Data struct {
	Session *api.Session   // authentification information
	Project *db.Project    // requested project
	Post    interface{}    // post data
	IDs     map[string]int // ids
}

Data defines the payload data for request handlers.

type HandlerFunc

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)

HandlerFunc defines the callback function to handle callbacks with data.

func WithAuth

func WithAuth(f HandlerFunc) HandlerFunc

WithAuth checks if the given request contains a valid authentication token. If not an appropriate error is returned before the given callback function is called. If the authentification succeeds, the session is put into the context as "auth".

func WithIDs added in v0.2.5

func WithIDs(f HandlerFunc, keys ...string) HandlerFunc

WithIDs fills the IDs map with the given (key,id) pairs or returns status not found if the given ids could not be parsed in the given order. You can prefix a key with `?` to mark it optional (the key is then not inserted intot the ID map if it is not present in the request's URL).

func WithProject

func WithProject(f HandlerFunc) HandlerFunc

WithProject loads the project data for the given project id and puts it into the context using "project" as key. Then it calls the given handler function.

func WithUser added in v0.5.0

func WithUser(f HandlerFunc) HandlerFunc

WithUser extracts the "/users/<numeric id>" part from the url, loads it and puts it into the context with the key "user".

Jump to

Keyboard shortcuts

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