-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·86 lines (72 loc) · 1.97 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
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
package main
import (
"encoding/json"
"github.com/edward-backend/controllers"
"github.com/edward-backend/database"
"github.com/edward-backend/utils"
"github.com/gin-contrib/multitemplate"
"github.com/joho/godotenv"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
)
var (
s *gocron.Scheduler
)
func createMyRender() multitemplate.Renderer {
r := multitemplate.NewRenderer()
r.AddFromString("redirect", "<html>\n<head>\n <title>Átirányítás...</title>\n</head>\n<body>\n<p id=\"d\" style=\"display: none\">d{{.data}}</p>\n<script>\n window.opener.postMessage(document.getElementById(\"d\").innerText, \"*\");\n setTimeout(() => {\n window.close()\n }, 500)\n</script>\n</body>\n</html>")
return r
}
func main() {
log.Println(`Starting...`)
godotenv.Load()
s = gocron.NewScheduler(time.UTC)
router := gin.Default()
database.Connect()
router.HTMLRender = createMyRender()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}))
utils.InitDiscord()
router.Use(func(c *gin.Context) {
if !strings.Contains(c.GetHeader("Content-Type"), "application/json") {
c.Next()
}
rawBody, _ := io.ReadAll(c.Request.Body)
var body map[string]interface{}
err := json.Unmarshal(rawBody, &body)
if err != nil {
c.Next()
}
c.Set("body", body)
c.Next()
})
r := router.Group("/v1")
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
router.NoRoute(func(c *gin.Context) {
c.JSON(404, map[string]interface{}{
"status": "error",
"error": "Route not found!",
})
})
controllers.InitAuth(r.Group("/auth"))
controllers.InitGuilds(r.Group("/guild"))
router.Run(":" + os.Getenv("PORT"))
}