-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (50 loc) · 1.76 KB
/
main.go
File metadata and controls
60 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"time"
"github.com/devfeel/dotweb"
)
// JWT Middleware Example
// This demonstrates how to use JWT authentication with dotweb
func main() {
// init DotApp
app := dotweb.New()
// Set route
app.HttpServer.GET("/api/public", func(ctx dotweb.Context) error {
return ctx.WriteString("This is a public endpoint")
})
// Protected route - in real app, add JWT validation middleware
app.HttpServer.GET("/api/private", func(ctx dotweb.Context) error {
return ctx.WriteJson(map[string]interface{}{
"message": "This is a private endpoint",
"user": "admin",
})
})
// Login endpoint - returns simple token (use proper JWT in production)
app.HttpServer.POST("/api/login", func(ctx dotweb.Context) error {
// In production, validate credentials from database
username := ctx.PostFormValue("username")
password := ctx.PostFormValue("password")
// Simple validation (replace with real auth)
if username == "admin" && password == "password" {
// Generate simple token (use proper JWT library in production)
token := generateSimpleToken(username)
return ctx.WriteJson(map[string]string{
"token": token,
})
}
return ctx.WriteString("Invalid credentials")
})
fmt.Println("JWT Example server starting on :8080")
fmt.Println("Public endpoint: GET /api/public")
fmt.Println("Private endpoint: GET /api/private")
fmt.Println("Login: POST /api/login?username=admin&password=password")
err := app.StartServer(8080)
fmt.Println("server error => ", err)
}
// generateSimpleToken creates a simple token (for demonstration only)
// In production, use: github.com/golang-jwt/jwt
func generateSimpleToken(username string) string {
exp := time.Now().Add(time.Hour).Unix()
return fmt.Sprintf("%s.%d.%s", username, exp, "signature")
}