Documentation
¶
Overview ¶
Example ¶
app := fx.New(
fx.Provide(
// Provide configuration
func() *fxConfig.Config {
config := &fxConfig.Config{}
config.Accessor = fxConfig.ConfigAccessor()
return config
},
// Provide logger
func() *zap.Logger {
return zap.NewNop()
},
// Provide routes
AsRoute(func() RouteRegistryIf {
return GET("/hello", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "Hello World"})
}).Build()
}),
// Provide route groups
AsGroup(func() GroupRegistryIf {
return NewGroup("/api/v1").
AddRoute(GET("/users", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]interface{}{"users": []string{}})
}).Build()).
AddRoute(POST("/users", func(c echo.Context) error {
return c.JSON(http.StatusCreated, map[string]string{"message": "User created"})
}).Build()).
Build()
}),
// Provide custom middleware
fx.Annotate(
func() echo.MiddlewareFunc {
return middleware.Logger()
},
fx.ResultTags(`group:"middlewares"`),
),
),
FxEcho,
fx.Invoke(func(e *echo.Echo) {
// Echo server will be automatically started
}),
)
app.Run()
Index ¶
- Constants
- Variables
- func AsGroup(f any) any
- func AsMiddleware(f any) any
- func AsRoute(f any) any
- func NewEcho(p EchoParams) (*echo.Echo, error)
- type EchoParams
- type GroupBuilder
- type GroupRegistryIf
- type MiddlewareRegistryIf
- type RouteBuilder
- func DELETE(path string, handle echo.HandlerFunc) *RouteBuilder
- func GET(path string, handle echo.HandlerFunc) *RouteBuilder
- func NewRoute(method, path string, handle echo.HandlerFunc) *RouteBuilder
- func PATCH(path string, handle echo.HandlerFunc) *RouteBuilder
- func POST(path string, handle echo.HandlerFunc) *RouteBuilder
- func PUT(path string, handle echo.HandlerFunc) *RouteBuilder
- type RouteRegistryIf
- type ServerConfig
Examples ¶
Constants ¶
const ModuleName = "fxecho"
Variables ¶
var FxEcho = fx.Module( ModuleName, fx.Provide( NewEcho, NewServerConfig, ), fx.Invoke(func(e *echo.Echo) {}), )
Functions ¶
func AsMiddleware ¶
AsMiddleware annotates a middleware constructor for Fx.
Types ¶
type EchoParams ¶
type EchoParams struct {
fx.In
Lifecycle fx.Lifecycle
Routes []RouteRegistryIf `group:"routes"`
Groups []GroupRegistryIf `group:"groups"`
Config *fxConfig.Config
Middlewares []echo.MiddlewareFunc `group:"middlewares"`
Logger *zap.Logger
}
EchoParams holds all dependencies for Echo server
type GroupBuilder ¶
type GroupBuilder struct {
// contains filtered or unexported fields
}
GroupBuilder provides a fluent interface for building route groups
func (*GroupBuilder) AddGroup ¶
func (gb *GroupBuilder) AddGroup(group GroupRegistryIf) *GroupBuilder
AddGroup adds a child group
func (*GroupBuilder) AddRoute ¶
func (gb *GroupBuilder) AddRoute(route RouteRegistryIf) *GroupBuilder
AddRoute adds a route to the group
func (*GroupBuilder) Build ¶
func (gb *GroupBuilder) Build() GroupRegistryIf
Build returns the group registry interface
func (*GroupBuilder) Use ¶
func (gb *GroupBuilder) Use(middleware ...echo.MiddlewareFunc) *GroupBuilder
Use adds middleware to the group
type GroupRegistryIf ¶
GroupRegistryIf defines the interface for route group registration
type MiddlewareRegistryIf ¶
type MiddlewareRegistryIf interface {
Priority() int
Middleware() echo.MiddlewareFunc
}
MiddlewareRegistryIf defines the interface for middleware registration
type RouteBuilder ¶
type RouteBuilder struct {
// contains filtered or unexported fields
}
RouteBuilder provides a fluent interface for building routes
func DELETE ¶
func DELETE(path string, handle echo.HandlerFunc) *RouteBuilder
DELETE creates a DELETE route
func NewRoute ¶
func NewRoute(method, path string, handle echo.HandlerFunc) *RouteBuilder
NewRoute creates a new route builder
func PATCH ¶
func PATCH(path string, handle echo.HandlerFunc) *RouteBuilder
PATCH creates a PATCH route
func (*RouteBuilder) Build ¶
func (rb *RouteBuilder) Build() RouteRegistryIf
Build returns the route registry interface
type RouteRegistryIf ¶
RouteRegistryIf defines the interface for route registration
type ServerConfig ¶
type ServerConfig struct {
Host string `mapstructure:"host"`
Port string `mapstructure:"port"`
ReadTimeout time.Duration `mapstructure:"read_timeout"`
WriteTimeout time.Duration `mapstructure:"write_timeout"`
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
}
ServerConfig holds Echo server configuration
func NewServerConfig ¶
func NewServerConfig(config *fxConfig.Config) (*ServerConfig, error)
NewServerConfig creates server configuration from fxConfig