web

package module
v0.0.0-...-b88d7a6 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2021 License: MIT Imports: 15 Imported by: 0

README

web

A minimal library for creating webservers in go

It is built on gorilla mux and handles lifecycle management, jwt auth (if you want), http redirect, middleware, and endpoint specification if you want it to. The goal is to eliminate the need to rewrite a lot of plumbing while allowing you to customize the server for more complex usecases.

Why?

Because I often want to create a minimal webserver using gorilla mux and I found myself following this pattern quite a bit. I find that frameworks are often restrictive so this is not a framework. This is a library that is intended to help with basic plumbing for an http server. You can overwrite whatever you want (for example the router or middleware) with more complex logic if/when you want to.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	Prefix  bool
	Path    string
	Method  string
	Queries map[string]string
	Handler http.HandlerFunc
}

Endpoint ...

type Logger

type Logger interface {
	Infof(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
}

Logger is a logger you can use to optionally print out information

type Opt

type Opt func(s *serverBuilder) error

Opt is an option for configuring the rest server

func AddAllMiddleware

func AddAllMiddleware(allMiddleware ...mux.MiddlewareFunc) Opt

AddAllMiddleware adds a set of middleware to the server. Order matters here as first will be encountered first in a request.

func AddAuthedEndpoint

func AddAuthedEndpoint(authed ...Endpoint) Opt

AddAuthedEndpoint adds some authed endpoints to the server

func AddAuthedMiddleware

func AddAuthedMiddleware(authedMiddleware ...mux.MiddlewareFunc) Opt

AddAuthedMiddleware adds a set of middleware to the server. Order matters here as first will be encountered first in a request.

func AddEndpoint

func AddEndpoint(public ...Endpoint) Opt

AddEndpoint adds some public endpoints to the server

func AddPublicMiddleware

func AddPublicMiddleware(publicMiddleware ...mux.MiddlewareFunc) Opt

AddPublicMiddleware adds a set of middleware to the server. Order matters here as first will be encountered first in a request.

func CORSOptions

func CORSOptions(c cors.Options) Opt

CORSOptions configures cors options for the server

func DisableDefaultMiddleware

func DisableDefaultMiddleware(disable bool) Opt

DisableDefaultMiddleware will disable the server from adding request fingerprinting prometheus metrics for all routes, and jwt auth for authed routes

func HTTPPort

func HTTPPort(port int) Opt

HTTPPort configures the port the http redirect is served over

func HTTPSPort

func HTTPSPort(port int) Opt

HTTPSPort configures the port the https server serves on

func Handler

func Handler(handler http.Handler) Opt

Handler configures the rest handler that will route and respond to requests. Use this configuration if you want to route your own requests outside of this libary.

func JWTSigningCertPath

func JWTSigningCertPath(path string) Opt

JWTSigningCertPath configures the path to the public key that will be used to validate jwts. Defaults to the tls cert.

func JWTSigningKey

func JWTSigningKey(key *rsa.PrivateKey) Opt

JWTSigningKey configures the path to the private key that will be used to validate the jwts. Defaults to the tls private key.

func JWTSigningKeyPath

func JWTSigningKeyPath(path string) Opt

JWTSigningKeyPath configures the path to the private key that will be used to validate the jwts. Defaults to the tls private key.

func JWTSingingKeyPassphrase

func JWTSingingKeyPassphrase(passphrase string) Opt

JWTSingingKeyPassphrase configures the passphrase that is required to use the signing key (if there is one).

func SetLanding

func SetLanding(s string) Opt

SetLanding will set the top level home path for the server. Defaults to just the bare / If you are using a versioned api this will respect the versioning.

func SetLogger

func SetLogger(logger Logger) Opt

SetLogger sets a logger if you want one

func SetTimeout

func SetTimeout(d time.Duration) Opt

SetTimeout sets the http timeout for requests and responses. Defaults to 10 seconds.

func TLSCertPath

func TLSCertPath(path string) Opt

TLSCertPath configures the path to the tls certificate

func TLSKeyPath

func TLSKeyPath(path string) Opt

TLSKeyPath configures the path to the tls private key

type Server

type Server struct {
	*life.Life
	// contains filtered or unexported fields
}

Server is a wrapper around the gorilla mux http server that manages signals Note that I don't use life' lifecycle here because we have a blocking call for run (so I don't use life.Close or life.Done for managing the background thread. I use the server.ListenAndServe and server.Shutdown).

func NewServer

func NewServer(opts ...Opt) (s *Server, err error)

NewServer creates a new http server with a router. The reason why we pass through is because we are using a functional constructor and we don't want to pollute the main struct with intermediate config state

func (Server) Close

func (s Server) Close() error

Close closes the server down gracefully

func (Server) StartAndListen

func (s Server) StartAndListen()

StartAndListen synchronously will start the server and bypass the clean goRoutine handling that life provides. It will block while the server is listening

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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