Documentation ¶
Index ¶
- Constants
- Variables
- func DeserializeJSONRequestBody[T any](r *http.Request) (*T, error)
- func ErrorMapperAlreadyExistsError(err error) *api.Error
- func ErrorMapperDeserializeError(err error) *api.Error
- func ErrorMapperInvalidFieldMaskError(err error) *api.Error
- func ErrorMapperNotFoundError(err error) *api.Error
- func FromItem(item *item.Item) *api.Item
- func SerializeJSONResponse[T any](w http.ResponseWriter, statusCode int, data T)
- func WithPort(port string) func(*Server) error
- type DeserializeError
- type ErrorHandler
- type ErrorMapper
- type Option
- type Server
- func (s *Server) Handler() http.Handler
- func (s *Server) ItemsCreate(w http.ResponseWriter, r *http.Request)
- func (s *Server) ItemsGet(w http.ResponseWriter, r *http.Request, name string)
- func (s *Server) ItemsList(w http.ResponseWriter, r *http.Request, params api.ItemsListParams)
- func (s *Server) ItemsUpdate(w http.ResponseWriter, _ *http.Request, _ string)
- func (s *Server) ItemsUpsert(w http.ResponseWriter, r *http.Request)
- func (s *Server) ListenAndServer() error
- func (s *Server) Shutdown()
Constants ¶
Variables ¶
var ErrorCodeMapping = map[api.ErrorCode]int{ api.ParameterInvalid: http.StatusBadRequest, api.ParameterMissing: http.StatusBadRequest, api.ProcessingError: http.StatusInternalServerError, api.ResourceAlreadyExists: http.StatusConflict, api.ResourceMissing: http.StatusNotFound, }
Functions ¶
func DeserializeJSONRequestBody ¶
DeserializeJSONRequestBody reads the JSON-encoded request body from an HTTP request and deserializes it into a value of type T.
func ErrorMapperAlreadyExistsError ¶
ErrorMapperAlreadyExistsError maps a resource.AlreadyExistsError to an API error response.
func ErrorMapperDeserializeError ¶
ErrorMapperDeserializeError maps a resource.DeserializationError to an API error response.
func ErrorMapperInvalidFieldMaskError ¶
ErrorMapperInvalidFieldMaskError maps a fieldmask.InvalidFieldMaskError to an API error response.
func ErrorMapperNotFoundError ¶
ErrorMapperNotFoundError maps a resource.NotFoundError to an API error response.
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.
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 ¶
ErrorMapper is a function that maps an error to an API error response.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) ItemsCreate ¶
func (s *Server) ItemsCreate(w http.ResponseWriter, r *http.Request)
ItemsCreate creates a new item.
func (*Server) ItemsList ¶
func (s *Server) ItemsList(w http.ResponseWriter, r *http.Request, params api.ItemsListParams)
func (*Server) ItemsUpdate ¶
ItemsUpdate updates an item by name.
func (*Server) ItemsUpsert ¶
func (s *Server) ItemsUpsert(w http.ResponseWriter, r *http.Request)
func (*Server) ListenAndServer ¶
ListenAndServe starts the server.