api

package
v0.0.0-...-f464bf3 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package api exposes the main API engine.

To use this package, you should create a new instance with New() passing a valid Config. The resulting Router will have the Router.Handler() function that returns a handler that can be used in a http.Server (or in other middlewares).

Example:

// Create the API router
apirouter, err := api.New(api.Config{
	Logger:   logger
})
if err != nil {
	logger.WithError(err).Error("error creating the API server instance")
	return fmt.Errorf("error creating the API server instance: %w", err)
}
handler := apirouter.Handler(
	handlerCfg,
	controllers...,
)

// Create the API server
apiserver := http.Server{
	Addr:              cfg.Web.APIHost,
	Handler:           handler,
	ReadTimeout:       cfg.Web.ReadTimeout,
	ReadHeaderTimeout: cfg.Web.ReadTimeout,
	WriteTimeout:      cfg.Web.WriteTimeout,
}

// Start the service listening for requests in a separate goroutine
apiserver.ListenAndServe()

See the `main.go` file inside the `cmd/webapi` for a full usage example.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLivenessChecker

func NewLivenessChecker(healthChecks ...healthCheck) httprouter.Handle

livenessChecker is an HTTP handler that checks the API server status. If the server cannot serve requests (e.g., some resources are not ready), this should reply with HTTP Status 500. Otherwise, with HTTP Status 200

Types

type HandlerConfig

type HandlerConfig struct {
	Photos HandlerConfigPhotos
	Deps   HandlerConfigDependencies
}

type HandlerConfigDependencies

type HandlerConfigDependencies struct {
	LivenessChecker     httprouter.Handle
	TokenAuthMiddleware routes.Middleware
}

type HandlerConfigPhotos

type HandlerConfigPhotos struct {
	PhotosDirectory string
	PhotosUrlPath   string
}

HandlerConfig is used to provide dependencies and configuration to the Handler function.

type Router

type Router interface {
	// Handler returns an HTTP handler for APIs provided in this package
	Handler(
		cfg HandlerConfig,
		controllers ...controllers.Controller,
	) http.Handler

	// Close terminates any resource used in the package
	Close() error
}

Router is the package API interface representing an API handler builder

func New

func New(cfg RouterConfig) (Router, error)

New returns a new Router instance

type RouterConfig

type RouterConfig struct {
	// Logger where log entries are sent
	Logger logrus.FieldLogger
}

RouterConfig is used to provide dependencies and configuration to the New function.

Directories

Path Synopsis
Package reqcontext contains the request context.
Package reqcontext contains the request context.

Jump to

Keyboard shortcuts

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