-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (113 loc) · 3.54 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
package main
import (
"bytes"
"io"
"log"
"net/http"
"strconv"
"time"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
models "github.com/otomato/softcat/models"
)
// postComponents adds a new component to the list.
func postComponents(c *gin.Context) {
var newComponent models.Component
if err := c.BindJSON(&newComponent); err != nil {
log.Println("error binding component", err)
c.AbortWithError(http.StatusBadRequest, err)
return
}
if err := models.PostComponents(newComponent); err != nil {
log.Println("error inserting component:", err)
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.IndentedJSON(http.StatusCreated, newComponent)
}
// postTeams adds a new team to the list.
func postTeams(c *gin.Context) {
var newTeam models.Team
if err := c.BindJSON(&newTeam); err != nil {
return
}
models.PostTeams(newTeam)
c.IndentedJSON(http.StatusCreated, newTeam)
}
// getComponents responds with the list of all components as JSON.
func getComponents(c *gin.Context) {
components := models.GetComponents()
c.IndentedJSON(http.StatusOK, components)
}
// getTeams responds with the list of all teams as JSON.
func getTeams(c *gin.Context) {
teams := models.GetTeams()
c.IndentedJSON(http.StatusOK, teams)
}
// getComponentsByTeam responds with the list of all components for a given team as JSON.
func getComponentsByTeam(c *gin.Context) {
teamID, _ := strconv.Atoi(c.Param("teamID"))
teamComponents := models.GetComponentsByTeam(teamID)
c.IndentedJSON(http.StatusOK, teamComponents)
}
// getComponentsById responds with the component with the given ID as JSON.
func getComponentsById(c *gin.Context) {
componentID := c.Param("componentID")
component, err := models.GetComponentByID(componentID)
if err == nil {
c.IndentedJSON(http.StatusOK, component)
return
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "component not found"})
}
// getTeamsById responds with the team with the given ID as JSON.
func getTeamById(c *gin.Context) {
teamID, _ := strconv.Atoi(c.Param("teamID"))
team, err := models.GetTeamByID(teamID)
if err == nil {
c.IndentedJSON(http.StatusOK, team)
return
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "team not found"})
}
func generateIdenticons() {
for {
var components []models.Component = models.GetComponents()
for _, component := range components {
img, _ := models.GetImgByComponent(component.ID)
if img == nil {
log.Println("generating identicon for component", component.ID)
res, err := http.Post("http://localhost:8081",
"application/json",
bytes.NewBuffer([]byte(`{"id": `+strconv.Itoa(component.ID)+`, "name": "`+component.Name+`"}`)))
if err != nil {
log.Println("failed to generate identicon for component", component.ID, err)
} else if res.StatusCode != http.StatusOK {
log.Println("failed to generate identicon for component", component.ID)
} else {
identicon, _ := io.ReadAll(res.Body)
img := models.Image{ID: component.ID, Image: identicon}
models.PostImg(img)
}
}
}
log.Println(time.Now().UTC())
time.Sleep(10000 * time.Millisecond)
}
}
func main() {
models.ConnectDB()
//generate identicons in the background
go generateIdenticons()
// run gin server
r := gin.Default()
pprof.Register(r)
r.GET("/components", getComponents)
r.POST("/components", postComponents)
r.GET("/components/:componentID", getComponentsById)
r.GET("/teams", getTeams)
r.POST("/teams", postTeams)
r.GET("/teams/:teamID", getTeamById)
r.GET("/teams/:teamID/components", getComponentsByTeam)
r.Run(":8080")
}