maintenance

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: BSD-3-Clause Imports: 6 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HTMLContentType = "text/html; charset=utf-8"
	TextContentType = "text/text; charset=utf-8"
	JSONContentType = "application/json; charset=utf-8"
)

Values for HTTP Content-Type header.

Functions

This section is empty.

Types

type FileStore

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

FileStore implements Store that manages maintenance status by existence of a specific file. If file exists maintenance is enabled, otherwise is disabled. This store persists maintenance state and provides a simple way to set maintenance on local filesystem with external tools.

func NewFileStore

func NewFileStore(filename string) *FileStore

NewFileStore creates a new instance of FileStore.

func (*FileStore) Off

func (s *FileStore) Off() (changed bool, err error)

Off disables maintenance.

func (*FileStore) On

func (s *FileStore) On() (changed bool, err error)

On enables maintenance.

func (*FileStore) Status

func (s *FileStore) Status() (on bool, err error)

Status returns true if maintenance is enabled.

type MemoryStore

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

MemoryStore implements Store that keeps data in memory.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new instance of MemoryStore.

func (*MemoryStore) Off

func (s *MemoryStore) Off() (changed bool, err error)

Off disables maintenance.

func (*MemoryStore) On

func (s *MemoryStore) On() (changed bool, err error)

On enables maintenance.

func (*MemoryStore) Status

func (s *MemoryStore) Status() (on bool, err error)

Status returns true if maintenance is enabled.

type Option

type Option func(*Service)

Option is a function that sets optional parameters to the Handler.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the function that will perform message logging. Default is slog.Default().

func WithStore

func WithStore(store Store) Option

WithStore sets Store to the Handler. If this option is not used, handler defaults to MemoryStore.

type Response

type Response struct {
	// Body will be returned if Handler is nil.
	Body    string
	Handler http.Handler
}

Response holds configuration for HTTP response during maintenance mode.

type Service

type Service struct {
	HTML Response
	JSON Response
	Text Response
	// contains filtered or unexported fields
}

Service implements http.Service interface to write a custom HTTP response during maintenance mode. It also provides JSON API handlers that can be used to check, set and remove maintenance mode.

func New

func New(options ...Option) (s *Service)

New creates a new instance of Handler. The first argument is the handler that will be executed when maintenance mode is off.

func (Service) HTMLHandler

func (s Service) HTMLHandler(h http.Handler) http.Handler

HTMLHandler is a HTTP middleware that should be used alongide HTML pages.

func (Service) JSONHandler

func (s Service) JSONHandler(h http.Handler) http.Handler

JSONHandler is a HTTP middleware that should be used alongide JSON-encoded responses.

func (Service) OffHandler

func (s Service) OffHandler(w http.ResponseWriter, r *http.Request)

OffHandler can be used in JSON-encoded HTTP API to disable maintenance.

func (Service) OnHandler

func (s Service) OnHandler(w http.ResponseWriter, r *http.Request)

OnHandler can be used in JSON-encoded HTTP API to enable maintenance. It returns HTTP Status Created if the maintenance is enabled. If the maintenance is already enabled, it returns HTTP Status OK.

func (Service) Status added in v0.2.9

func (s Service) Status() (on bool, err error)

Status returns whether the maintenance mode is enabled.

func (Service) StatusHandler

func (s Service) StatusHandler(w http.ResponseWriter, r *http.Request)

StatusHandler can be used in JSON-encoded HTTP API to check the status of maintenance.

func (Service) TextHandler

func (s Service) TextHandler(h http.Handler) http.Handler

TextHandler is a HTTP middleware that should be used alongide plaintext responses.

type Store

type Store interface {
	// Return true if maintenance is enabled.
	Status() (on bool, err error)
	// Enable maintenance and returns true if the state has changed.
	On() (changed bool, err error)
	// Disables maintenance and returns true if the state has changed.
	Off() (changed bool, err error)
}

Store defines methods that are required to check, set and remove information whether the maintenance is on of off. Usually only one boolean value is needed to be stored

Jump to

Keyboard shortcuts

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