### Hello, World! Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/quickstart.md
A basic 'Hello, World!' example demonstrating Echo's core setup, middleware usage, and starting the server.
```go
package main
import (
"net/http"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
func main() {
e := echo.New()
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
e.GET("/", func(c *echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "Hello, World!"})
})
if err := e.Start(":1323"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
```
--------------------------------
### Run a Cookbook Recipe Example
Source: https://github.com/labstack/echox/blob/master/README.md
Commands to navigate to a specific cookbook recipe directory and run it using Go. Assumes Go is installed.
```bash
cd cookbook/hello-world
go run .
```
--------------------------------
### Minimal Echo Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/hello-world.md
Sets up a basic Echo server with Logger and Recover middleware, a root route, and starts the server. Ensure Echo is installed.
```go
package main
import (
"context"
"net/http"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
// Route => handler
e.GET("/", func(c *echo.Context) error {
return c.String(http.StatusOK, "Hello, World!\n")
})
// Start server
sc := echo.StartConfig{Address: ":1323"}
if err := sc.Start(context.Background(), e); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
```
--------------------------------
### Full Example: Echo HTTP/2 Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/http2.md
A complete Go program demonstrating how to set up an Echo server that handles requests over HTTP/2 using TLS. Includes a handler to display request information and starts the server with a certificate.
```go
package main
import (
"context"
"fmt"
"net/http"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
e.GET("/request", func(c *echo.Context) error {
req := c.Request()
format := `
Protocol: %s
`
return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
})
sc := echo.StartConfig{Address: ":1323"}
if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
```
--------------------------------
### Basic Echo Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/index.mdx
Create a new Echo instance, register a root route, and start the server on port 1323. This demonstrates the minimal setup for a running Echo application.
```go
e := echo.New()
e.GET("/", hello)
e.Start(":1323")
```
--------------------------------
### Install Prometheus Middleware
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/prometheus.md
Install the echo-prometheus module using go get.
```bash
go get github.com/labstack/echo-prometheus
```
--------------------------------
### Install Gorilla Sessions Dependency
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/session.md
Install the gorilla/sessions package using go get.
```bash
go get github.com/gorilla/sessions
```
--------------------------------
### Install and Run Documentation Site
Source: https://github.com/labstack/echox/blob/master/README.md
Commands to install dependencies and run the development server for the documentation site. Requires Node.js.
```bash
cd site
npm install
npm run dev # dev server at http://localhost:4321
npm run build # production build to site/dist
npm run preview # preview the production build
```
--------------------------------
### Install Echo
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/quickstart.md
Initialize a new Go module and add the Echo framework dependency.
```bash
go mod init myapp
go get github.com/labstack/echo/v5
```
--------------------------------
### Full Example with Request ID
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/request-id.md
A complete example demonstrating how to integrate the RequestID middleware and access the generated ID within a request handler.
```go
func main() {
e := echo.New()
e.Use(middleware.RequestID())
e.GET("/", func(c *echo.Context) error {
return c.String(http.StatusOK, c.Response().Header().Get(echo.HeaderXRequestID))
})
if err := e.Start(":8080"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
```
--------------------------------
### Start Proxy Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/reverse-proxy.md
Command to start the main Echo proxy server.
```sh
go run server.go
```
--------------------------------
### Install Casbin Dependency
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/casbin-auth.md
Install the Casbin Go library using go get.
```bash
go get github.com/casbin/casbin/v3
```
--------------------------------
### Start Custom HTTP Server with TLS
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/http2.md
Configure and start a custom http.Server with Echo as the handler and custom TLS configuration. This provides more control over server settings like timeouts.
```go
s := http.Server{
Addr: ":8443",
Handler: e, // set Echo as handler
TLSConfig: &tls.Config{
//Certificates: nil, // <-- s.ListenAndServeTLS will populate this field
},
//ReadTimeout: 30 * time.Second, // use custom timeouts
}
if err := s.ListenAndServeTLS("cert.pem", "key.pem"); err != http.ErrServerClosed {
log.Fatal(err)
}
```
--------------------------------
### Full Casbin + JWT Example in Go
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/casbin-auth.md
A complete example demonstrating the integration of Casbin authorization with JWT authentication in an Echo application.
```go
package main
import (
"log/slog"
"net/http"
"github.com/casbin/casbin/v3"
"github.com/golang-jwt/jwt/v5"
echojwt "github.com/labstack/echo-jwt/v5"
"github.com/labstack/echo/v5"
)
// NewCasbinMiddleware returns middleware for Casbin (https://casbin.org/).
func NewCasbinMiddleware(enforcer *casbin.Enforcer, userGetter func(*echo.Context) (string, error)) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
username, err := userGetter(c)
if err != nil {
return echo.ErrUnauthorized.Wrap(err)
}
if pass, err := enforcer.Enforce(username, c.Request().URL.Path, c.Request().Method); err != nil {
return echo.ErrInternalServerError.Wrap(err)
} else if !pass {
return echo.NewHTTPError(http.StatusForbidden, "access denied")
}
return next(c)
}
}
}
func main() {
e := echo.New()
ce, err := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
if err != nil {
slog.Error("failed to initialize Casbin enforcer", "error", err)
}
e.Use(echojwt.JWT([]byte("secret")))
jwtUser := func(c *echo.Context) (string, error) { // JWT user getter for Casbin authorization
token, err := echo.ContextGet[*jwt.Token](c, "user")
if err != nil {
return "", err
}
return token.Claims.GetSubject()
}
e.Use(NewCasbinMiddleware(ce, jwtUser)) // Casbin does authorization
e.GET("/*", func(c *echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
if err := e.Start(":8080"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
}
```
--------------------------------
### Run Go Application
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/quickstart.md
Execute the Go application to start the Echo server.
```bash
go run main.go
```
--------------------------------
### Start Echo TLS Server
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/http2.md
Start the Echo server using a TLS certificate and key. This enables HTTP/2 communication. Ensure the certificate and key files are accessible.
```go
sc := echo.StartConfig{Address: ":1323"}
if err := sc.StartTLS(context.Background(), e, "cert.pem", "key.pem"); err != nil {
e.Logger.Error("failed to start server", "error", err)
}
```
--------------------------------
### Serve All Files with Echo#Static()
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/static-files.md
Serve all files from a directory under the root path. This example serves files from 'assets' under the '/' path.
```go
e := echo.New()
e.Static("/", "assets")
```
--------------------------------
### Start Echo Server with Auto TLS
Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/auto-tls.md
Configure Echo's `StartConfig` with an autocert manager's `TLSConfig` to automatically obtain and renew TLS certificates from Let's Encrypt. This is suitable for basic Echo server setups listening on port 443.
```go
package main
import (
"context"
"crypto/tls"
"errors"
"log/slog"
"net/http"
"os"
"golang.org/x/crypto/acme"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"golang.org/x/crypto/acme/autocert"
)
func main() {
e := echo.New()
e.Logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
e.Use(middleware.Recover())
e.Use(middleware.RequestLogger())
e.GET("/", func(c *echo.Context) error {
return c.HTML(http.StatusOK, `
Host: %s
Remote Address: %s
Method: %s
Path: %s
Hello from upstream server %s
` func main() { name := os.Args[1] port := os.Args[2] e := echo.New() e.Use(middleware.Recover()) e.Use(middleware.RequestLogger()) e.GET("/", func(c *echo.Context) error { return c.HTML(http.StatusOK, fmt.Sprintf(index, name)) }) sc := echo.StartConfig{Address: port} if err := sc.Start(context.Background(), e); err != nil { e.Logger.Error("failed to start server", "error", err) } } ``` -------------------------------- ### Get Session from Context Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/session.md Retrieves the session store from the context and then gets a specific session by name. Handles potential errors during store retrieval. ```go func GetSession(c *echo.Context, name string) (*sessions.Session, error) { store, err := echo.ContextGet[sessions.Store](c, "_session_store") if err != nil { return nil, err } return store.Get(c.Request(), name) } ``` -------------------------------- ### Setting Path Parameters Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/testing.md Demonstrates how to set path parameters on an Echo context using c.SetPathValues. ```go c.SetPathValues(echo.PathValues{ {Name: "id", Value: "1"}, {Name: "email", Value: "jon@labstack.com"}, }) ``` -------------------------------- ### Static File Serving with Custom Configuration Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/static.md Serve static files from the 'static' directory and enable directory browsing. ```go e := echo.New() e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Root: "static", Browse: true, })) ``` -------------------------------- ### Advanced Template Example with Echo Reverse Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/templates.md This example demonstrates how to call Echo's Reverse function from within an HTML template. It requires a custom renderer that augments the context passed to the template. ```html{{ with $x := index . "reverse" }} {{ call $x "foobar" }} {{ end }}
``` -------------------------------- ### Restricted Resource Response Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/jwt.md Example response from a restricted resource after successful authentication with a valid JWT. ```text Welcome Jon Snow! ``` -------------------------------- ### Client: Delete User Request Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/crud.md Example of how to delete a user by their ID using a DELETE request. ```sh curl -X DELETE localhost:1323/users/1 ``` -------------------------------- ### Proxy with Custom Configuration Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/proxy.md Initialize the Proxy middleware with a custom configuration, allowing for fine-grained control over its behavior. ```go e := echo.New() e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{})) ``` -------------------------------- ### Basic Static File Serving Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/static.md Serve static files from the '/static' directory. Requests to '/js/main.js' will fetch 'static/js/main.js'. ```go e := echo.New() e.Use(middleware.Static("/static")) ``` -------------------------------- ### Custom Method Override Configuration Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/method-override.md Configure Method Override to get the overridden method from a form field named '_method'. ```go e.Pre(middleware.MethodOverrideWithConfig(middleware.MethodOverrideConfig{ Getter: middleware.MethodFromForm("_method"), })) ``` -------------------------------- ### Client: Update User Request Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/crud.md Example of how to update an existing user's name using a PUT request with a JSON payload. ```sh curl -X PUT \ -H 'Content-Type: application/json' \ -d '{"name":"Joe"}' \ localhost:1323/users/1 ``` -------------------------------- ### Testing GetUser Handler with echotest Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/testing.md Tests the getUser handler using echotest.ContextConfig to set path parameters and simulate a GET request. ```go func TestGetUser(t *testing.T) { c, rec := echotest.ContextConfig{ PathValues: echo.PathValues{ {Name: "email", Value: "jon@labstack.com"}, }, Headers: map[string][]string{ echo.HeaderContentType: {echo.MIMEApplicationJSON}, }, }.ToContextRecorder(t) h := &handler{mockDB} // Assertions if assert.NoError(t, h.getUser(c)) { assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, userJSON+"\n", rec.Body.String()) } } ``` -------------------------------- ### Handling Files in Responses Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/context.md Serve files directly, prompt for download with a specified filename, or render files inline. ```go c.File("public/report.pdf") // serve a file c.Attachment("invoice.pdf", "inv.pdf") // prompt download c.Inline("photo.png", "photo.png") // render inline ``` -------------------------------- ### Sample Prometheus Metrics Output Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/prometheus.md Example output of Prometheus metrics collected by the middleware, showing request duration, size, and total requests. ```bash curl http://localhost:8080/metrics # HELP echo_request_duration_seconds The HTTP request latencies in seconds. # TYPE echo_request_duration_seconds summary echo_request_duration_seconds_sum 0.41086482 echo_request_duration_seconds_count 1 # HELP echo_request_size_bytes The HTTP request sizes in bytes. # TYPE echo_request_size_bytes summary echo_request_size_bytes_sum 56 echo_request_size_bytes_count 1 # HELP echo_requests_total How many HTTP requests processed, partitioned by status code and HTTP method. # TYPE echo_requests_total counter echo_requests_total{code="200",host="localhost:8080",method="GET",url="/"} 1 # HELP echo_response_size_bytes The HTTP response sizes in bytes. # TYPE echo_response_size_bytes summary echo_response_size_bytes_sum 61 echo_response_size_bytes_count 1 ... ``` -------------------------------- ### Serve HTML and Push Dependencies Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/http2-server-push.md Serves an index.html file and proactively pushes its associated assets (CSS, JS, images) if the underlying response writer supports http.Pusher. This eliminates a round trip for these assets. ```go e.GET("/", func(c *echo.Context) (err error) { rw, err := echo.UnwrapResponse(c.Response()) if err != nil { return } if pusher, ok := rw.ResponseWriter.(http.Pusher); ok { if err = pusher.Push("/app.css", nil); err != nil { return } if err = pusher.Push("/app.js", nil); err != nil { return } if err = pusher.Push("/echo.png", nil); err != nil { return } } return c.File("index.html") }) ``` -------------------------------- ### Registering Routes for Any or Multiple Methods Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/routing.md Use `Any` to register a handler for all supported HTTP methods on a path, or `Match` to specify a subset of methods. ```go e.Any("/ping", pong) e.Match([]string{http.MethodGet, http.MethodPost}, "/form", handleForm) ``` -------------------------------- ### Import Gorilla Sessions Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/session.md Import the necessary sessions package for session management. ```go import ( "github.com/gorilla/sessions" ) ``` -------------------------------- ### Pre-compile Templates with ParseGlob Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/guide/templates.md Pre-compile your HTML templates using ParseGlob to efficiently load all templates from a specified directory. This is typically done once during application setup. ```html {{define "hello"}}Hello, {{.}}{{end}} ``` ```go t := &Template{ templates: template.Must(template.ParseGlob("public/views/*.html")) } ``` -------------------------------- ### Graceful Shutdown with Echo StartConfig Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/graceful-shutdown.md This snippet demonstrates how to use `echo.StartConfig` with a cancellable context and `GracefulTimeout` for automatic graceful shutdown on interrupt signals. ```go package main import ( "context" "errors" "net/http" "os" "os/signal" "syscall" "time" "github.com/labstack/echo/v5" ) func main() { // Setup e := echo.New() e.GET("/", func(c *echo.Context) error { time.Sleep(5 * time.Second) return c.JSON(http.StatusOK, "OK") }) ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() sc := echo.StartConfig{ Address: ":1323", GracefulTimeout: 5 * time.Second, } if err := sc.Start(ctx, e); err != nil { e.Logger.Error("failed to start server", "error", err) } } ``` -------------------------------- ### SSE Server Implementation Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/sse.md Sets up an Echo server to handle SSE connections. It writes SSE headers and emits events every second. Use http.NewResponseController(w).Flush() to push events immediately. Ensure server.WriteTimeout is disabled for SSE. ```go package main import ( "context" "fmt" "log" "net/http" "os" "os/signal" "syscall" "time" "github.com/labstack/echo/v5" "github.com/labstack/echo/v5/middleware" ) func main() { e := echo.New() e.Use(middleware.RequestLogger()) e.Use(middleware.Recover()) e.File("/", "./index.html") e.GET("/sse", func(c *echo.Context) error { log.Printf("SSE client connected, ip: %v", c.RealIP()) w := c.Response() w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() count := uint64(0) for { select { case <-c.Request().Context().Done(): log.Printf("SSE client disconnected, ip: %v", c.RealIP()) return nil case <-ticker.C: count++ event := Event{ Data: []byte(fmt.Sprintf("count: %d, time: %s\n\n", count, time.Now().Format(time.RFC3339Nano))), } if err := event.MarshalTo(w); err != nil { return err } if err := http.NewResponseController(w).Flush(); err != nil { return err } } } }) sc := echo.StartConfig{ Address: ":8080", BeforeServeFunc: func(s *http.Server) error { s.WriteTimeout = 0 // IMPORTANT: disable for SSE return nil }, } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) // start shutdown process on ctrl+c defer cancel() if err := sc.Start(ctx, e); err != nil { e.Logger.Error("failed to start server", "error", err) } } ``` -------------------------------- ### Configure CORS with an Allow List Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/cors.md Use `middleware.CORS` with a list of allowed origins to restrict access. This example allows requests from `https://labstack.com` and `https://labstack.net`. ```go package main import ( "context" "net/http" "github.com/labstack/echo/v5" "github.com/labstack/echo/v5/middleware" ) var ( users = []string{"Joe", "Veer", "Zion"} ) func getUsers(c *echo.Context) error { return c.JSON(http.StatusOK, users) } func main() { e := echo.New() e.Use(middleware.RequestLogger()) e.Use(middleware.Recover()) // CORS default // Allows requests from any origin wth GET, HEAD, PUT, POST or DELETE method. // e.Use(middleware.CORS("*\n")) // CORS restricted // Allows requests from any `https://labstack.com` or `https://labstack.net` origin e.Use(middleware.CORS("https://labstack.com", "https://labstack.net")) e.GET("/api/users", getUsers) sc := echo.StartConfig{Address: ":1323"} if err := sc.Start(context.Background(), e); err != nil { e.Logger.Error("failed to start server", "error", err) } } ``` -------------------------------- ### Custom Rewrite Configuration Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/rewrite.md Initialize Echo and apply rewrite middleware with custom configuration. ```go e := echo.New() e.Pre(middleware.RewriteWithConfig(middleware.RewriteConfig{})) ``` -------------------------------- ### Request Logging with Logrus Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/logger.md Configure the request logger to use Logrus, a popular logging library with a customizable Hook API. This example logs the request URI and status. ```go log := logrus.New() e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{ LogURI: true, LogStatus: true, LogValuesFunc: func(c *echo.Context, values middleware.RequestLoggerValues) error { log.WithFields(logrus.Fields{ "URI": values.URI, "status": values.Status, }).Info("request") return nil }, })) ``` -------------------------------- ### Create Session Middleware Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/middleware/session.md Creates the session middleware using a provided session store. The store is set in the context for later retrieval. ```go func NewSessionMiddleware(store sessions.Store) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c *echo.Context) error { c.Set("_session_store", store) return next(c) } } } ``` -------------------------------- ### HTML Structure for Server Push Source: https://github.com/labstack/echox/blob/master/site/src/content/docs/cookbook/http2-server-push.md An example HTML file that links to CSS, JavaScript, and an image. These assets are intended to be pushed by the server using HTTP/2 Server Push. ```html
/app.css/app.js/echo.png