MCPcopy Create free account
hub / github.com/devfeel/dotweb / main

Function main

example/group/main.go:8–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6)
7
8func main() {
9 // Create DotWeb app
10 app := dotweb.New()
11
12 // Set global 404 handler
13 app.SetNotFoundHandle(func(ctx dotweb.Context) {
14 ctx.Response().Header().Set("Content-Type", "application/json")
15 ctx.WriteString(`{"code": 404, "message": "Global 404 - Page not found"}`)
16 })
17
18 // Create API group
19 apiGroup := app.HttpServer.Group("/api")
20
21 // Set group-level 404 handler
22 apiGroup.SetNotFoundHandle(func(ctx dotweb.Context) {
23 ctx.Response().Header().Set("Content-Type", "application/json")
24 ctx.WriteString(`{"code": 404, "message": "API 404 - Resource not found", "hint": "Check API documentation for available endpoints"}`)
25 })
26
27 // Register API routes
28 apiGroup.GET("/users", func(ctx dotweb.Context) error {
29 return ctx.WriteString(`{"users": ["Alice", "Bob", "Charlie"]}`)
30 })
31
32 apiGroup.GET("/health", func(ctx dotweb.Context) error {
33 return ctx.WriteString(`{"status": "ok"}`)
34 })
35
36 // Create Web group (no custom 404 handler, will use global)
37 webGroup := app.HttpServer.Group("/web")
38
39 webGroup.GET("/index", func(ctx dotweb.Context) error {
40 return ctx.WriteString("<h1>Welcome to Web</h1>")
41 })
42
43 // Root route
44 app.HttpServer.GET("/", func(ctx dotweb.Context) error {
45 return ctx.WriteString("Welcome to DotWeb! Try:\n" +
46 "- GET /api/users (exists)\n" +
47 "- GET /api/unknown (API 404)\n" +
48 "- GET /web/index (exists)\n" +
49 "- GET /web/unknown (Global 404)\n" +
50 "- GET /unknown (Global 404)")
51 })
52
53 fmt.Println("Server starting on :8080...")
54 fmt.Println("\nTest routes:")
55 fmt.Println(" curl http://localhost:8080/ - Welcome page")
56 fmt.Println(" curl http://localhost:8080/api/users - API: Users list")
57 fmt.Println(" curl http://localhost:8080/api/health - API: Health check")
58 fmt.Println(" curl http://localhost:8080/api/unknown - API: 404 (group handler)")
59 fmt.Println(" curl http://localhost:8080/web/index - Web: Index page")
60 fmt.Println(" curl http://localhost:8080/web/unknown - Web: 404 (global handler)")
61 fmt.Println(" curl http://localhost:8080/unknown - Global: 404 (global handler)")
62
63 // Start server
64 err := app.StartServer(8080)
65 if err != nil {

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
StartServerMethod · 0.80
SetNotFoundHandleMethod · 0.65
SetMethod · 0.65
ResponseMethod · 0.65
WriteStringMethod · 0.65
GroupMethod · 0.65
GETMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected