server

package
v0.0.0-...-8692103 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package server implements the HTTP server that will respond to the requests for URLs, sending the user to the appropriate location (or rejecting the response)

Index

Constants

View Source
const (
	CtxErrors key = "error"
)

Ctx* are context value keys

Variables

View Source
var (
	ErrFailedToApplyOption = errors.New("failed to apply option")
	ErrFailedToStart       = errors.New("failed to start server")
)

Err* are sentinel errors

View Source
var ErrDependencyFailure = errors.New("dependency failure")

ErrDependencyFailure just means there was a failure resolving a dependency

View Source
var (
	ProblemUnknown = problem.New(
		problem.Status(http.StatusInternalServerError),
		problem.Title("An unexpected error has occurred"),
		problem.Detail("The server has encountered an unexpected error. There's nothing, as a user, you can do. Please try again later"))
)

Problem* are common types of problems

Functions

func Error

func Error(next http.Handler) http.Handler

Error cancels the request processing

func Intercept

func Intercept(matches MatcherFunc, intercept http.Handler) func(next http.Handler) http.Handler

Intercept is a type of middleware that offloads messages that are destined for the "default" handler and instead redirects them to some other handler.

This allows using more complex matching logic. See IsGRPCGateway for an example.

func IsGRPC

func IsGRPC(r *http.Request) bool

IsGRPC offloads requests to the gRPC mux. Note: This does not use a bunch of GRPC features; that's fine.

See - https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go#L51-L61 - https://ahmet.im/blog/grpc-http-mux-go/

func IsH2C

func IsH2C(r *http.Request) bool

IsH2C does the detection of the initial message. The message looks like:

PRI * HTTP/2.0

SM

<Byte Data>

(At least, as reproduced by $ curl --http2-prior-knowledge). See: 1. https://www.rfc-editor.org/rfc/rfc7540#section-4.1

func New

func New(opts ...Option) (*http.Server, error)

New creates a server instance, configured appropriately

func WireServer

func WireServer() (*http.Server, error)

func WithError

func WithError(r *http.Request, err error)

WithError adds the error to the current request. Middleware later picks it out, and writes out the status.

https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/http/server.go;l=2141-2150 https://github.com/go-chi/render/blob/14f1cb3d5c2969d6e462632a205eacb6421eb4dc/responder.go#L25-L26

Types

type MatcherFunc

type MatcherFunc func(*http.Request) bool

MatcherFunc is a function that can be used by the interceptor to match requests.

func AllOf

func AllOf(matchers ...MatcherFunc) MatcherFunc

AllOf combines multiple matchers into a single matcher func

func IsHost

func IsHost(host string) MatcherFunc

IsHost matches whether or not a request matches a specific host

type Option

type Option func(*http.Server) error

Option is a function type that modifies the behavior of the server

func ResolveOptions

func ResolveOptions() ([]Option, error)

ResolveOptions generates a server with the appropriate configuration, based on Viper and other required dependencies

func WithGRPC

func WithGRPC(host string, server *grpc.Server) Option

WithGRPC enables GRPC to be served over the

func WithH2C

func WithH2C() Option

WithH2C allows piping the connection to a HTTP/2 server, which will hijack the request to use the HTTP/2 protocol but over the initially supplied connection.

func WithListenAddress

func WithListenAddress(addr string) Option

WithListenAddress indicates the server should start on the specific address

func WithMiddleware

func WithMiddleware(m func(next http.Handler) http.Handler) Option

WithMiddleware appends middleware to the default handler

func WithStorage

func WithStorage(str storage.Storer) Option

WithStorage allows starting the service with a specific storage engine.

Directories

Path Synopsis
Package message provides common utilities for working with HTTP messages.
Package message provides common utilities for working with HTTP messages.

Jump to

Keyboard shortcuts

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