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

Function main

example/session/main.go:14–69  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12)
13
14func main() {
15 // Create DotWeb app
16 app := dotweb.New()
17
18 // Enable session with default runtime config
19 app.HttpServer.SetEnabledSession(true)
20 app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())
21
22 // Login - set session
23 app.HttpServer.GET("/login", func(ctx dotweb.Context) error {
24 ctx.Session().Set("user", "Alice")
25 ctx.Session().Set("role", "admin")
26 return ctx.WriteString("✅ Logged in as Alice (admin)")
27 })
28
29 // Get user info from session
30 app.HttpServer.GET("/user", func(ctx dotweb.Context) error {
31 user := ctx.Session().Get("user")
32 role := ctx.Session().Get("role")
33
34 if user == nil {
35 return ctx.WriteString("❌ Not logged in. Visit /login first.")
36 }
37
38 return ctx.WriteString(fmt.Sprintf("👤 User: %v\n🔑 Role: %v", user, role))
39 })
40
41 // Logout - destroy session
42 app.HttpServer.GET("/logout", func(ctx dotweb.Context) error {
43 err := ctx.DestorySession()
44 if err != nil {
45 return ctx.WriteString("❌ Logout failed: " + err.Error())
46 }
47 return ctx.WriteString("✅ Logged out successfully")
48 })
49
50 // Check session exists
51 app.HttpServer.GET("/check", func(ctx dotweb.Context) error {
52 user := ctx.Session().Get("user")
53 if user != nil {
54 return ctx.WriteString("✅ Session exists")
55 }
56 return ctx.WriteString("❌ No session found")
57 })
58
59 fmt.Println("🚀 Session example running at http://localhost:8080")
60 fmt.Println("\nTest routes:")
61 fmt.Println(" curl http://localhost:8080/login -> Set session")
62 fmt.Println(" curl http://localhost:8080/user -> Get session data")
63 fmt.Println(" curl http://localhost:8080/check -> Check session")
64 fmt.Println(" curl http://localhost:8080/logout -> Destroy session")
65
66 if err := app.StartServer(8080); err != nil {
67 fmt.Printf("Server error: %v\n", err)
68 }
69}

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
NewDefaultRuntimeConfigFunction · 0.92
SetEnabledSessionMethod · 0.80
SetSessionConfigMethod · 0.80
StartServerMethod · 0.80
GETMethod · 0.65
SetMethod · 0.65
SessionMethod · 0.65
WriteStringMethod · 0.65
GetMethod · 0.65
DestorySessionMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected