Documentation
¶
Overview ¶
Package echo provides a simplified wrapper around the Echo web framework.
This package offers convenient methods for creating and managing Echo HTTP servers with support for routing, middleware, and graceful shutdown.
Features:
- Simplified server setup with Echo v4
- Handler and middleware registration
- Group routing support
- Graceful shutdown with timeout
- Thread-safe operations
Example:
server := &echo.Server{}
server.RegisterHandler(echo.GET, "/hello", func(c echo.Context) error {
return c.String(200, "Hello, World!")
})
server.Start(":8080", nil)
Index ¶
- func WrapHandler(handler http.Handler) echo.HandlerFunc
- func WrapHandlerFunc(handlerFunc http.HandlerFunc) echo.HandlerFunc
- type Server
- func (s *Server) GetEcho() *echo.Echo
- func (s *Server) Group(prefix string, middleware ...echo.MiddlewareFunc) *echo.Group
- func (s *Server) IsRunning() bool
- func (s *Server) RegisterHandler(method string, path string, handler echo.HandlerFunc, ...)
- func (s *Server) RegisterHandlerAny(path string, handler echo.HandlerFunc, middleware ...echo.MiddlewareFunc)
- func (s *Server) Start(address string, listenAndServeFailureFunc func(err error)) error
- func (s *Server) Stop(shutdownTimeout time.Duration) error
- func (s *Server) Use(middleware ...echo.MiddlewareFunc)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WrapHandler ¶
func WrapHandler(handler http.Handler) echo.HandlerFunc
WrapHandler wraps a standard http.Handler into an Echo handler function.
This function allows you to use standard Go http.Handler implementations (such as http.FileServer, third-party handlers, or http.HandlerFunc) within the Echo framework.
Parameters:
- handler: Standard http.Handler to wrap
Returns:
- echo.HandlerFunc: Echo-compatible handler function
Example:
// Wrap http.FileServer
fs := http.FileServer(http.Dir("./static"))
server.RegisterHandler(echo.GET, "/static/*", WrapHandler(fs))
// Wrap http.HandlerFunc
stdHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from standard handler"))
})
server.RegisterHandler(echo.GET, "/std", WrapHandler(stdHandler))
func WrapHandlerFunc ¶
func WrapHandlerFunc(handlerFunc http.HandlerFunc) echo.HandlerFunc
WrapHandlerFunc wraps a standard http.HandlerFunc into an Echo handler function.
This is a convenience function that wraps http.HandlerFunc (a function type) into an Echo-compatible handler. It's functionally equivalent to WrapHandler but provides better type clarity when working with HandlerFunc types.
Parameters:
- handlerFunc: Standard http.HandlerFunc to wrap
Returns:
- echo.HandlerFunc: Echo-compatible handler function
Example:
// Wrap a HandlerFunc directly
handler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello World"))
}
server.RegisterHandler(echo.GET, "/hello", WrapHandlerFunc(handler))
// Or use with http.HandlerFunc type conversion
server.RegisterHandler(echo.POST, "/api", WrapHandlerFunc(
http.HandlerFunc(myStandardHandler),
))
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct that provides Echo HTTP server related methods.
func (*Server) GetEcho ¶
GetEcho returns the underlying Echo instance for advanced usage.
Returns:
- *echo.Echo: Echo instance
Use this method when you need direct access to Echo's API for advanced configuration.
Example:
e := server.GetEcho() e.HideBanner = true e.Debug = true
func (*Server) Group ¶
Group creates a new router group with optional middleware.
Parameters:
- prefix: Path prefix for the group
- middleware: Optional middleware functions for the group
Returns:
- *echo.Group: Echo router group
Example:
api := server.Group("/api/v1")
api.GET("/users", getUsersHandler)
func (*Server) IsRunning ¶
IsRunning returns whether the server is currently running.
Returns:
- bool: true if server is running, false otherwise
Example:
if server.IsRunning() {
fmt.Println("Server is active")
}
func (*Server) RegisterHandler ¶
func (s *Server) RegisterHandler(method string, path string, handler echo.HandlerFunc, middleware ...echo.MiddlewareFunc)
RegisterHandler registers an HTTP handler with optional middleware.
Parameters:
- method: HTTP method (echo.GET, echo.POST, etc.)
- path: URL path pattern
- handler: Echo handler function
- middleware: Optional middleware functions
Example:
server.RegisterHandler(echo.GET, "/users/:id", getUserHandler)
func (*Server) RegisterHandlerAny ¶
func (s *Server) RegisterHandlerAny(path string, handler echo.HandlerFunc, middleware ...echo.MiddlewareFunc)
RegisterHandlerAny registers an HTTP handler for all methods (GET, POST, PUT, DELETE, etc.) with optional middleware.
Parameters:
- path: URL path pattern
- handler: Echo handler function
- middleware: Optional middleware functions
Example:
server.RegisterHandlerAny("/users/:id", getUserHandler)
func (*Server) Start ¶
Start starts the Echo HTTP server.
Parameters:
- address: Server address (e.g., ":8080", "localhost:3000")
- listenAndServeFailureFunc: Optional callback for server errors (excluding graceful shutdown)
Returns:
- error: Error if server is already running or fails to start
The server runs in a background goroutine. Use Stop() for graceful shutdown.
Example:
err := server.Start(":8080", func(err error) {
log.Printf("Server error: %v", err)
})
func (*Server) Stop ¶
Stop gracefully shuts down the Echo server.
Parameters:
- shutdownTimeout: Maximum time to wait for shutdown
Returns:
- error: Error if shutdown fails
Example:
err := server.Stop(10 * time.Second)
func (*Server) Use ¶
func (s *Server) Use(middleware ...echo.MiddlewareFunc)
Use registers global middleware.
Parameters:
- middleware: Middleware functions to apply globally
Example:
server.Use(middleware.Logger(), middleware.Recover())