Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (102 loc) · 2.97 KB
/
Copy pathmain.go
File metadata and controls
123 lines (102 loc) · 2.97 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"GinSvelteEmbed/internal/counter"
"GinSvelteEmbed/internal/debug"
"GinSvelteEmbed/web"
ratelimit "github.com/JGLTechnologies/gin-rate-limit"
"github.com/gin-gonic/gin"
"io/fs"
"log"
"net/http"
"strings"
"time"
)
func main() {
// Initialize the counter with a persistent file
cnt, err := counter.NewCounter("counter.txt")
if err != nil {
log.Fatal(err)
}
f := getFileSystem()
//print fs content for debugging
debug.PrintFSContent(f, 1)
// Create a Gin engine
r := gin.Default()
// Set up API routes
setupAPIRoutes(r, cnt)
// Serve static files and fallback for SPA
r.Use(spaFileHandler(f))
// Run the server on port 8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}
func keyFunc(c *gin.Context) string {
return c.ClientIP()
}
func errorHandler(c *gin.Context, info ratelimit.Info) {
c.JSON(http.StatusTooManyRequests, gin.H{"error": "Rate limit exceeded, reset in " + time.Until(info.ResetTime).String()})
}
// setupAPIRoutes configures the API endpoints
func setupAPIRoutes(r *gin.Engine, cnt *counter.Counter) {
// Middleware to limit the rate of requests
//je veux max 10request par minute et reset après 30 secondes
store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
Rate: 1 * time.Minute, // 1 minute
Limit: 10, // max 10 requests per minute
})
mw := ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: errorHandler,
KeyFunc: keyFunc,
})
api := r.Group("/api")
{
api.Use(mw)
api.GET("/count", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"count": cnt.Value()})
})
api.POST("/increment", func(c *gin.Context) {
if err := cnt.Increment(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"count": cnt.Value()})
})
api.POST("/decrement", func(c *gin.Context) {
if err := cnt.Decrement(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"count": cnt.Value()})
})
}
}
// spaFileHandler serves static files and handles fallback for SPA routing
func spaFileHandler(httpFS http.FileSystem) gin.HandlerFunc {
fileServer := http.FileServer(httpFS)
return func(c *gin.Context) {
path := c.Request.URL.Path
// Bypass the handler for API requests
if strings.HasPrefix(path, "/api") {
c.Next()
return
}
// Try to serve the exact file
if _, err := httpFS.Open(path); err == nil {
fileServer.ServeHTTP(c.Writer, c.Request)
return
}
// Fallback to serving index.html for SPA
c.Request.URL.Path = "/"
fileServer.ServeHTTP(c.Writer, c.Request)
c.Request.URL.Path = path // Restore the original path
}
}
// getFileSystem retrieves the embedded filesystem for serving static files
func getFileSystem() http.FileSystem {
fSys, err := fs.Sub(web.Fs, "build") // Extract the "build" directory from the embedded filesystem
if err != nil {
panic(err)
}
return http.FS(fSys)
}