-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathllms.txt
More file actions
215 lines (168 loc) · 5.39 KB
/
Copy pathllms.txt
File metadata and controls
215 lines (168 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Echo
> Echo is a high performance, extensible, minimalist Go web framework built on the standard `net/http` library. Echo adds a fast radix-tree router, request binding with pluggable validator, a deep middleware ecosystem, centralized error handling, and template rendering on top of the standard library.
Key facts for LLMs:
- Import path is `github.com/labstack/echo/v5`
- Handlers use `*echo.Context` (pointer to struct)
- `Context` is a concrete struct, not an interface
- Logging uses `log/slog`
- Go 1.25+ required
## Quick Reference
- [Echo v5 API Changes](https://github.com/labstack/echo/blob/master/API_CHANGES_V5.md): Breaking changes reference
- [Echo Official Docs](https://echo.labstack.com): Human-readable documentation site
- [pkg.go.dev](https://pkg.go.dev/github.com/labstack/echo/v5): API reference on Go package discovery
## Core Patterns
### Server setup
```go
package main
import (
"log/slog"
"net/http"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
func hello(c *echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func main() {
e := echo.New()
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
e.GET("/", hello)
if err := e.Start(":8080"); err != nil {
slog.Error("failed to start server", "error", err)
}
}
```
### Handler signature
```go
func handler(c *echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"hello": "world"})
}
```
### Route registration
```go
e := echo.New()
e.GET("/users/:id", getUser)
e.POST("/users", createUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)
// Group routes
g := e.Group("/api/v1")
g.GET("/posts", listPosts)
g.POST("/posts", createPost)
// Group with middleware
admin := e.Group("/admin", middleware.BasicAuth(basicAuthValidator))
admin.GET("/stats", getStats)
```
### Generic parameter extraction
```go
// Type-safe path params
id, err := echo.PathParam[int](c, "id")
name, err := echo.PathParam[string](c, "name")
// Query params with defaults
page, err := echo.QueryParamOr[int](c, "page", 1)
tags, err := echo.QueryParams[string](c, "tags")
// Form values
val, err := echo.FormValue[string](c, "field")
val2, err := echo.FormValueOr[string](c, "field", "default")
// Supported types: bool, string, int/int8/.../int64, uint/.../uint64,
// float32/float64, time.Time, time.Duration
```
### Request binding
```go
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
func createUser(c *echo.Context) error {
u := new(User)
if err := c.Bind(u); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "invalid request body")
}
if err := c.Validate(u); err != nil {
return err
}
return c.JSON(http.StatusCreated, u)
}
```
### Error handling
```go
// NewHTTPError takes a string message
err := echo.NewHTTPError(http.StatusBadRequest, "invalid input")
// Custom error handler
e.HTTPErrorHandler = func(c *echo.Context, err error) {
// custom error handling
}
// DefaultHTTPErrorHandler is a factory (exposeError bool)
e.HTTPErrorHandler = echo.DefaultHTTPErrorHandler(true)
```
### Logging
```go
// Echo uses the standard library slog
e.Logger = slog.Default()
// In handlers
c.Logger().Info("request", "path", c.Path(), "method", c.Request().Method)
// Custom logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
e.Logger = logger
```
### Middleware
```go
// Built-in middleware
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.Use(middleware.Gzip())
e.Use(middleware.BasicAuth(func(c *echo.Context, username, password string) (bool, error) {
return username == "admin" && password == "secret", nil
}))
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20)))
// Route-level middleware
e.GET("/sensitive", handler, middleware.RequestID(), middleware.Logger())
```
### Response helpers
```go
c.String(http.StatusOK, "text")
c.JSON(http.StatusOK, data)
c.JSONPretty(http.StatusOK, data, " ")
c.XML(http.StatusOK, data)
c.Blob(http.StatusOK, "image/png", bytes)
c.Stream(http.StatusOK, "text/event-stream", reader)
c.File("path/to/file")
c.FileFS("file.txt", filesystem)
c.Attachment("path/to/file", "download.txt")
c.Redirect(http.StatusFound, "/new-path")
c.NoContent(http.StatusOK)
c.HTML(http.StatusOK, "<h1>Hello</h1>")
```
### Static files
```go
e.Static("/assets", "public")
e.StaticFS("/static", filesystem)
e.File("/robots.txt", "robots.txt")
e.FileFS("file.txt", filesystem)
```
### Context store (type-safe)
```go
// Set and get values on context
c.Set("user", user)
val, err := echo.ContextGet[*User](c, "user")
count, err := echo.ContextGetOr[int](c, "count", 0)
```
### Graceful shutdown with StartConfig
```go
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
sc := echo.StartConfig{
Address: ":8080",
GracefulTimeout: 10 * time.Second,
}
if err := sc.Start(ctx, e); err != nil {
log.Fatal(err)
}
```
## Optional
- [Echo GitHub Discussions](https://github.com/labstack/echo/discussions): Community Q&A and proposals
- [Echo Roadmap](https://github.com/labstack/echo/blob/master/ROADMAP.md): Future plans
- [Echo Changelog](https://github.com/labstack/echo/blob/master/CHANGELOG.md): Release history
- [API_CHANGES_V5.md](https://github.com/labstack/echo/blob/master/API_CHANGES_V5.md): Full v5 breaking changes reference