This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
73 lines (58 loc) · 1.68 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
package main
import (
"fmt"
"os"
"strings"
"github.com/FactoryCampus/radau/api"
"github.com/FactoryCampus/radau/migrations"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-pg/pg"
)
func initCORSConfig() cors.Config {
corsConfig := cors.DefaultConfig()
originConfig, hasCorsOrigins := os.LookupEnv("CORS_ORIGINS")
if !hasCorsOrigins {
corsConfig.AllowAllOrigins = true
} else {
corsConfig.AllowOrigins = strings.Split(originConfig, ",")
}
corsConfig.AddAllowHeaders("Authorization")
corsConfig.AllowCredentials = true
return corsConfig
}
func main() {
isDebug := os.Getenv("DEBUG")
if strings.ToLower(isDebug) != "true" && isDebug != "1" {
gin.SetMode(gin.ReleaseMode)
}
db := pg.Connect(&pg.Options{
Addr: fmt.Sprintf("%s:5432", os.Getenv("DB_HOST")),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
Database: os.Getenv("DB_DATABASE"),
MaxRetries: 4,
})
defer db.Close()
err := migrations.EnsureCurrentSchema(db)
if err != nil {
panic(err)
}
authManagementKey := os.Getenv("API_KEY_MANAGEMENT")
authRadiusKey := os.Getenv("API_KEY_RADIUS")
if !gin.IsDebugging() && (authManagementKey == "" || authRadiusKey == "") {
panic("Please secure the service using environment variables: API_KEY_MANAGEMENT & API_KEY_RADIUS")
}
r := gin.Default()
r.Use(cors.New(initCORSConfig()))
// Auth is handled individually per route
api.InitUserHandler(r, db)
// Auth is handled by TokenHandler
api.InitTokenHandler(r.Group("/token"), db)
radiusRoutes := r.Group("/radius", gin.BasicAuth(gin.Accounts{
"Radius": authRadiusKey,
"radius": authRadiusKey,
}))
api.InitRadiusHandler(radiusRoutes, db)
r.Run()
}