Documentation ¶
Overview ¶
Package apimanager provides a way to create and manage API servers.
Example ¶
server := apimanager.New(nil) err := server.Mount(apimanager.Route{ Path: "/", Methods: []string{fiber.MethodGet}, Handler: func(c fiber.Ctx) error { return c.SendString("Hello, World!") }, }) if err != nil { fmt.Println(err) return } if err := server.Run(context.Background()); err != nil { panic(err) }
Output:
Index ¶
- Constants
- func OkHandler(c fiber.Ctx) error
- type Config
- type ErrAlreadyRunning
- type ErrNotRunning
- type Route
- func Delete(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- func Get(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- func Patch(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- func Post(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- func Put(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- func Use(path string, handler fiber.Handler, middlewares ...fiber.Handler) Route
- type RouteGroup
- type Server
- type TLSConfig
Examples ¶
Constants ¶
const MethodUse = "USE"
MethodUse is a custom HTTP method to indicate that a route should be registered for all HTTP methods.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // Address is the address to listen on. Address string `yaml:"address" mapstructure:"address"` // BasePath is the base path of the API. BasePath string `yaml:"basePath" mapstructure:"basePath"` // UseDefaultHealthz indicates if the default healthz handler should be used. UseDefaultHealthz bool `yaml:"useDefaultHealthz" mapstructure:"useDefaultHealthz"` // TLS is the TLS configuration. TLS TLSConfig `yaml:"tls" mapstructure:"tls"` }
Config is the configuration of the server.
type ErrAlreadyRunning ¶
type ErrAlreadyRunning struct{}
ErrAlreadyRunning is an error that is returned when the server is already running.
func (*ErrAlreadyRunning) Error ¶
func (e *ErrAlreadyRunning) Error() string
Error returns the error message.
func (*ErrAlreadyRunning) Is ¶
func (e *ErrAlreadyRunning) Is(target error) bool
Is checks if the target error is an ErrAlreadyRunning error.
type ErrNotRunning ¶
type ErrNotRunning struct{}
ErrNotRunning is an error that is returned when the server is not running.
func (*ErrNotRunning) Error ¶
func (e *ErrNotRunning) Error() string
Error returns the error message.
func (*ErrNotRunning) Is ¶
func (e *ErrNotRunning) Is(target error) bool
Is checks if the target error is an ErrNotRunning error.
type Route ¶
type Route struct { // Path is the path of the route. Path string // Methods is the HTTP method of the route. // To register the route to all http methods, use [MethodUse]. // [MethodUse] is mutually exclusive with other methods. Methods []string // Handler is the handler function of the route. Handler fiber.Handler // Middlewares are the middlewares to use for the route. Middlewares []fiber.Handler }
Route is a route to register to the server.
func Delete ¶ added in v0.4.0
Delete creates a new Route with the provided path, handler, and middlewares for the http.MethodDelete method.
func Get ¶ added in v0.4.0
Get creates a new Route with the provided path, handler, and middlewares for the http.MethodGet method.
func Patch ¶ added in v0.4.0
Patch creates a new Route with the provided path, handler, and middlewares for the http.MethodPatch method.
func Post ¶ added in v0.4.0
Post creates a new Route with the provided path, handler, and middlewares for the http.MethodPost method.
func Put ¶ added in v0.4.0
Put creates a new Route with the provided path, handler, and middlewares for the http.MethodPut method.
type RouteGroup ¶
type RouteGroup struct { // Path is the Path of the route. Path string // App is the fiber sub-App to use. App fiber.Router }
RouteGroup is a route to register a sub-app to.
func NewRouteGroup ¶ added in v0.4.0
func NewRouteGroup(path string, app fiber.Router) RouteGroup
NewRouteGroup creates a new RouteGroup with the provided path and sub-app.
type Server ¶
type Server interface { // Run attaches all previously mounted routes and starts the server. // Runs indefinitely until an error occurs, the server shuts down, or the provided context is done. // // Example setup: // server := apimanager.New(nil) // server.Mount(apimanager.Get("/", func(c fiber.Ctx) error { // return c.SendString("Hello, World!") // })) // // The server will listen on the default address ":8080" and respond with "Hello, World!" on a GET request to "/". // server.Run(context.Background()) Run(ctx context.Context) error // Restart restarts the server by shutting it down and starting it again. // If any routes or groups are provided, they will be added to the server. // All existing routes and groups will be preserved. Restart(ctx context.Context, routes []Route, groups []RouteGroup) error // Shutdown gracefully shuts down the server. Shutdown(ctx context.Context) error // Mount adds the provided routes to the server. Mount(routes ...Route) error // MountGroup adds the provided route groups to the server. MountGroup(groups ...RouteGroup) error // App returns the fiber app of the server. App() *fiber.App // Mounted returns all mounted routes, groups, and global middlewares. Mounted() (routes []Route, groups []RouteGroup, middlewares []fiber.Handler) // Config returns the configuration of the server. Config() Config }
Server is the interface for an API server.
type TLSConfig ¶
type TLSConfig struct { // Enabled indicates if TLS is enabled. Enabled bool `yaml:"enabled" mapstructure:"enabled"` // CertFile is the path to the certificate file. CertFile string `yaml:"certPath" mapstructure:"certPath"` // CertKeyFile is the path to the certificate key file. CertKeyFile string `yaml:"keyPath" mapstructure:"keyPath"` }
TLSConfig is the TLS configuration.
Directories ¶
Path | Synopsis |
---|---|
Package fiberutils provides utilities for working with the fiber.Ctx type.
|
Package fiberutils provides utilities for working with the fiber.Ctx type. |
Package middleware provides common middleware as fiber.Handler.
|
Package middleware provides common middleware as fiber.Handler. |