-
Notifications
You must be signed in to change notification settings - Fork 130
/
main.go
160 lines (125 loc) · 5.91 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package bootstrap
import (
"embed"
"github.com/diggerhq/digger/backend/config"
"github.com/diggerhq/digger/backend/segment"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"time"
"github.com/diggerhq/digger/backend/controllers"
"github.com/diggerhq/digger/backend/middleware"
"github.com/diggerhq/digger/backend/models"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
)
// based on https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications
var Version = "dev"
func Bootstrap(templates embed.FS, diggerController controllers.DiggerController) *gin.Engine {
defer segment.CloseClient()
initLogging()
cfg := config.DiggerConfig
if err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
EnableTracing: true,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 0.1,
Release: "api@" + Version,
Debug: true,
}); err != nil {
log.Printf("Sentry initialization failed: %v\n", err)
}
//database migrations
models.ConnectDatabase()
r := gin.Default()
// TODO: check "secret"
store := gormsessions.NewStore(models.DB.GormDB, true, []byte("secret"))
r.Use(sessions.Sessions("digger-session", store))
r.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
r.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"build_date": cfg.GetString("build_date"),
"deployed_at": cfg.GetString("deployed_at"),
"version": Version,
"commit_sha": Version,
})
})
r.SetFuncMap(template.FuncMap{
"formatAsDate": func(msec int64) time.Time {
return time.UnixMilli(msec)
},
})
if _, err := os.Stat("templates"); err != nil {
matches, _ := fs.Glob(templates, "templates/*.tmpl")
for _, match := range matches {
r.LoadHTMLFiles(match)
}
r.StaticFS("/static", http.FS(templates))
} else {
r.Static("/static", "./templates/static")
r.LoadHTMLGlob("templates/*.tmpl")
}
r.POST("/github-app-webhook", diggerController.GithubAppWebHook)
r.POST("/github-app-webhook/aam", diggerController.GithubAppWebHookAfterMerge)
tenantActionsGroup := r.Group("/api/tenants")
tenantActionsGroup.Use(middleware.CORSMiddleware())
tenantActionsGroup.Any("/associateTenantIdToDiggerOrg", controllers.AssociateTenantIdToDiggerOrg)
githubGroup := r.Group("/github")
githubGroup.Use(middleware.GetWebMiddleware())
githubGroup.GET("/callback", diggerController.GithubAppCallbackPage)
githubGroup.GET("/repos", diggerController.GithubReposPage)
githubGroup.GET("/setup", controllers.GithubAppSetup)
githubGroup.GET("/exchange-code", diggerController.GithubSetupExchangeCode)
authorized := r.Group("/")
authorized.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(models.CliJobAccessType, models.AccessPolicyType, models.AdminPolicyType))
admin := r.Group("/")
admin.Use(middleware.GetApiMiddleware(), middleware.AccessLevel(models.AdminPolicyType))
fronteggWebhookProcessor := r.Group("/")
fronteggWebhookProcessor.Use(middleware.SecretCodeAuth())
authorized.GET("/repos/:repo/projects/:projectName/access-policy", controllers.FindAccessPolicy)
authorized.GET("/orgs/:organisation/access-policy", controllers.FindAccessPolicyForOrg)
authorized.GET("/repos/:repo/projects/:projectName/plan-policy", controllers.FindPlanPolicy)
authorized.GET("/orgs/:organisation/plan-policy", controllers.FindPlanPolicyForOrg)
authorized.GET("/repos/:repo/projects/:projectName/drift-policy", controllers.FindDriftPolicy)
authorized.GET("/orgs/:organisation/drift-policy", controllers.FindDriftPolicyForOrg)
authorized.GET("/repos/:repo/projects/:projectName/runs", controllers.RunHistoryForProject)
authorized.POST("/repos/:repo/projects/:projectName/runs", controllers.CreateRunForProject)
authorized.POST("/repos/:repo/projects/:projectName/jobs/:jobId/set-status", diggerController.SetJobStatusForProject)
authorized.GET("/repos/:repo/projects", controllers.FindProjectsForRepo)
authorized.POST("/repos/:repo/report-projects", controllers.ReportProjectsForRepo)
authorized.GET("/orgs/:organisation/projects", controllers.FindProjectsForOrg)
admin.PUT("/repos/:repo/projects/:projectName/access-policy", controllers.UpsertAccessPolicyForRepoAndProject)
admin.PUT("/orgs/:organisation/access-policy", controllers.UpsertAccessPolicyForOrg)
admin.PUT("/repos/:repo/projects/:projectName/plan-policy", controllers.UpsertPlanPolicyForRepoAndProject)
admin.PUT("/orgs/:organisation/plan-policy", controllers.UpsertPlanPolicyForOrg)
admin.PUT("/repos/:repo/projects/:projectName/drift-policy", controllers.UpsertDriftPolicyForRepoAndProject)
admin.PUT("/orgs/:organisation/drift-policy", controllers.UpsertDriftPolicyForOrg)
admin.POST("/tokens/issue-access-token", controllers.IssueAccessTokenForOrg)
r.Use(middleware.CORSMiddleware())
projectsApiGroup := r.Group("/api/projects")
projectsApiGroup.Use(middleware.GetApiMiddleware())
projectsApiGroup.GET("/", controllers.FindProjectsForOrg)
projectsApiGroup.GET("/:project_id", controllers.ProjectDetails)
projectsApiGroup.GET("/:project_id/runs", controllers.RunsForProject)
activityApiGroup := r.Group("/api/activity")
activityApiGroup.Use(middleware.GetApiMiddleware())
activityApiGroup.GET("/", controllers.GetActivity)
runsApiGroup := r.Group("/api/runs")
runsApiGroup.Use(middleware.CORSMiddleware(), middleware.GetApiMiddleware())
runsApiGroup.GET("/:run_id", controllers.RunDetails)
runsApiGroup.POST("/:run_id/approve", controllers.ApproveRun)
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
return r
}
func initLogging() {
log.SetOutput(os.Stdout)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.Println("Initialized the logger successfully")
}