()
| 12 | ) |
| 13 | |
| 14 | func 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 | } |
nothing calls this directly
no test coverage detected