srv

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrCodeMethodNotAllowed     = 1
	ErrCodeInternalServerError  = iota
	ErrCodeFailedToParseRequest = iota
	ErrCodeAccessDenied         = iota
	ErrCodeMax                  = 100
)

Common server error codes.

Variables

View Source
var (
	ErrMethodNotAllowed = &Error{
		Status:  http.StatusMethodNotAllowed,
		Code:    ErrCodeMethodNotAllowed,
		Message: "HTTP method not allowed",
	}
	ErrInternalServerError = &Error{
		Status:  http.StatusInternalServerError,
		Code:    ErrCodeInternalServerError,
		Message: "internal server error",
	}
	ErrFailedToParseRequest = &Error{
		Status:  http.StatusBadRequest,
		Code:    ErrCodeFailedToParseRequest,
		Message: "failed to parse request",
	}
	ErrAccessDenied = &Error{
		Status:  http.StatusForbidden,
		Code:    ErrCodeAccessDenied,
		Message: "access denied",
	}
)

Common server errors.

Functions

func GetURL

func GetURL(conf *Config, path string) string

GetURL returns a server URL for a given path.

func NewHTTPRequest

func NewHTTPRequest(conf *Config, method,
	path string, req *Request) (*http.Request, error)

NewHTTPRequest creates a new HTTP request from a given server request.

func NewHTTPRequestWithURL

func NewHTTPRequestWithURL(method, url string, req *Request) (*http.Request, error)

NewHTTPRequestWithURL creates a new HTTP request for a given url.

Types

type AuthFunc

type AuthFunc func(username, password string) bool

AuthFunc checks if a given username and password pair is correct.

type Config

type Config struct {
	Addr string
	TLS  *TLSConfig
}

Config is a server configuration.

func NewConfig

func NewConfig() *Config

NewConfig creates a default server configuration.

type Context

type Context struct {
	Username string
}

Context is a request context data.

type Error

type Error struct {
	Status  int    `json:"-"`
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Error is a server error.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request, ctx *Context)

HandlerFunc is an HTTP request handler function which receives additional context.

type Request

type Request struct {
	Args json.RawMessage `json:"args,omitempty"`
}

Request is a server request.

type Response

type Response struct {
	Error  *Error          `json:"error,omitempty"`
	Result json.RawMessage `json:"result,omitempty"`
}

Response is a server reply.

func Send

func Send(req *http.Request) (*Response, error)

Send sends an HTTP request and returns a server response.

type Server

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

Server is an HTTP server.

func NewServer

func NewServer(conf *Config) *Server

NewServer creates a new HTTP server.

func (*Server) Close

func (s *Server) Close() error

Close immediately closes the server making ListenAndServe() to return.

func (*Server) HandleFunc

func (s *Server) HandleFunc(pattern string, handler HandlerFunc)

HandleFunc registers a handler function for a given pattern.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts to listen and to serve requests.

func (*Server) Mux

func (s *Server) Mux() *http.ServeMux

Mux is an associated http.ServeMux instance.

func (*Server) ParseRequest

func (s *Server) ParseRequest(logger log.Logger,
	w http.ResponseWriter, r *http.Request, args interface{}) bool

ParseRequest parses request handling possible errors.

func (*Server) RequireBasicAuth

func (s *Server) RequireBasicAuth(logger log.Logger,
	handler HandlerFunc, auth AuthFunc) HandlerFunc

RequireBasicAuth wraps a given handler function inside a handler with basic access authentication.

func (*Server) RequireHTTPMethods

func (s *Server) RequireHTTPMethods(logger log.Logger,
	handler HandlerFunc, methods ...string) HandlerFunc

RequireHTTPMethods wraps a given handler function inside an HTTP method validating handler.

func (*Server) RespondError

func (s *Server) RespondError(logger log.Logger, w http.ResponseWriter, err *Error)

RespondError sends a response with a given error.

func (*Server) RespondResult

func (s *Server) RespondResult(logger log.Logger, w http.ResponseWriter,
	result interface{})

RespondResult sends a response with a given result.

type TLSConfig

type TLSConfig struct {
	CertFile string
	KeyFile  string
}

TLSConfig is a TLS configuration.

Jump to

Keyboard shortcuts

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