-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
720 lines (560 loc) · 18.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/adamlahbib/gitaz/cmd/create"
"github.com/adamlahbib/gitaz/cmd/msg"
"github.com/adamlahbib/gitaz/controllers"
"github.com/adamlahbib/gitaz/initializers"
"github.com/adamlahbib/gitaz/models"
"github.com/cloudflare/cloudflare-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
_ "github.com/adamlahbib/gitaz/lib"
"github.com/joho/godotenv"
)
var githubAccessToken string
var mq msg.MQ
var clientCloudflare *cloudflare.API
var deploymentStatus *github.DeploymentStatus
var client *github.Client
func main() {
clientCloudflare = create.NewCloudflareClient(os.Getenv("CLOUDFLARE_TOKEN"), os.Getenv("CLOUDFLARE_EMAIL"))
config := msg.MQConfig{
Host: os.Getenv("MQHOST"),
Port: os.Getenv("MQPORT"),
User: os.Getenv("MQUSER"),
Pass: os.Getenv("MQPASS"),
}
mq = msg.MQ{}
mq.Init(config)
// make a queue
queue := mq.Queue("Inform")
go func() {
queue.Consume(func(msg []byte) {
// msg -> app name, status
// convert msg to json
jBody := map[string]interface{}{
"githuburl": `json:"github_url"`,
"status": `json:"status"`,
"appname": `json:"application_name"`,
}
print(string(msg))
err := json.Unmarshal(msg, &jBody)
if err != nil {
log.Panic(err)
}
// extract username and reponame from github url
username := strings.Split(jBody["githuburl"].(string), "/")[3]
repository := strings.Split(jBody["githuburl"].(string), "/")[4]
appname := jBody["appname"].(string)
status := jBody["status"].(string)
// create status check success
if deploymentStatus.GetState() == "success" {
_, err := create.CreateStatusCheck(client, username, repository, "main", status, "test", "https://"+appname+".kli8nt.tech", "test")
if err != nil {
log.Panic(err)
}
}
//create a record in cloudflare
err = create.CreateRecord(clientCloudflare, os.Getenv("CLOUDFLARE_ZONEID"), appname, os.Getenv("LOADBALANCER_IP"), false)
if err != nil {
log.Panic(err)
}
})
}()
// Doing: converting http to Gin and implementing controllers
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowHeaders: []string{"access-control-allow-origin, access-control-allow-headers, authorization, origin, content-type, accept"},
}))
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/logs/:app", func(c *gin.Context) {
controllers.HandleLogsStreamingSocket(c)
})
// Simply returns a link to the login route
//http.HandleFunc("/", rootHandler)
// //just a link no need for it, shows at the root of the server
// Login route
//http.HandleFunc("/login/github/", githubLoginHandler)
r.GET("/login/github/", githubLoginHandler)
// Github callback
//http.HandleFunc("/login/github/callback", githubCallbackHandler)
r.GET("/login/github/callback", githubCallbackHandler)
// Route where the authenticated user is redirected to
// http.HandleFunc("/loggedin", func(w http.ResponseWriter, r *http.Request) {
// loggedinHandler(w, r, "")
// })
r.GET("/loggedin", func(c *gin.Context) {
loggedinHandler(c.Writer, c.Request, "")
})
r.POST("/hook", func(c *gin.Context) {
username, reponame := controllers.GithubHooks(c)
deployment := controllers.FetchDeploymentByRepoName(reponame)
token := controllers.GetTokenByUsername(username)
dependencies := strings.Join(deployment.DependenciesFiles, ";")
iss := "false"
if deployment.IsStatic {
iss = "true"
}
jBody := map[string]interface{}{
"technology": deployment.Technology,
"version": deployment.Version,
"repository_url": deployment.RepositoryURL,
"github_token": token,
"application_name": deployment.ApplicationName,
"run_command": deployment.RunCommand,
"build_command": deployment.BuildCommand,
"install_command": deployment.InstallCommand,
"dependencies_files": dependencies,
"is_static": iss,
"output_directory": deployment.OutputDirectory,
"environment_variables": deployment.EnvironmentVariables,
"port": deployment.Port,
}
jsonString, err := json.Marshal(jBody)
if err != nil {
log.Panic(err)
}
queue := mq.Queue("Build")
queue.Publish(jsonString)
})
r.POST("/deploy", func(ctx *gin.Context) {
token := ctx.GetHeader("Authorization")
if token == "" || len(strings.Split(token, " ")) != 2 {
ctx.JSON(401, gin.H{
"message": "Unauthorized",
})
return
}
token = strings.Split(token, " ")[1]
userDataAsString := getGithubUsersData(token)
userData := map[string]interface{}{}
err := json.Unmarshal([]byte(userDataAsString), &userData)
if err != nil {
ctx.JSON(500, gin.H{
"message": "Internal Server Error",
})
return
}
deploymentHandler(ctx, userData["login"].(string), token)
})
r.GET("/apps", func(ctx *gin.Context) {
token := ctx.GetHeader("Authorization")
if token == "" || len(strings.Split(token, " ")) != 2 {
ctx.JSON(401, gin.H{
"message": "Unauthorized",
})
return
}
token = strings.Split(token, " ")[1]
userDataAsString := getGithubUsersData(token)
userData := map[string]interface{}{}
err := json.Unmarshal([]byte(userDataAsString), &userData)
if err != nil {
ctx.JSON(500, gin.H{
"message": "Internal Server Error",
})
return
}
deployments := controllers.FetchDeploymentsByUsername(userData["login"].(string))
ctx.JSON(200, gin.H{
"deployments": deployments,
})
})
r.GET("/:username", controllers.GetUser)
r.GET("/:username/update", func(c *gin.Context) {
})
r.GET("/repos", func(ctx *gin.Context) {
token := ctx.GetHeader("Authorization")
if token == "" || len(strings.Split(token, " ")) != 2 {
ctx.JSON(401, gin.H{
"message": "Unauthorized",
})
return
}
token = strings.Split(token, " ")[1]
userDataAsString := getGithubUsersData(token)
userData := map[string]interface{}{}
err := json.Unmarshal([]byte(userDataAsString), &userData)
if err != nil {
ctx.JSON(500, gin.H{
"message": "Internal Server Error",
})
return
}
controllers.FetchReposByUsername(ctx, userData["login"].(string))
})
r.GET("/:username/repos", controllers.FetchReposByUser)
r.GET("/:username/:name", controllers.GetRepo)
// r.GET("/user/:username/repos/refresh", ) to fetch user repos all over again maybe when user clicks refresh or sth
// docker must be running and connected to a remote registry
r.Run()
// fmt.Println("[ UP ON PORT 3000 ]")
// log.Panic(
// http.ListenAndServe(":3000", nil),
// )
}
func init() {
// loads values from .env into the system
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
initializers.ConnectDB()
initializers.SyncDB()
}
func updateUserRepos(client *github.Client, user *github.User) {
// Fetch repositories of the user
// Fetch all repositories of the user with pagination
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.List(context.Background(), "", opt)
if err != nil {
log.Panic(err)
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
log.Println(allRepos)
var owner models.User
initializers.DB.Where("username = ?", *user.Login).First(&owner)
//saving user repos to DB
for _, repo := range allRepos {
controllers.AddUserRepository(models.Repo{
OwnerID: owner.ID, // Set the ID of the user in the database
Name: *repo.Name,
Branch: *repo.DefaultBranch,
RepoUrl: *repo.CloneURL,
})
}
}
func loggedinHandler(w http.ResponseWriter, r *http.Request, githubData string) {
ctx := context.Background()
if githubData == "" {
// Unauthorized users get an unauthorized message
fmt.Fprintf(w, "UNAUTHORIZED!")
return
}
// Set return type JSON
w.Header().Set("Content-type", "application/json")
// Prettifying the json
var prettyJSON bytes.Buffer
// json.indent is a library utility function to prettify JSON indentation
parserr := json.Indent(&prettyJSON, []byte(githubData), "", "\t")
if parserr != nil {
log.Panic("JSON parse error")
}
// Return the prettified JSON as a string
// fmt.Fprintf(w, string(prettyJSON.Bytes()))
// create github client
log.Println(githubAccessToken)
client = create.NewClient(githubAccessToken)
user, _, err := client.Users.Get(ctx, "")
if err != nil {
log.Panic(err)
}
if !controllers.UserExists(*user.Login) {
// save user data to db
controllers.AddUser(
models.User{
Username: *user.Login,
Avatar: *user.AvatarURL,
Token: githubAccessToken,
},
)
// fetch user repos, TODO call the function when user hits the update endpoint or sth as well.
updateUserRepos(client, user)
} else {
// update user token
controllers.UpdateUserToken(*user.Login, githubAccessToken)
}
// REDIRECT
frontendUrl := os.Getenv("FRONT_URL")
http.Redirect(w, r, frontendUrl+"/apps/new?token="+githubAccessToken, http.StatusTemporaryRedirect)
}
func deploymentHandler(c *gin.Context, username string, token string) {
log.Println(c)
lock := false // to lock heavy operations of the code, but they're functional
//fetch user token
var user models.User
initializers.DB.Where("username = ?", username).First(&user)
githubAccessToken := user.Token
// create github client
client := create.NewClient(githubAccessToken)
// create deployment using controller function and specify the repo froeign key
var repoTBD models.Repo
initializers.DB.Where("repo_url = ?", c.PostForm("repository_url")).First(&repoTBD)
controllers.AddDeployment(
models.Deployment{
RepoID: repoTBD.ID,
Technology: c.PostForm("technology"),
Version: c.PostForm("version"),
RepositoryURL: c.PostForm("repository_url"),
GithubToken: token,
ApplicationName: c.PostForm("application_name"),
RunCommand: c.PostForm("run_command"),
BuildCommand: c.PostForm("build_command"),
InstallCommand: c.PostForm("install_command"),
DependenciesFiles: strings.Split(c.PostForm("dependencies_files"), ","),
IsStatic: c.PostForm("is_static") == "true",
OutputDirectory: c.PostForm("output_directory"),
EnvironmentVariables: c.PostForm("environment_variables"),
Port: c.PostForm("port"),
Status: "pending",
},
)
dependencies := strings.Join(strings.Split(c.PostForm("dependencies_files"), ","), ";")
iss := "false"
if c.PostForm("is_static") == "true" {
iss = "true"
}
jBody := map[string]interface{}{
"technology": c.PostForm("technology"),
"version": c.PostForm("version"),
"repository_url": c.PostForm("repository_url"),
"github_token": token,
"application_name": c.PostForm("application_name"),
"run_command": strings.Split(c.PostForm("run_command"), " "),
"build_command": c.PostForm("build_command"),
"install_command": c.PostForm("install_command"),
"dependencies_files": dependencies,
"is_static": iss,
"output_directory": c.PostForm("output_directory"),
"environment_variables": c.PostForm("environment_variables"),
"port": c.PostForm("port"),
}
jsonString, err := json.Marshal(jBody)
if err != nil {
log.Panic(err)
}
queue := mq.Queue("Build")
queue.Publish(jsonString)
// create cloudflare client
log.Println(clientCloudflare)
if !lock {
// create deployment
deployment, err := create.CreateDeployment(client, username, repoTBD.Name, repoTBD.Branch, "production", "test")
if err != nil {
log.Panic(err)
}
deployment_id := deployment.GetID()
// update deployment status to success
frontEnd := os.Getenv("FRONT_URL")
target := "https://" + c.PostForm("application_name") + "." + os.Getenv("KLI8NT_DOMAIN")
logsUrl := frontEnd + "/apps/dep/" + fmt.Sprint(deployment_id)
__, err := create.CreateDeploymentStatus(client, username, repoTBD.Name, deployment_id, "success", "test", frontEnd+"/apps/dep/"+fmt.Sprint(deployment_id))
if err != nil {
log.Panic(err)
}
fmt.Println(__)
_, err = create.CreateStatusCheck(client, username, repoTBD.Name, repoTBD.Branch, "success", "test", logsUrl, "test")
if err != nil {
log.Panic(err)
}
// if deploymentStatus.GetState() == "success" {
// // create status check
// _, err := create.CreateStatusCheck(client, username, repoTBD.Name, repoTBD.Branch, "pending", "test", logsUrl, "test")
// if err != nil {
// log.Panic(err)
// }
// // enable depandabot alerts for the repo
// }
// set website
_, err = create.SetWebsite(client, username, repoTBD.Name, target)
if err != nil {
log.Panic(err)
}
exists, err := create.HookExists(client, username, repoTBD.Name, os.Getenv("HOOK_URL"))
if err != nil {
log.Panic(err)
}
if !exists {
// create hook
_, err = create.CreateHook(client, username, repoTBD.Name, os.Getenv("HOOK_URL"), []string{"push"})
if err != nil {
log.Panic(err)
}
}
// clone repo locally
// err = imaging.CloneRepo("https://github.com/"+c.Param("username")+"/"+repoTBD.Name+".git", githubAccessToken, repoTBD.Name)
// if err != nil {
// fmt.Println("Error:", err)
// return
// }
// build image and push
// err = imaging.Build(repoTBD.Name, repoTBD.Name, "latest")
// if err != nil {
// fmt.Println("Error:", err)
// return
// }
// log.Println(deploymentStatus)
}
}
// func rootHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, `<a href="/login/github/">LOGIN</a>`)
// }
func getGithubClientID() string {
githubClientID, exists := os.LookupEnv("CLIENT_ID")
if !exists {
log.Fatal("Github Client ID not defined in .env file")
}
return githubClientID
}
func getGithubClientSecret() string {
githubClientSecret, exists := os.LookupEnv("CLIENT_SECRET")
if !exists {
log.Fatal("Github Client ID not defined in .env file")
}
return githubClientSecret
}
// func githubLoginHandler(w http.ResponseWriter, r *http.Request) {
// // Get the environment variable
// githubClientID := getGithubClientID()
// // Create the dynamic redirect URL for login
// redirectURL := fmt.Sprintf(
// "https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s",
// githubClientID,
// "http://localhost:3000/login/github/callback",
// )
// // add scopes X-OAuth-Scopes: repo, user
// redirectURL = fmt.Sprintf("%s&scope=%s", redirectURL, "repo,user")
// http.Redirect(w, r, redirectURL, 301)
// }
func githubLoginHandler(c *gin.Context) {
// Get the environment variable
githubClientID := getGithubClientID()
// Create the dynamic redirect URL for login
backendUrl := os.Getenv("BACK_URL")
redirectURL := fmt.Sprintf(
"https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s",
githubClientID,
backendUrl+"/login/github/callback",
)
// add scopes X-OAuth-Scopes: repo, user
redirectURL = fmt.Sprintf("%s&scope=%s", redirectURL, "repo,user")
// Redirect to the dynamic URL
c.Redirect(http.StatusMovedPermanently, redirectURL)
}
// func githubCallbackHandler(w http.ResponseWriter, r *http.Request) {
// code := r.URL.Query().Get("code")
// githubAccessToken = getGithubAccessToken(code)
// githubData := getGithubData(githubAccessToken)
// loggedinHandler(w, r, githubData)
// }
func githubCallbackHandler(c *gin.Context) {
code := c.Query("code")
githubAccessToken = getGithubAccessToken(code)
githubData := getGithubData(githubAccessToken)
loggedinHandler(c.Writer, c.Request, githubData)
}
func getGithubAccessToken(code string) string {
clientID := getGithubClientID()
clientSecret := getGithubClientSecret()
// Set us the request body as JSON
requestBodyMap := map[string]string{
"client_id": clientID,
"client_secret": clientSecret,
"code": code,
}
requestJSON, _ := json.Marshal(requestBodyMap)
// POST request to set URL
req, reqerr := http.NewRequest(
"POST",
"https://github.com/login/oauth/access_token",
bytes.NewBuffer(requestJSON),
)
if reqerr != nil {
log.Panic("Request creation failed")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// Get the response
resp, resperr := http.DefaultClient.Do(req)
if resperr != nil {
log.Panic("Request failed")
}
// Response body converted to stringified JSON
respbody, _ := ioutil.ReadAll(resp.Body)
// Represents the response received from Github
type githubAccessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
}
// Convert stringified JSON to a struct object of type githubAccessTokenResponse
var ghresp githubAccessTokenResponse
json.Unmarshal(respbody, &ghresp)
// Return the access token (as the rest of the
// details are relatively unnecessary for us)
return ghresp.AccessToken
}
func getGithubData(accessToken string) string {
// Get request to a set URL
req, reqerr := http.NewRequest(
"GET",
"https://api.github.com/user/repos",
nil,
)
if reqerr != nil {
log.Panic("API Request creation failed")
}
// Set the Authorization header before sending the request
// Authorization: token XXXXXXXXXXXXXXXXXXXXXXXXXXX
log.Println("Access token: ", accessToken)
authorizationHeaderValue := fmt.Sprintf("token %s", accessToken)
req.Header.Set("Authorization", authorizationHeaderValue)
// Make the request
resp, resperr := http.DefaultClient.Do(req)
if resperr != nil {
log.Panic("Request failed")
}
// Read the response as a byte slice
respbody, _ := ioutil.ReadAll(resp.Body)
// Convert byte slice to string and return
return string(respbody)
}
func getGithubUsersData(accessToken string) string {
// Get request to a set URL
req, reqerr := http.NewRequest(
"GET",
"https://api.github.com/user",
nil,
)
if reqerr != nil {
log.Panic("API Request creation failed")
}
// Set the Authorization header before sending the request
// Authorization: token XXXXXXXXXXXXXXXXXXXXXXXXXXX
log.Println("Access token: ", accessToken)
authorizationHeaderValue := fmt.Sprintf("token %s", accessToken)
req.Header.Set("Authorization", authorizationHeaderValue)
// Make the request
resp, resperr := http.DefaultClient.Do(req)
if resperr != nil {
log.Panic("Request failed")
}
// Read the response as a byte slice
respbody, _ := ioutil.ReadAll(resp.Body)
// Convert byte slice to string and return
return string(respbody)
}