-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (49 loc) · 1.74 KB
/
main.go
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
package main
import (
"net/http"
"os"
"time"
"github.com/Biskwit/CoTify/controllers"
"github.com/Biskwit/CoTify/middlewares"
ratelimit "github.com/JGLTechnologies/gin-rate-limit"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
func main() {
godotenv.Load()
logLevel, _ := log.ParseLevel(os.Getenv("LOG_LEVEL"))
log.SetLevel(logLevel)
r := gin.New()
r.Use(gin.Recovery())
// Rate limit middleware
store := ratelimit.InMemoryStore(&ratelimit.InMemoryOptions{
Rate: time.Second,
Limit: 20,
})
ratelimit := ratelimit.RateLimiter(store, &ratelimit.Options{
ErrorHandler: func(c *gin.Context, info ratelimit.Info) {
c.AbortWithStatus(http.StatusTooManyRequests)
},
KeyFunc: func(c *gin.Context) string {
return c.ClientIP()
},
})
r.Use(ratelimit)
// CORS middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true // Allow all origins
config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} // You can also use cors.Default() to allow all standard methods
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"} // Or use cors.Default() to allow all standard headers
config.AllowCredentials = true // Allow credentials
r.Use(cors.New(config))
//Auth middleware
AuthRoutes := r.Group("/")
AuthRoutes.Use(middlewares.AuthMiddleware())
//Forwarding
AuthRoutes.Any("*path", controllers.Forward)
//Server
log.Info("CoT Proxy is running on port " + os.Getenv("PORT"))
r.Run(os.Getenv("HOST") + ":" + os.Getenv("PORT")) // listen and serve on
}