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

Function main

example/routing/main.go:11–107  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9)
10
11func main() {
12 // Create DotWeb app
13 app := dotweb.New()
14 app.SetDevelopmentMode()
15
16 // ===== Basic Routes =====
17
18 // GET request
19 app.HttpServer.GET("/", func(ctx dotweb.Context) error {
20 return ctx.WriteString("GET / - Home page")
21 })
22
23 // POST request
24 app.HttpServer.POST("/users", func(ctx dotweb.Context) error {
25 return ctx.WriteString("POST /users - Create user")
26 })
27
28 // PUT request
29 app.HttpServer.PUT("/users/:id", func(ctx dotweb.Context) error {
30 id := ctx.GetRouterName("id")
31 return ctx.WriteString("PUT /users/" + id + " - Update user")
32 })
33
34 // DELETE request
35 app.HttpServer.DELETE("/users/:id", func(ctx dotweb.Context) error {
36 id := ctx.GetRouterName("id")
37 return ctx.WriteString("DELETE /users/" + id + " - Delete user")
38 })
39
40 // Any method
41 app.HttpServer.Any("/any", func(ctx dotweb.Context) error {
42 return ctx.WriteString("ANY /any - Method: " + ctx.Request().Method)
43 })
44
45 // ===== Path Parameters =====
46
47 // Single parameter
48 app.HttpServer.GET("/users/:id", func(ctx dotweb.Context) error {
49 id := ctx.GetRouterName("id")
50 return ctx.WriteString("User ID: " + id)
51 })
52
53 // Multiple parameters
54 app.HttpServer.GET("/users/:userId/posts/:postId", func(ctx dotweb.Context) error {
55 userId := ctx.GetRouterName("userId")
56 postId := ctx.GetRouterName("postId")
57 return ctx.WriteString(fmt.Sprintf("User: %s, Post: %s", userId, postId))
58 })
59
60 // Wildcard (catch-all)
61 app.HttpServer.GET("/files/*filepath", func(ctx dotweb.Context) error {
62 filepath := ctx.GetRouterName("filepath")
63 return ctx.WriteString("File path: " + filepath)
64 })
65
66 // ===== Route Groups =====
67
68 // API group

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
SetDevelopmentModeMethod · 0.80
StartServerMethod · 0.80
GETMethod · 0.65
WriteStringMethod · 0.65
POSTMethod · 0.65
PUTMethod · 0.65
GetRouterNameMethod · 0.65
DELETEMethod · 0.65
AnyMethod · 0.65
RequestMethod · 0.65
GroupMethod · 0.65

Tested by

no test coverage detected