chttp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 22 Imported by: 25

Documentation

Overview

Package chttp helps setup a http server with routing, middlewares, and more.

Index

Constants

This section is empty.

Variables

View Source
var URLParams = mux.Vars

URLParams returns the route variables for the current request, if any

WireModule can be used as part of google/wire setup.

Functions

func NewHandler

func NewHandler(p NewHandlerParams) http.Handler

NewHandler creates a http.Handler with the given routes and middlewares. The handler can be used with a http.Server or as an argument to StartServer.

Types

type HTMLDir

type HTMLDir fs.FS

HTMLDir is a directory that can be embedded or found on the host system. It should contain sub-directories and files to support the WriteHTML function in ReaderWriter.

type HTMLRenderer

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

HTMLRenderer provides functionality in rendering templatized HTML along with HTML components

func NewHTMLRenderer

func NewHTMLRenderer(p NewHTMLRendererParams) (*HTMLRenderer, error)

NewHTMLRenderer creates a new HTMLRenderer with HTML templates stored in dir and registers the provided HTML components

type HTMLRouter

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

HTMLRouter provides a route that exposes the StaticDir and handles server-side HTML components

func NewHTMLRouter

func NewHTMLRouter(p NewHTMLRouterParams) (*HTMLRouter, error)

NewHTMLRouter instantiates a new HTMLRouter

func (*HTMLRouter) Routes

func (ro *HTMLRouter) Routes() []Route

Routes defines the /static/ route that serves the StaticDir

type Middleware

type Middleware interface {
	Handle(next http.Handler) http.Handler
}

Middleware is a function that takes in a http.Handler and returns one as well. It allows you to execute code before or after calling the handler.

type NewHTMLRendererParams

type NewHTMLRendererParams struct {
	HTMLDir   HTMLDir
	StaticDir StaticDir
	AppConfig cconfig.Config
}

NewHTMLRendererParams holds the params needed to create HTMLRenderer

type NewHTMLRouterParams

type NewHTMLRouterParams struct {
	StaticDir StaticDir
	RW        *ReaderWriter
	HTML      *HTMLRenderer
	AppConfig cconfig.Config
	Logger    clogger.Logger
}

NewHTMLRouterParams holds the params needed to instantiate a new HTMLRouter

type NewHandlerParams

type NewHandlerParams struct {
	Routers           []Router
	GlobalMiddlewares []Middleware
}

NewHandlerParams holds the params needed for NewHandler.

type NewServerParams

type NewServerParams struct {
	Handler   http.Handler
	Lifecycle *copper.Lifecycle
	Config    cconfig.Config
	Logger    clogger.Logger
}

NewServerParams holds the params needed to create a server.

type ReaderWriter

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

ReaderWriter provides functions to read data from HTTP requests and write response bodies in various formats

func NewReaderWriter

func NewReaderWriter(renderer *HTMLRenderer, config cconfig.Config, logger clogger.Logger) *ReaderWriter

NewReaderWriter instantiates a new ReaderWriter with its dependencies

func (*ReaderWriter) ReadJSON

func (rw *ReaderWriter) ReadJSON(w http.ResponseWriter, req *http.Request, body interface{}) bool

ReadJSON reads JSON from the http.Request into the body var. If the body struct has validate tags on it, the struct is also validated. If the validation fails, a BadRequest response is sent back and the function returns false.

func (*ReaderWriter) WriteHTML

func (rw *ReaderWriter) WriteHTML(w http.ResponseWriter, r *http.Request, p WriteHTMLParams)

WriteHTML writes an HTML response to the provided http.ResponseWriter. Using the given WriteHTMLParams, the HTML is generated with a layout, page, and component templates.

func (*ReaderWriter) WriteHTMLError

func (rw *ReaderWriter) WriteHTMLError(w http.ResponseWriter, r *http.Request, err error)

WriteHTMLError handles the given error. In dev environment, it writes an HTML page with the error. Errors are logged in all environments.

func (*ReaderWriter) WriteJSON

func (rw *ReaderWriter) WriteJSON(w http.ResponseWriter, p WriteJSONParams)

WriteJSON writes a JSON response to the http.ResponseWriter. It can be configured with status code and data using WriteJSONParams.

type RequestLoggerMiddleware

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

RequestLoggerMiddleware logs each request's HTTP method, path, and status code along with user uuid (from basic auth) if any.

func NewRequestLoggerMiddleware

func NewRequestLoggerMiddleware(logger clogger.Logger) *RequestLoggerMiddleware

NewRequestLoggerMiddleware creates a new RequestLoggerMiddleware.

func (*RequestLoggerMiddleware) Handle

func (mw *RequestLoggerMiddleware) Handle(next http.Handler) http.Handler

Handle wraps the current request with a request/response recorder. It records the method path and the return status code. It logs this with the given logger.

type Route

type Route struct {
	Middlewares []Middleware
	Path        string
	Methods     []string
	Handler     http.HandlerFunc
}

Route represents a single HTTP route (ex. /api/profile) that can be configured with middlewares, path, HTTP methods, and a handler.

type Router

type Router interface {
	Routes() []Route
}

Router is used to group routes together that are returned by the Routes method.

type Server

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

Server represents a configurable HTTP server that supports graceful shutdown.

func NewServer

func NewServer(p NewServerParams) *Server

NewServer creates a new server.

func (*Server) Run

func (s *Server) Run() error

Run configures an HTTP server using the provided app config and starts it.

type StaticDir

type StaticDir fs.FS

StaticDir represents a directory that holds static resources (JS, CSS, images, etc.)

type WriteHTMLParams

type WriteHTMLParams struct {
	StatusCode     int
	Error          error
	Data           interface{}
	PageTemplate   string
	LayoutTemplate string
}

WriteHTMLParams holds the params for the WriteHTML function in ReaderWriter

type WriteJSONParams

type WriteJSONParams struct {
	StatusCode int
	Data       interface{}
}

WriteJSONParams holds the params for the WriteJSON function in ReaderWriter

Directories

Path Synopsis
Package chttptest provides utility functions that are useful when testing chttp
Package chttptest provides utility functions that are useful when testing chttp

Jump to

Keyboard shortcuts

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