api

package
v0.0.0-...-7c6bf0a Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequestError

func BadRequestError(w http.ResponseWriter, err error)

BadRequestError writes the provided error along with a 400 http status.

func Error

func Error(w http.ResponseWriter, err error, httpStatus int)

Error writes the provided error along with the provided http status.

func NotFoundError

func NotFoundError(w http.ResponseWriter, err error)

NotFoundError writes the provided error along with a 404 http status.

func Ok

func Ok(w http.ResponseWriter, response interface{}, httpStatus int)

Ok encodes to JSON and writes the provided response (if any) along with the httpStatus.

func ServerError

func ServerError(w http.ResponseWriter, err error)

ServerError writes the provided error along with a 500 http status.

func ValidationError

func ValidationError(w http.ResponseWriter, err error)

ValidationError writes the provided error along with a 422 http status. If the error cannot be marshalled, a 500 error is returned instead.

Types

type ApiError

type ApiError struct {
	Message string  `json:"message"`
	Errors  []error `json:"errors,omitempty"`
}

ApiError defines the standard error the API returns.

type ErrorWrapper

type ErrorWrapper struct {
	Error ApiError `json:"error"`
}

ErrorWrapper wraps an ApiError, so consumers can check for it easier.

type Server

type Server struct {
	Router      *http.ServeMux
	UserService *user.Service
	Logger      *slog.Logger
}

The Server is used as a container for the most important dependencies.

func NewServer

func NewServer(userService *user.Service, logger *slog.Logger) *Server

NewServer returns a pointer to a new Server.

func (*Server) CreateRoutes

func (s *Server) CreateRoutes()

CreateRoutes creates all app routes.

func (*Server) EnableCORS

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

func (*Server) HandleAddUser

func (s *Server) HandleAddUser() http.HandlerFunc

HandleAddUser handles the "POST /users" endpoint.

func (*Server) HandleCheckHealth

func (s *Server) HandleCheckHealth() http.HandlerFunc

HandleCheckHealth is used for checking if the service is healthy.

func (*Server) HandleCheckLive

func (s *Server) HandleCheckLive() http.HandlerFunc

HandleCheckLive is used for checking if the service is up.

func (*Server) HandleGetUser

func (s *Server) HandleGetUser() http.HandlerFunc

HandleGetUser handles the "GET /users/{id}" endpoint.

func (*Server) HandleGetUsers

func (s *Server) HandleGetUsers() http.HandlerFunc

HandleGetUsers handles the "GET /users" endpoint.

func (*Server) LimitBody

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

LimitBody limits the size of incoming request body to 1MB.

Jump to

Keyboard shortcuts

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