server

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: MIT Imports: 12 Imported by: 0

README

little server

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteResponse

func WriteResponse(w http.ResponseWriter, response interface{})

WriteResponse does one of two things.

1. If the response parameter is a HTTPError its private message and status code will be logged, then the public message will be written to the response body.

2. If the response parameter is not a HTTPError it will be marshaled and then written to the response body.

Types

type Config

type Config struct {
	Domain string `json:"domain"`
	Port   string `json:"port"`
}

type HTTPError

type HTTPError struct {
	StatusCode     int
	PublicMessage  string
	PrivateMessage log.Fields
}

HTTPError stores a status code as well as a public and private message.

func NewHTTPError

func NewHTTPError(statusCode int, publicMessage string, privateMessage log.Fields) *HTTPError

NewHTTPError creates a new HTTPError.

type HTTPRequestInfo

type HTTPRequestInfo struct {
	StatusCode int
	Method     string
	URI        string
	Duration   time.Duration
	Length     int
	IPAddress  string
	Referer    string
	UserAgent  string
	Protocol   string
}

HTTPRequestInfo stores all the relevant information from an incoming HTTP request.

type HTTPResponse

type HTTPResponse struct {
	Data         interface{} `json:"data"`
	ErrorMessage string      `json:"errorMessage"`
}

HTTPResponse is for writing an outgoing response to a HTTP request, it stores response data and an optional error message.

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	StatusCode int
	Length     int
}

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(bytes []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

type Server

type Server struct {
	Router *mux.Router
	// contains filtered or unexported fields
}

func NewServer

func NewServer(config *Config) *Server

func (*Server) HeadersMiddleware

func (s *Server) HeadersMiddleware(next http.Handler) http.Handler

func (*Server) LoggingMiddleware

func (s *Server) LoggingMiddleware(next http.Handler) http.Handler

func (*Server) RegisterAction

func (s *Server) RegisterAction(path string, handler func(http.ResponseWriter, *http.Request), methods ...string)

func (*Server) RegisterAssetsDirectory

func (s *Server) RegisterAssetsDirectory(pathPrefix string, directory string)

func (*Server) RegisterHeader

func (s *Server) RegisterHeader(key, value string)

func (*Server) RegisterMiddleware

func (s *Server) RegisterMiddleware(middlewares ...mux.MiddlewareFunc)

func (*Server) Run

func (s *Server) Run()

func (*Server) SetNotFoundHandler

func (s *Server) SetNotFoundHandler(handler func(http.ResponseWriter, *http.Request))

Jump to

Keyboard shortcuts

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