Skip to content

Commit

Permalink
fetch all deployments endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlahbib committed May 31, 2023
1 parent a9a76ec commit 1e57545
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/create/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ func CreateRecord(client *cloudflare.API, zoneID string, name string, target str
return nil
}

// TODO: delete a cname record
// TODO: delete a canme record
8 changes: 8 additions & 0 deletions controllers/deploymentController.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ func FetchDeploymentByRepoName(repoName string) models.Deployment {
initializers.DB.First(&deployment, "repo_name = ?", repoName)
return deployment
}

// fetch all deployments by username

func FetchDeploymentsByUsername(username string) []models.Deployment {
var deployments []models.Deployment
initializers.DB.Find(&deployments, "username = ?", username)
return deployments
}
38 changes: 32 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ func main() {
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) {
Expand Down Expand Up @@ -240,8 +269,6 @@ func main() {
// 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 ]")
Expand Down Expand Up @@ -441,11 +468,11 @@ func deploymentHandler(c *gin.Context, username string, token string) {

// update deployment status to success
frontEnd := os.Getenv("FRONT_URL")
deploymentStatus, err := create.CreateDeploymentStatus(client, username, repoTBD.Name, deployment_id, "success", "test", frontEnd + "/apps/dep/" + string(deployment_id))
deploymentStatus, err := create.CreateDeploymentStatus(client, username, repoTBD.Name, deployment_id, "success", "test", frontEnd+"/apps/dep/"+string(deployment_id))
if err != nil {
log.Panic(err)
}

target := "https://" + c.PostForm("application_name") + "." + os.Getenv("KLI8NT_DOMAIN")
if deploymentStatus.GetState() == "success" {
// create status check
Expand Down Expand Up @@ -654,7 +681,6 @@ func getGithubData(accessToken string) string {
return string(respbody)
}


func getGithubUsersData(accessToken string) string {
// Get request to a set URL
req, reqerr := http.NewRequest(
Expand Down Expand Up @@ -683,4 +709,4 @@ func getGithubUsersData(accessToken string) string {

// Convert byte slice to string and return
return string(respbody)
}
}

0 comments on commit 1e57545

Please sign in to comment.