api

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultAllowHeader = "Access-Control-Allow-Origin, Content-Type, Authorization"
	AllowOriginHeader  = "Access-Control-Allow-Origin"
	AllowMethodsHeader = "Access-Control-Allow-Methods"
	AllowHeadersHeader = "Access-Control-Allow-Headers"
	AllowOriginValue   = "*"
)
View Source
const (
	HeaderContentType   = "Content-Type"
	TypeApplicationJson = "application/json"
	DefaultHttpStatus   = http.StatusOK
)
View Source
const (
	ServerDefaultTimeOut = 10 * time.Second
	ServerWriteTimeOut   = ServerDefaultTimeOut
	ServerReadTimeOut    = ServerDefaultTimeOut
)

Variables

This section is empty.

Functions

func BadRequest added in v1.3.0

func BadRequest(w http.ResponseWriter, err error)

func ParseJsonRequest added in v1.3.0

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

Types

type BodyOption

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

func (BodyOption) Apply

func (p BodyOption) Apply(response *Response)

type ErrorResponse added in v1.3.1

type ErrorResponse struct {
	Error string `json:"error"`
}

func NewErrorResponse added in v1.3.1

func NewErrorResponse(err error) *ErrorResponse

type HeaderOption

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

func (HeaderOption) Apply

func (h HeaderOption) Apply(response *Response)

type JsonBodyOption

type JsonBodyOption struct {
	ResponseOption
	// contains filtered or unexported fields
}

func (JsonBodyOption) Apply

func (jp JsonBodyOption) Apply(response *Response)

type MuxBuilder

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

func NewMuxBuilder

func NewMuxBuilder(logger *slog.Logger, opts ...MuxOption) *MuxBuilder

func (*MuxBuilder) Build

func (b *MuxBuilder) Build(r []*Route) *http.ServeMux

type MuxOption

type MuxOption func(*MuxBuilder)

func WithAllowHeaders

func WithAllowHeaders(h []string) MuxOption

func WithAllowOrigin

func WithAllowOrigin(o string) MuxOption

type Response

type Response struct {
	Status  int
	Headers map[string]string
	Body    []byte
}

func NewJsonResponse

func NewJsonResponse(v any, opts ...ResponseOption) *Response

func NewResponse

func NewResponse(opts ...ResponseOption) *Response

NewResponse creates a Response with default attributes: Status=200 Body=empty byte slice Headers=empty hashmap

func (*Response) Write

func (r *Response) Write(w http.ResponseWriter)

Write sends the Response's content to the http.ResponseWriter.

type ResponseOption

type ResponseOption interface {
	Apply(response *Response)
}

func WithBody

func WithBody(body []byte) ResponseOption

WithBody sets the response's body.

func WithContentTypeJson

func WithContentTypeJson() ResponseOption

WithContentTypeJson tells the Response its content-Type will be "application/json"

func WithHeader

func WithHeader(name, value string) ResponseOption

WithHeader adds a header to the Response. Invoke WithHeader as many times as you have headers to set.

func WithJsonBody

func WithJsonBody(v any) ResponseOption

WithJsonBody takes an object meant to be translated to JSON inside the response's body and sets the content-type to "application/json"

func WithStatus

func WithStatus(code int) ResponseOption

type Route

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

func NewRoute

func NewRoute(v Visibility, p string, h http.Handler) *Route

func NewRouteFunc

func NewRouteFunc(v Visibility, p string, f func(http.ResponseWriter, *http.Request)) *Route

type Server

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

func NewServer

func NewServer(l *slog.Logger, h http.Handler, a string) *Server

func (*Server) Listen

func (s *Server) Listen(ctx context.Context) error

type StatusOption

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

func (StatusOption) Apply

func (s StatusOption) Apply(response *Response)

type Visibility

type Visibility int
const (
	Public Visibility = iota
	Internal
)

Jump to

Keyboard shortcuts

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