-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (59 loc) · 1.51 KB
/
main.go
File metadata and controls
72 lines (59 loc) · 1.51 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
package main
import (
"context"
"musicboxapi/configuration"
"musicboxapi/database"
"musicboxapi/http"
"musicboxapi/logging"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/lrstanley/go-ytdlp"
)
func main() {
configuration.LoadConfiguration()
database.CreateDatabasConnectionPool()
defer database.DbInstance.Close()
// If yt-dlp isn't installed yet, download and cache it for further use.
ytdlp.MustInstall(context.TODO(), nil)
setGinMode()
ginEngine := gin.Default()
// Trust nginx
ginEngine.SetTrustedProxies([]string{"127.0.0.1"})
ginEngine.Use(corsMiddelWare())
// V1 API
apiv1Group := ginEngine.Group(configuration.GetApiGroupUrl("v1"))
http.V1Endpoints(apiv1Group)
if configuration.Config.DevPort != "" {
devPort := "127.0.0.1:" + configuration.Config.DevPort
logging.Info("Running on development port")
ginEngine.Run(devPort)
} else {
ginEngine.Run() // listen and serve on 0.0.0.0:8080
}
}
func corsMiddelWare() gin.HandlerFunc {
if configuration.Config.UseDevUrl {
return cors.Default()
} else {
origin := os.Getenv("CORS_ORIGIN")
// Use Default cors
if len(origin) == 0 {
logging.Warning("CORS is enabled for all origins")
return cors.Default()
} else {
strictCors := cors.New(cors.Config{
AllowAllOrigins: false,
AllowOrigins: []string{origin}, // move to env
})
return strictCors
}
}
}
func setGinMode() {
if configuration.Config.UseDevUrl {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
}