server

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

README

REST api server

Deployment

SERVER_ADDR

SERVER_ADDR defines the address the Alexandria API service will listen to. This is required.

SERVER_ADDR=:443
TLS_CERT / TLS_KEY / TLS_CACERT / MTLS_ENABLED

TLS_CERT and TLS_KEY define the file path where the certificates used to serve https traffic are stored. These are optional.

When MTLS_ENABLED is set to true, TLS_CACERT defines the path to the CA certifcate of the client used to enable mutual TLS.

For the local dev environment, these are set to

TLS_CERT="/etc/certs/arcade.pem"
TLS_KEY="/etc/certs/arcade_key.pem"
TLS_CACERT="/etc/certs/rootCA.pem"
MTS_ENABLED="true"
ALLOWED_ORIGINS

ALLOWED_ORIGINS sets the allowed origins for CORS requests, as used in the 'Allow-Access-Control-Origin' HTTP header. Note: Passing in a string "*" will allow any domain.

ALLOWED_METHODS

ALLOWED_METHODS can be used to explicitly allow methods in the Access-Control-Allow-Methods header. This is a replacement operation so you must also pass GET, HEAD, and POST if you wish to support those methods.

ALLOWED_HEADERS

ALLOWED_HEADERS adds the provided headers to the list of allowed headers in a CORS request. This is an append operation so the headers Accept, Accept-Language, and Content-Language are always allowed. Content-Type must be explicitly declared if accepting Content-Types other than application/x-www-form-urlencoded, multipart/form-data, or text/plain.

PPROF_ENABLED

PPROF_ENABLED adds the pprof endpoints to the server. See: https://pkg.go.dev/net/http/pprof and https://go.dev/blog/pprof

PPROF_ENABLED="true"
READ_TIMEOUT

READ_TIMEOUT sets the server's read timeout. The default value is 5 seconds.

READ_TIMEOUT=5s
WRITE_TIMEOUT

WRITE_TIMEOUT sets the server's write timeout. The default value is 10 seconds.

WRITE_TIMEOUT=15s
SHUTDOWN_TIMEOUT

SHUTDOWN_TIMEOUT sets the server's shutdown timeout. The defailt value is 10 seconds.

SHUTDOWN_TIMEOUT=10s

Documentation

Overview

Package server provides an http server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Response

func Response(ctx context.Context, w http.ResponseWriter, err error)

Response writes an http error responses to the http.ResponseWriter.

Types

type Config added in v0.2.4

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

Config holds the configuration information for the restful api server.

func NewConfig added in v0.2.4

func NewConfig(prefix ...string) (Config, error)

NewConfig returns the configuration of restful api server.

func (Config) AllowedHeaders added in v0.2.4

func (c Config) AllowedHeaders() []string

AllowedHeaders returns a string of Allowed Headers passed to the CORS middleware.

func (Config) AllowedMethods added in v0.2.4

func (c Config) AllowedMethods() []string

AllowedMethods returns a string of Allowed Methods passed to the CORS middleware.

func (Config) AllowedOrigins added in v0.2.4

func (c Config) AllowedOrigins() []string

AllowedOrigins returns a string of Allowed Origins passed to the CORS middleware.

func (Config) MTLSEnabled added in v0.2.4

func (c Config) MTLSEnabled() bool

MTLSEnabled returns true if MTLS is enabled.

func (Config) PprofEnabled added in v0.2.11

func (c Config) PprofEnabled() bool

PprofEnabled returns true when pprof should be enabled.

func (Config) ReadTimeout added in v0.2.5

func (c Config) ReadTimeout() time.Duration

ReadTimeout returns the server read timeout.

func (Config) ServerAddr added in v0.2.4

func (c Config) ServerAddr() string

ServerAddr returns the network address the server will listen on.

func (Config) ShutdownTimeout added in v0.2.5

func (c Config) ShutdownTimeout() time.Duration

ShutdownTimeout returns the server shutdown timeout.

func (Config) TLSCACert added in v0.2.4

func (c Config) TLSCACert() string

TLSCACert returns the path of the CA certificate file.

func (Config) TLSCert added in v0.2.4

func (c Config) TLSCert() string

TLSCert returns the path of the certificate file.

func (Config) TLSKey added in v0.2.4

func (c Config) TLSKey() string

TLSKey returns the path of the certificate key file.

func (Config) ToOptions added in v0.2.7

func (c Config) ToOptions() []Option

ToOptions converts the configuration to a list of options.

func (Config) WriteTimeout added in v0.2.5

func (c Config) WriteTimeout() time.Duration

WriteTimeout returns the server write timeout.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option provides options for configuring the creation of a http server.

func WithAddr

func WithAddr(addr string) Option

WithAddr will configure the server with the listen address.

func WithCORSAllowedHeaders added in v0.2.5

func WithCORSAllowedHeaders(headers []string) Option

With CORSAllowedHeaders will configure the CORS allowed headers.

func WithCORSAllowedMethods added in v0.2.5

func WithCORSAllowedMethods(methods []string) Option

With CORSAllowedMethods will configure the CORS allowed methods.

func WithCORSAllowedOrigins added in v0.2.5

func WithCORSAllowedOrigins(origins []string) Option

With CORSAllowedOrigins will configure the CORS allowed origins.

func WithCORSOptions added in v0.2.4

func WithCORSOptions(c *cors.Options) Option

WithCORSOptions will configure the server with the CORS options.

func WithMTLSEnabled added in v0.2.4

func WithMTLSEnabled(enabled bool) Option

WithMTLSEnabled when set to true, enabled mutual TLS.

func WithReadTimeout added in v0.0.15

func WithReadTimeout(timeout time.Duration) Option

WithReadTimeout sets the http server read timeout.

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout sets the timout for shutting down the server.

func WithTLSCert added in v0.2.4

func WithTLSCert(cert, key string) Option

WithTLSCert provides the file name of the public TLS certificate and key.

func WithTLSClientCACert added in v0.2.4

func WithTLSClientCACert(cacert string) Option

WithTLSClientCACert provides the file name of the client CA public certificate.

func WithTLSConfig added in v0.2.4

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig will configure the server to require TLS.

func WithWriteTimeout added in v0.0.15

func WithWriteTimeout(timeout time.Duration) Option

WithWriteTimeout sets the http server read timeout.

type ResponseError

type ResponseError struct {
	// Status is the http status code applicable to this problem.
	Status int `json:"status"`

	// Detail is a human-readable explanation specific to this occurrence of
	// the problem.
	Detail string `json:"detail,omitempty"`
}

ResponseError provides additional information about problems encounted while performing an operation. See: https://jsonapi.org/format/#error-objects

swagger:response ResponseError

func (ResponseError) Error

func (e ResponseError) Error() string

Error translates the error to a string.

type Server

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

Server represents an HTTP server.

func New

func New(ctx context.Context, opts ...Option) (*Server, error)

New creates an HTTP server with and has not started to accept requests yet.

func (Server) Middleware

func (s Server) Middleware(mw ...mux.MiddlewareFunc)

Middleware installs the given middleware with the router.

func (Server) Name added in v0.2.8

func (s Server) Name() string

Name returns the name of the server.

func (*Server) Register

func (s *Server) Register(ctx context.Context, services ...Service)

Register associates the given services with the router.

func (Server) Serve

func (s Server) Serve(ctx context.Context) error

Serve accepts incoming connections. This is a blocking call and should be called in the context of a new goroutime.

func (Server) Shutdown

func (s Server) Shutdown(ctx context.Context)

Shutdown stops the http server gracefully without interrupting any active connections. It will, however, forcefully stop if the shutdown timeout expires while shutting down.

type Service

type Service interface {
	// Register will register this service with the given router.
	Register(router *mux.Router)

	// Name provides the name of the service.
	Name() string

	// Shutdown allows the service to stop any long running background processes it
	// may have.
	Shutdown(context.Context)
}

Service defines the methods required by the Server to register with the service with the router.

Jump to

Keyboard shortcuts

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