server

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package server provides classes and functions for the HTTP server side of the middleware.

In particular, the package handles client sessions by producing credentials for logged user and by verifying these credentials for each request.

It is a wrapper around net/http.

Index

Constants

This section is empty.

Variables

View Source
var Ok bool

Whether the package is usable. May be false if there is no configuration for the package.

View Source
var SessionOptions gs.Options

SessionOptions reflects the configured options for sessions. Modifying it has no effect on the sessions generated by the package.

Functions

func AddSessionIdToPath

func AddSessionIdToPath(path *string, sessionId string)

AddSessionIdToPath adds a session id to a URL path. This function is meant to be used by HTTP clients and tests.

func Handle

func Handle(pattern string, handler Handler)

Handle registers the handler for the given pattern. See http.ServeMux for a description of the pattern format.

func HandleFunc

func HandleFunc(pattern string, fct HandleFunction)

Handle registers the handler function for the given pattern. See http.ServeMux for a description of the pattern format.

func MakeSessionId

func MakeSessionId() (string, error)

MakeSessionId create a new session id.

This is a low level function, made available for tests.

func NewSession

func NewSession(st gs.Store, opts *gs.Options, sessionId string, user User) (session *gs.Session)

NewSession creates a new session for the given user.

This is a low level function, made available for tests. Use SendLoginAccepted instead.

func SessionKeys

func SessionKeys() [][]byte

SessionKeys retrieves the session keys for test purpose.

This is a low level function, made available for tests.

func Start

func Start() (err error)

Start the server. Parameters are taken from the configuration.

Types

type HandleFunction

type HandleFunction = func(ctx context.Context, response Response, request *Request)

type Handler

type Handler interface {
	Handle(ctx context.Context, response Response, request *Request)
}

A Handler responds to an HTTP request.

The Handle method should read the Request then use Response's methods to send the response. The Context must be checked for completion and transmitted to all called functions.

As a convenience, if the Handle method panics with an HttpError then that error is send as response.

type HandlerFunc

type HandlerFunc HandleFunction

HandlerFunc wraps a function into a Handler.

func (HandlerFunc) Handle

func (self HandlerFunc) Handle(ctx context.Context, response Response, request *Request)

type HandlerWrapper

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

HandlerWrapper wraps a Handler into an http.Handler.

func NewHandlerWrapper

func NewHandlerWrapper(pattern string, handler Handler) HandlerWrapper

func (HandlerWrapper) ServeHTTP

func (self HandlerWrapper) ServeHTTP(wr http.ResponseWriter, original *http.Request)

ServeHTTP implements http.Handler.

type HttpError

type HttpError struct {
	// HTTP status code for the error.
	Code int
	// contains filtered or unexported fields
}

A HttpError is an error that can be send as an HTTP response.

func NewHttpError

func NewHttpError(code int, msg string, detail string) HttpError

NewHttpError constructs a new HttpError.

The code is to be sent as the HTTP code of the response. It should be a constant from the net/http package. The message (msg) is to be sent as body of the HTTP response. This is the public description of the error. The detail is the private description of the error, to be displayed in the logs.

func NewInternalHttpError

func NewInternalHttpError(err error) HttpError

NewInternalHttpError wraps another error into an InternalServerError HttpError. This function is particularly usefull to panic inside an Handler, see Handler.

func (HttpError) Error

func (self HttpError) Error() string

func (HttpError) Unwrap

func (self HttpError) Unwrap() error

type Request

type Request struct {
	// User is the session user.
	// It is nil if no user is successfully logged in the current session.
	User *User

	// SessionError is the error raised when checking the session informations send by the client.
	// It is nil either if the client did not send any session information (in which case User is nil
	// too) or if the session has been successfully checked (in which case Use is not nil).
	SessionError error

	// FullPath contains all path elements of the request made by the client.
	FullPath []string

	// RemainingPath contains the path elements after the pattern corresponding to the current
	// Handler.
	RemainingPath []string
	// contains filtered or unexported fields
}

A request represents an HTTP request to be handled by the server.

func (*Request) UnmarshalJSONBody

func (self *Request) UnmarshalJSONBody(dst interface{}) error

UnmarshalJSONBody retrieves the body of the request as a JSON object. See json.Unmarshal for details of the unmarshalling process.

type Response

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

func (Response) SendError

func (self Response) SendError(err error)

SendError sends an error as response. If err is an HttpError, its code and msg are used in the HTPP response. Also log the error.

func (Response) SendJSON

func (self Response) SendJSON(ctx context.Context, data interface{})

SendJSON sends a JSON as response. On success statuc code is http.StatusOK.

func (Response) SendLoginAccepted

func (self Response) SendLoginAccepted(ctx context.Context, user User, req *Request)

SendLoginAccepted create new credential for the user and send it as response.

type User

type User struct {
	Name string
	Id   uint32
}

User represents a logged user.

Directories

Path Synopsis
Package servertest provides methods and types to test server.Handler implementations.
Package servertest provides methods and types to test server.Handler implementations.

Jump to

Keyboard shortcuts

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