server

package
v0.0.0-...-1612f9a Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: GPL-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ShutdownGracePeriod = 10 * time.Second
	DefaultPort         = 8080
	DefaultReadTimeout  = 5 * time.Second
	DefaultWriteTimeout = 10 * time.Second
)

Variables

Functions

func DeserializeJSONRequestBody

func DeserializeJSONRequestBody[T any](r *http.Request) (*T, error)

DeserializeJSONRequestBody reads the JSON-encoded request body from an HTTP request and deserializes it into a value of type T.

func ErrorMapperAlreadyExistsError

func ErrorMapperAlreadyExistsError(err error) *api.Error

ErrorMapperAlreadyExistsError maps a resource.AlreadyExistsError to an API error response.

func ErrorMapperDeserializeError

func ErrorMapperDeserializeError(err error) *api.Error

ErrorMapperDeserializeError maps a resource.DeserializationError to an API error response.

func ErrorMapperInvalidFieldMaskError

func ErrorMapperInvalidFieldMaskError(err error) *api.Error

ErrorMapperInvalidFieldMaskError maps a fieldmask.InvalidFieldMaskError to an API error response.

func ErrorMapperNotFoundError

func ErrorMapperNotFoundError(err error) *api.Error

ErrorMapperNotFoundError maps a resource.NotFoundError to an API error response.

func FromItem

func FromItem(item *item.Item) *api.Item

func SerializeJSONResponse

func SerializeJSONResponse[T any](w http.ResponseWriter, statusCode int, data T)

SerializeJSONResponse serializes the given data to JSON and writes it to the HTTP response. It sets the Content-Type header to "application/json" and the response status code to the provided statusCode.

func WithPort

func WithPort(port string) func(*Server) error

WithPort is an option that sets the port the server listens on.

Types

type DeserializeError

type DeserializeError struct {
	Err error
}

DeserializeError is an error type that wraps an error that occurred during deserialization.

func NewDeserializeError

func NewDeserializeError(err error) *DeserializeError

func (*DeserializeError) Error

func (e *DeserializeError) Error() string

type ErrorHandler

type ErrorHandler struct {
	Mappers map[reflect.Type]ErrorMapper
}

func NewErrorHandler

func NewErrorHandler() *ErrorHandler

NewErrorHandler returns a new instance of ErrorHandler.

func (*ErrorHandler) HandleError

func (h *ErrorHandler) HandleError(w http.ResponseWriter, _ *http.Request, err error)

HandleError handles an error by writing an appropriate response to the client.

func (*ErrorHandler) RegisterErrorMapper

func (h *ErrorHandler) RegisterErrorMapper(errType reflect.Type, mapper ErrorMapper)

RegisterErrorMapper registers an error mapper for a specific error type.

type ErrorMapper

type ErrorMapper func(error) *api.Error

ErrorMapper is a function that maps an error to an API error response.

type Option

type Option func(*Server) error

type Server

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

func New

func New(
	logger *slog.Logger,
	itemService *item.Service,
	middlewares []func(http.Handler) http.Handler,
	opts ...Option,
) (*Server, error)

func (*Server) Handler

func (s *Server) Handler() http.Handler

func (*Server) ItemsCreate

func (s *Server) ItemsCreate(w http.ResponseWriter, r *http.Request)

ItemsCreate creates a new item.

func (*Server) ItemsGet

func (s *Server) ItemsGet(w http.ResponseWriter, r *http.Request, name string)

ItemsGet retrieves an item by name.

func (*Server) ItemsList

func (s *Server) ItemsList(w http.ResponseWriter, r *http.Request, params api.ItemsListParams)

func (*Server) ItemsUpdate

func (s *Server) ItemsUpdate(w http.ResponseWriter, _ *http.Request, _ string)

ItemsUpdate updates an item by name.

func (*Server) ItemsUpsert

func (s *Server) ItemsUpsert(w http.ResponseWriter, r *http.Request)

func (*Server) ListenAndServer

func (s *Server) ListenAndServer() error

ListenAndServe starts the server.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown gracefully shuts down the underlying server without interrupting any active connections.

Directories

Path Synopsis
Package middleware contains application specific middleware.
Package middleware contains application specific middleware.

Jump to

Keyboard shortcuts

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