-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapis.go
64 lines (52 loc) · 1.48 KB
/
apis.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
package main
import (
"encoding/json"
"jutra/router"
"net/http"
"strconv"
)
type BranchStatusDto struct {
BranchId int64 `json:"branch_id"`
BranchName string `json:"branch_name"`
Status string `json:"last_build_status"`
Date string `json:"date"`
}
func serveApiListProjects(context *router.HttpContext) {
projects := DAO.GetAllProjects()
response, jsonErr := json.Marshal(projects)
if jsonErr != nil {
http.Error(context.Resp, jsonErr.Error(), http.StatusInternalServerError)
return
}
context.Resp.Header().Set("Content-Type", "text/json")
context.Resp.Write(response)
}
func serveApiBranchesStatus(context *router.HttpContext) {
projectIdStr := context.PathParams["projectId"]
projectId, err := strconv.ParseInt(projectIdStr, 10, 64)
if err != nil {
http.Error(context.Resp, err.Error(), http.StatusInternalServerError)
return
}
branches := DAO.GetAllBranchesInfo(projectId, nil)
dtos := make([]*BranchStatusDto, 0, len(branches))
for _, v := range branches {
dto := new(BranchStatusDto)
dto.BranchId = v.Id
dto.BranchName = v.BranchName
if v.LastLauchFailed() {
dto.Status = "FAILED"
} else {
dto.Status = "HEALTHY"
}
dto.Date = v.CreationDate.Format("2006-01-02 15:04:05")
dtos = append(dtos, dto)
}
response, jsonErr := json.Marshal(dtos)
if jsonErr != nil {
http.Error(context.Resp, jsonErr.Error(), http.StatusInternalServerError)
return
}
context.Resp.Header().Set("Content-Type", "text/json")
context.Resp.Write(response)
}