Skip to content

Commit c1d01d1

Browse files
committed
docs: add documentation improvements
- Update README.md with Group SetNotFoundHandle example - Add CHANGELOG.md to track version changes - Add Go Doc comments to Group interface and xGroup struct Resolves: #233
1 parent ae49f86 commit c1d01d1

3 files changed

Lines changed: 142 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
- **Group SetNotFoundHandle** - Support custom 404 handler for router groups (#229)
12+
- Added `SetNotFoundHandle(handler StandardHandle)` method to Group interface
13+
- Group-level 404 handler takes priority over app-level handler
14+
- Example: `apiGroup.SetNotFoundHandle(func(ctx Context) error { ... })`
15+
16+
- **Trailing Slash Redirect Configuration** - Control trailing slash redirect behavior (#245)
17+
- Added `EnabledRedirectTrailingSlash` configuration option
18+
- Default is `false` to match net/http behavior
19+
- Set to `true` to enable automatic 301 redirect for trailing slashes
20+
21+
- **CORS Preflight Support** - Fixed CORS preflight request handling (#250)
22+
- Modified `DefaultAutoOPTIONSHandler` to include CORS headers
23+
- Better integration with CORS middleware
24+
25+
### Fixed
26+
- **Security Vulnerabilities** - Upgraded dependencies to fix security issues
27+
- Upgraded `gopkg.in/yaml.v2` to `v3.0.1` (fixes DoS vulnerability)
28+
- Upgraded `golang.org/x/net` to `v0.33.0` (fixes XSS and proxy bypass)
29+
30+
### Changed
31+
- **Go Version** - Maintained Go 1.21 compatibility
32+
- **Code Quality** - Improved error handling and nil checks
33+
34+
## [1.0.0] - Previous Release
35+
36+
### Features
37+
- Support for go mod
38+
- Static routing, parameter routing, group routing
39+
- Route support for file/directory services
40+
- HttpModule support
41+
- Middleware support (App, Group, Router levels)
42+
- STRING/JSON/JSONP/HTML output formats
43+
- Built-in Mock capability
44+
- Custom Context support
45+
- Timeout Hook integration
46+
- Global HTTP error handling
47+
- Global logging
48+
- Hijack and WebSocket support
49+
- Built-in Cache support
50+
- Built-in Session support with Redis failover
51+
- Built-in TLS support
52+
- Third-party template engine support
53+
- Modular configuration
54+
- Built-in statistics
55+
56+
---
57+
58+
For more details, see [GitHub Releases](https://github.com/devfeel/dotweb/releases).

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,28 @@ test:
175175
<br>curl http://127.0.0.1/user
176176
<br>curl http://127.0.0.1/user/profile
177177

178+
#### 5) group SetNotFoundHandle (Since v1.8)
179+
支持为路由组设置独立的 404 处理器,优先于应用级别的 NotFoundHandler。
180+
``` go
181+
// Create API group
182+
apiGroup := server.Group("/api")
183+
184+
// Set group-level 404 handler
185+
apiGroup.SetNotFoundHandle(func(ctx dotweb.Context) error {
186+
ctx.Response().Header().Set("Content-Type", "application/json")
187+
return ctx.WriteString(`{"code": 404, "message": "API resource not found"}`)
188+
})
189+
190+
// Register routes
191+
apiGroup.GET("/users", func(ctx dotweb.Context) error {
192+
return ctx.WriteString("Users list")
193+
})
194+
```
195+
Behavior:
196+
<br>Request to `/api/users` → Returns "Users list"
197+
<br>Request to `/api/unknown` → Returns JSON 404 response (group handler)
198+
<br>Request to `/unknown` → Returns global 404 (app handler)
199+
178200

179201
## 6. Binder
180202
* HttpContext.Bind(interface{})

group.go

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,59 @@ package dotweb
22

33
import "reflect"
44

5-
type (
6-
Group interface {
7-
Use(m ...Middleware) Group
8-
Group(prefix string, m ...Middleware) Group
9-
DELETE(path string, h HttpHandle) RouterNode
10-
GET(path string, h HttpHandle) RouterNode
11-
HEAD(path string, h HttpHandle) RouterNode
12-
OPTIONS(path string, h HttpHandle) RouterNode
13-
PATCH(path string, h HttpHandle) RouterNode
14-
POST(path string, h HttpHandle) RouterNode
15-
PUT(path string, h HttpHandle) RouterNode
16-
ServerFile(path string, fileroot string) RouterNode
17-
RegisterRoute(method, path string, h HttpHandle) RouterNode
18-
}
19-
xGroup struct {
20-
prefix string
21-
middlewares []Middleware
22-
allRouterExpress map[string]struct{}
23-
server *HttpServer
24-
}
25-
)
5+
// Group is the interface that wraps the group router methods.
6+
// A Group allows you to create routes with a common prefix and middleware chain.
7+
type Group interface {
8+
// Use registers middleware(s) to the group.
9+
// Middleware is applied to all routes in this group.
10+
Use(m ...Middleware) Group
11+
12+
// Group creates a new sub-group with the given prefix and optional middleware.
13+
// The sub-group inherits middleware from the parent group.
14+
Group(prefix string, m ...Middleware) Group
15+
16+
// DELETE registers a new DELETE route with the given path and handler.
17+
DELETE(path string, h HttpHandle) RouterNode
18+
19+
// GET registers a new GET route with the given path and handler.
20+
GET(path string, h HttpHandle) RouterNode
21+
22+
// HEAD registers a new HEAD route with the given path and handler.
23+
HEAD(path string, h HttpHandle) RouterNode
24+
25+
// OPTIONS registers a new OPTIONS route with the given path and handler.
26+
OPTIONS(path string, h HttpHandle) RouterNode
27+
28+
// PATCH registers a new PATCH route with the given path and handler.
29+
PATCH(path string, h HttpHandle) RouterNode
30+
31+
// POST registers a new POST route with the given path and handler.
32+
POST(path string, h HttpHandle) RouterNode
33+
34+
// PUT registers a new PUT route with the given path and handler.
35+
PUT(path string, h HttpHandle) RouterNode
36+
37+
// ServerFile registers a file server route with the given path and file root.
38+
ServerFile(path string, fileroot string) RouterNode
39+
40+
// RegisterRoute registers a new route with the given HTTP method, path and handler.
41+
RegisterRoute(method, path string, h HttpHandle) RouterNode
42+
43+
// SetNotFoundHandle sets a custom 404 handler for this group.
44+
// This handler takes priority over the app-level NotFoundHandler.
45+
// If a request path starts with the group's prefix but no route matches,
46+
// this handler will be called instead of the global NotFoundHandler.
47+
SetNotFoundHandle(handler StandardHandle) Group
48+
}
49+
50+
// xGroup is the implementation of Group interface.
51+
type xGroup struct {
52+
prefix string
53+
middlewares []Middleware
54+
allRouterExpress map[string]struct{}
55+
server *HttpServer
56+
notFoundHandler StandardHandle
57+
}
2658

2759
func NewGroup(prefix string, server *HttpServer) Group {
2860
g := &xGroup{prefix: prefix, server: server, allRouterExpress: make(map[string]struct{})}
@@ -119,3 +151,12 @@ func (g *xGroup) add(method, path string, handler HttpHandle) RouterNode {
119151
node.Node().groupMiddlewares = g.middlewares
120152
return node
121153
}
154+
155+
// SetNotFoundHandle sets a custom 404 handler for this group.
156+
// This handler takes priority over the app-level NotFoundHandler.
157+
// If a request path starts with the group's prefix but no route matches,
158+
// this handler will be called instead of the global NotFoundHandler.
159+
func (g *xGroup) SetNotFoundHandle(handler StandardHandle) Group {
160+
g.notFoundHandler = handler
161+
return g
162+
}

0 commit comments

Comments
 (0)