-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (86 loc) · 3.27 KB
/
main.go
File metadata and controls
112 lines (86 loc) · 3.27 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
package main
import (
"context"
"log"
"os"
"strings"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"google.golang.org/api/option"
profixio "github.com/nvbf/tournament-sync/repos/profixio"
resend "github.com/nvbf/tournament-sync/repos/resend"
auth "github.com/nvbf/tournament-sync/pkg/auth"
admin "github.com/nvbf/tournament-sync/services/admin"
matches "github.com/nvbf/tournament-sync/services/matches"
stats "github.com/nvbf/tournament-sync/services/stats"
sync "github.com/nvbf/tournament-sync/services/sync"
)
func main() {
ctx := context.Background()
projectID := os.Getenv("FIREBASE_PROJECT_ID")
credentialsJSON := os.Getenv("FIREBASE_CREDENTIALS_JSON")
profixioHost := os.Getenv("PROFIXIO_HOST")
port := os.Getenv("PORT")
firestoreDb := os.Getenv("FIREBASE_DATABASE_ID")
allowOrigins := os.Getenv("CORS_HOSTS")
hostURL := os.Getenv("HOST_URL")
credentialsOption := option.WithCredentialsJSON([]byte(credentialsJSON))
firestoreClient, err := firestore.NewClientWithDatabase(ctx, projectID, firestoreDb, credentialsOption)
if err != nil {
log.Fatalf("Failed to create Firestore client: %v", err)
}
defer firestoreClient.Close()
firebaseApp, err := firebase.NewApp(ctx, nil, credentialsOption)
if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}
profixioService := profixio.NewService(firestoreClient, profixioHost)
resendService := resend.NewService(firestoreClient, hostURL)
adminService := admin.NewAdminService(firestoreClient, firebaseApp, resendService)
syncService := sync.NewSyncService(firestoreClient, firebaseApp, profixioService)
matchesService := matches.NewMatchesService(firestoreClient, firebaseApp, profixioService)
statsService := stats.NewStatsService(firestoreClient, firebaseApp)
config := cors.DefaultConfig()
config.AllowOrigins = strings.Split(allowOrigins, ",")
config.AllowCredentials = true
config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Access-Control-Allow-Origin"}
router := gin.Default()
router.Use(corsMiddleware())
adminRouter := router.Group("/admin/v1")
adminRouter.Use(auth.AuthMiddleware(firebaseApp)) // Apply the middleware here
matchesRouter := router.Group("/match/v1")
matchesRouter.Use(auth.AuthMiddleware(firebaseApp)) // Apply the middleware here
syncRouter := router.Group("/sync/v1")
statsRouter := router.Group("/stats/v1")
admin.NewHTTPHandler(admin.HTTPOptions{
Service: adminService,
Router: adminRouter,
})
matches.NewHTTPHandler(matches.HTTPOptions{
Service: matchesService,
Router: matchesRouter,
})
sync.NewHTTPHandler(sync.HTTPOptions{
Service: syncService,
Router: syncRouter,
})
stats.NewHTTPHandler(stats.HTTPOptions{
Service: statsService,
Router: statsRouter,
})
log.Fatal(router.Run(":" + port))
}
func corsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}