httpserver

package
v0.0.0-...-8c16a7d Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInternalServer = &Error{
	Status: http.StatusInternalServerError,
	Code:   "INTERNAL_SERVER_ERROR",
	Desc:   "Internal Server Error",
}

ErrInternalServer is the default err for server side failures

Functions

func ReadJSON

func ReadJSON(r *http.Request, v any) error

ReadJSON reads the http.Request body and attempts to parse it into the desired type v. If read fails or parsing fails, it returns an error. v here should be a pointer to the actual type.

func WriteJSON

func WriteJSON(ctx context.Context, w http.ResponseWriter, v interface{}, headers map[string]string)

WriteJSON parses the given v to JSON equivalent and writes it to the http.ResponseWriter. It also sets the relevant headers such as status, Content-Type and Content-Length.

Types

type Error

type Error struct {
	// Status is the http status. This should be >= 400.
	Status int `json:"-"`
	// Code is the error code that will be printed in the json response
	Code string `json:"code"`
	// Description is the error description that will be printed in the json response
	Desc string `json:"description"`
}

Error represents an HTTP Error

func (Error) Error

func (e Error) Error() string

Error satisfies the error interface and returns the error details in string representation

type Router

type Router struct {
	ProfilingEnabled     bool
	ReadinessHandlerFunc http.HandlerFunc
	RESTRoutes           func(chi.Router)
	GQLHandler           http.Handler
}

func (Router) Handler

func (rtr Router) Handler() (chi.Router, error)

type Server

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

Server is the server instance

func New

func New(ctx context.Context, rtr Router, options ...ServerOption) (*Server, error)

New returns a new instance of Server.

func (*Server) Start

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

Start starts the server and is context aware and shuts down when the context gets cancelled.

type ServerOption

type ServerOption = func(*Server) error

ServerOption customizes the Server

func WithServerPort

func WithServerPort(port int) ServerOption

WithServerPort sets the server port to the given port

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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