-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
127 lines (102 loc) · 3.21 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"github.com/99designs/gqlgen/graphql/playground"
"github.com/guidewire/fern-reporter/pkg/graph/generated"
"github.com/guidewire/fern-reporter/pkg/graph/resolvers"
"github.com/guidewire/fern-reporter/pkg/utils"
"gorm.io/gorm"
"context"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/guidewire/fern-reporter/config"
"github.com/guidewire/fern-reporter/pkg/api/routers"
"github.com/guidewire/fern-reporter/pkg/auth"
"github.com/guidewire/fern-reporter/pkg/db"
"html/template"
"log"
"time"
"embed"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
//go:embed pkg/views/test_runs.html
//go:embed pkg/views/insights.html
var testRunsTemplate embed.FS
func main() {
initConfig()
initDb()
initServer()
}
func initConfig() {
if _, err := config.LoadConfig(); err != nil {
log.Fatalf("error: %v", err)
}
}
func initDb() {
db.Initialize()
}
func initServer() {
serverConfig := config.GetServer()
gin.SetMode(gin.DebugMode)
router := gin.Default()
if config.GetAuth().Enabled {
checkAuthConfig()
configJWTMiddleware(router)
} else {
log.Println("Auth is disabled, JWT Middleware is not configured.")
}
router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "ACCESS_TOKEN"},
AllowCredentials: false,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
}))
funcMap := template.FuncMap{
"CalculateDuration": utils.CalculateDuration,
"FormatDate": utils.FormatDate,
}
templ, err := template.New("").Funcs(funcMap).ParseFS(testRunsTemplate, "pkg/views/test_runs.html", "pkg/views/insights.html")
if err != nil {
log.Fatalf("error parsing templates: %v", err)
}
router.SetHTMLTemplate(templ)
// router.LoadHTMLGlob("pkg/views/*")
routers.RegisterRouters(router)
router.POST("/query", GraphqlHandler(db.GetDb()))
router.GET("/", PlaygroundHandler("/query"))
err = router.Run(serverConfig.Port)
if err != nil {
log.Fatalf("error starting routes: %v", err)
}
}
func PlaygroundHandler(path string) gin.HandlerFunc {
h := playground.Handler("GraphQL playground", path)
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func GraphqlHandler(gormdb *gorm.DB) gin.HandlerFunc {
h := handler.New(generated.NewExecutableSchema(generated.Config{Resolvers: &resolvers.Resolver{DB: gormdb}}))
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func checkAuthConfig() {
if config.GetAuth().ScopeClaimName == "" {
log.Fatal("Set SCOPE_CLAIM_NAME environment variable or add a default value in config.yaml")
}
if config.GetAuth().JSONWebKeysEndpoint == "" {
log.Fatal("Set AUTH_JSON_WEB_KEYS_ENDPOINT environment variable or add a default value in config.yaml")
}
}
func configJWTMiddleware(router *gin.Engine) {
authConfig := config.GetAuth()
ctx := context.Background()
keyFetcher, err := auth.NewDefaultJWKSFetcher(ctx, authConfig.JSONWebKeysEndpoint)
if err != nil {
log.Fatalf("Failed to create JWKS fetcher: %v", err)
}
jwtValidator := &auth.DefaultJWTValidator{}
router.Use(auth.JWTMiddleware(authConfig.JSONWebKeysEndpoint, keyFetcher, jwtValidator))
log.Println("JWT Middleware configured successfully.")
}