setup

package
v1.24.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Middleware for handling CORS (cross origin requests) Warning: CORS requests needs to be whitelisted on ist.io ingress configuration Technically this middleware shouldn't be necessary but currently the ingress proxy forwards the CORS preflight request to the pod

Server infrastructure common for all microservices in the project. It contains the code that start and configures a HTTP and/or GRPC server correctly.

Setup implementation shared between all microservices. If this file is changed it will affect _all_ microservices in the monorepo (and this is deliberately so).

Setup implementation shared between all microservices. If this file is changed it will affect _all_ microservices in the monorepo (and this is deliberately so).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBasicAuthHandler

func NewBasicAuthHandler(basicAuth *BasicAuth, chainedHandler http.Handler) http.Handler

func Permanent added in v1.16.0

func Permanent(err error) error

func Run

func Run(ctx context.Context, config ServerConfig)

Types

type BackgroundFunc added in v1.16.0

type BackgroundFunc func(context.Context, *HealthReporter) error

type BackgroundTaskConfig

type BackgroundTaskConfig struct {
	// a function that triggers a graceful shutdown of all other resources after completion
	Run  BackgroundFunc
	Name string
	// optional
	Shutdown func(context.Context) error
}

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

type BasicAuthHandler

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

func (*BasicAuthHandler) ServeHTTP

func (h *BasicAuthHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type CORSMiddleware

type CORSMiddleware struct {
	PolicyFor   func(req *http.Request) *CORSPolicy
	NextHandler http.Handler
}

func (*CORSMiddleware) ServeHTTP

func (check *CORSMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type CORSPolicy

type CORSPolicy struct {
	AllowMethods     string
	AllowHeaders     string
	AllowOrigin      string
	AllowCredentials bool
	MaxAge           int
}

type GRPCConfig

type GRPCConfig struct {
	// required
	Port     string
	Register func(*grpc.Server)
	Opts     []grpc.ServerOption

	// optional
	Shutdown func(context.Context) error
}

type HTTPConfig

type HTTPConfig struct {
	// required
	Port     string
	Register func(*http.ServeMux)

	// optional
	BasicAuth *BasicAuth
	Shutdown  func(context.Context) error
}

type Health added in v1.16.0

type Health uint
const (
	HealthStarting Health = iota
	HealthReady
	HealthBackoff
	HealthFailed
)

func (Health) MarshalJSON added in v1.16.0

func (h Health) MarshalJSON() ([]byte, error)

func (Health) String added in v1.16.0

func (h Health) String() string

type HealthReporter added in v1.16.0

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

func (*HealthReporter) ReportHealth added in v1.16.0

func (r *HealthReporter) ReportHealth(health Health, message string)

func (*HealthReporter) ReportHealthTtl added in v1.16.0

func (r *HealthReporter) ReportHealthTtl(health Health, message string, ttl *time.Duration) *time.Time

ReportHealthTtl returns the deadline (for testing)

func (*HealthReporter) ReportReady added in v1.16.0

func (r *HealthReporter) ReportReady(message string)

func (*HealthReporter) Retry added in v1.16.0

func (r *HealthReporter) Retry(ctx context.Context, fn func() error) error

Retry allows background services to set up reliable streaming with backoff.

This can be used to create background tasks that look like this:

func Consume(ctx context.Context, hr *setup.HealthReporter) error {
	state := initState()
	return hr.Retry(ctx, func() error {
		stream, err := startConsumer()
		if err != nil {
			return err
		}
		hr.ReportReady("receiving")
		for {
			select {
			case <-ctx.Done(): return nil
			case ev := <-stream: handleEvent(state, event)
			}
		}
  })
}

In the example above, connecting to the consumer will be retried a few times with backoff. The number of retries is reset whenever ReportReady is called so that successful connection heal the service.

type HealthServer added in v1.16.0

type HealthServer struct {
	BackOffFactory func() backoff.BackOff
	Clock          func() time.Time
	// contains filtered or unexported fields
}

func (*HealthServer) IsReady added in v1.16.0

func (h *HealthServer) IsReady(name string) bool

func (*HealthServer) Reporter added in v1.16.0

func (h *HealthServer) Reporter(name string) *HealthReporter

func (*HealthServer) ServeHTTP added in v1.16.0

func (h *HealthServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ServerConfig added in v1.16.0

type ServerConfig struct {
	GRPC *GRPCConfig
	HTTP []HTTPConfig
	// BackgroundTasks are tasks that are running forever, like Pub/sub receiver. If they
	// finish, a graceful shutdown will be triggered.
	Background []BackgroundTaskConfig
	Shutdown   func(context.Context) error
}

Config contains configurations for all servers & tasks will be started. A startup order is not guaranteed.

Jump to

Keyboard shortcuts

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