webutil

package
v0.0.0-...-f5f6071 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIResponse

type APIResponse[T any] struct {
	APIResponseMeta `json:"meta,omitempty"`
	Response        T `json:"response,omitempty"`
}

APIResponse is a generalized api response.

type APIResponseMeta

type APIResponseMeta struct {
	Status  int
	Message string
	Error   string
}

APIResponseMeta is metadata for an api response.

type GracefulServer

type GracefulServer struct {
	Server              *http.Server
	Listener            net.Listener
	ShutdownGracePeriod time.Duration
	OnStart             []func()
}

GracefulServer is a wrapper for an http server that implements the graceful interface.

func (*GracefulServer) RegisterOnStart

func (gs *GracefulServer) RegisterOnStart(fn func())

RegisterOnStart registers an on-start handler.

func (*GracefulServer) Start

func (gs *GracefulServer) Start(_ context.Context) (err error)

Start implements graceful.Graceful.Start. It is expected to block.

func (*GracefulServer) Stop

func (gs *GracefulServer) Stop(ctx context.Context) error

Stop implements graceful.Graceful.Stop.

type RewriteRule

type RewriteRule interface {
	Apply(string) (string, bool)
}

RewriteRule is a type that modifies a request.

type RewriteRuleFunc

type RewriteRuleFunc func(string) (string, bool)

RewriteRuleFunc is a function that rewrites a url.

func (RewriteRuleFunc) Apply

func (rrf RewriteRuleFunc) Apply(path string) (string, bool)

Apply applies the rewrite rule.

type StaticFileServer

type StaticFileServer struct {
	SearchPaths  []http.FileSystem
	RewriteRules []RewriteRule
	Headers      http.Header
}

StaticFileServer is a cache of static files. It can operate in cached mode, or with `CacheDisabled` set to `true` it will read from disk for each request. In cached mode, it automatically adds etags for files it caches.

func (*StaticFileServer) AddRegexPathRewrite

func (sc *StaticFileServer) AddRegexPathRewrite(match string, rewriteAction func(string, ...string) string) error

AddRegexPathRewrite adds a re-write rule that modifies the url path.

Typically these kinds of re-write rules are used for vanity forms or removing a cache busting string from a given path.

func (*StaticFileServer) ResolveFile

func (sc *StaticFileServer) ResolveFile(filePath string) (f http.File, finalPath string, err error)

ResolveFile resolves a file from rewrite rules and search paths.

First the file path is modified according to the rewrite rules. Then each search path is checked for the resolved file path.

func (*StaticFileServer) ServeHTTP

func (sc *StaticFileServer) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP is the entrypoint for the static server.

It adds default headers if specified, and then serves the file from disk.

Jump to

Keyboard shortcuts

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