/
register_controllers.go
244 lines (207 loc) · 7.66 KB
/
register_controllers.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
package controllers
const ControllersRegisterTemplate = `// generated code - do not edit
package controllers
import (
"fmt"
"log"
"net/http"
"{{PkgPathRoot}}/orm"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
// genQuery return the name of the column
func genQuery(columnName string) string {
return fmt.Sprintf("%s = ?", columnName)
}
// A GenericError is the default error message that is generated.
// For certain status codes there are more appropriate error structures.
//
// swagger:response genericError
type GenericError struct {
// in: body
Body struct {
Code int32 ` + "`" + `json:"code"` + "`" + `
Message string ` + "`" + `json:"message"` + "`" + `
} ` + "`" + `json:"body"` + "`" + `
}
// A ValidationError is an that is generated for validation failures.
// It has the same fields as a generic error but adds a Field property.
//
// swagger:response validationError
type ValidationError struct {
// in: body
Body struct {
Code int32 ` + "`" + `json:"code"` + "`" + `
Message string ` + "`" + `json:"message"` + "`" + `
Field string ` + "`" + `json:"field"` + "`" + `
} ` + "`" + `json:"body"` + "`" + `
}
// registerControllers register controllers
func registerControllers(r *gin.Engine) {
v1 := r.Group("/api/{{PkgPathRoot}}")
{ // insertion point for registrations{{` + string(rune(ControllersDeclaration)) + `}}
v1.GET("/v1/commitfrombacknb", GetController().GetLastCommitFromBackNb)
v1.GET("/v1/pushfromfrontnb", GetController().GetLastPushFromFrontNb)
v1.GET("/v1/ws/commitfrombacknb", GetController().onWebSocketRequestForCommitFromBackNb)
v1.GET("/v1/ws/stage", GetController().onWebSocketRequestForBackRepoContent)
}
}
// onWebSocketRequestForCommitFromBackNb is a function that is started each time
// a web socket request is received
//
// 1. upgrade the incomming web connection to a web socket
// 1. it subscribe to the backend commit number broadcaster
// 1. it stays live and pool for incomming backend commit number broadcast and forward
// them on the web socket connection
func (controller *Controller) onWebSocketRequestForCommitFromBackNb(c *gin.Context) {
// log.Println("Stack {{PkgPathRoot}}, onWebSocketRequestForCommitFromBackNb")
// Upgrader specifies parameters for upgrading an HTTP connection to a
// WebSocket connection.
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
origin := r.Header.Get("Origin")
return origin == "http://localhost:8080" || origin == "http://localhost:4200"
},
}
wsConnection, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println(err)
return
}
defer wsConnection.Close()
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetLastCommitFromBackNb", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack {{PkgPathRoot}}, Unkown stack", stackPath)
}
updateCommitBackRepoNbChannel := backRepo.SubscribeToCommitNb()
for nbCommitBackRepo := range updateCommitBackRepoNbChannel {
// Send elapsed time as a string over the WebSocket connection
err = wsConnection.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("%d", nbCommitBackRepo)))
if err != nil {
log.Println("client no longer receiver web socket message, assuming it is no longer alive, closing websocket handler")
fmt.Println(err)
return
}
}
}
// onWebSocketRequestForBackRepoContent is a function that is started each time
// a web socket request is received
//
// 1. upgrade the incomming web connection to a web socket
// 1. it subscribe to the backend commit number broadcaster
// 1. it stays live and pool for incomming backend commit number broadcast and forward
// them on the web socket connection
func (controller *Controller) onWebSocketRequestForBackRepoContent(c *gin.Context) {
// log.Println("Stack {{PkgPathRoot}}, onWebSocketRequestForBackRepoContent")
// Upgrader specifies parameters for upgrading an HTTP connection to a
// WebSocket connection.
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
origin := r.Header.Get("Origin")
return origin == "http://localhost:8080" || origin == "http://localhost:4200"
},
}
wsConnection, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println(err)
return
}
defer wsConnection.Close()
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetLastCommitFromBackNb", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack {{PkgPathRoot}}, Unkown stack", stackPath)
}
updateCommitBackRepoNbChannel := backRepo.SubscribeToCommitNb()
backRepoData := new(orm.BackRepoData)
orm.CopyBackRepoToBackRepoData(backRepo, backRepoData)
err = wsConnection.WriteJSON(backRepoData)
// log.Println("Stack {{PkgPathRoot}}, onWebSocketRequestForBackRepoContent, first sent back repo of", stackPath)
if err != nil {
log.Println("client no longer receiver web socket message, assuming it is no longer alive, closing websocket handler")
fmt.Println(err)
return
}
for nbCommitBackRepo := range updateCommitBackRepoNbChannel {
_ = nbCommitBackRepo
backRepoData := new(orm.BackRepoData)
orm.CopyBackRepoToBackRepoData(backRepo, backRepoData)
// Send backRepo data
err = wsConnection.WriteJSON(backRepoData)
// log.Println("Stack {{PkgPathRoot}}, onWebSocketRequestForBackRepoContent, sent back repo of", stackPath)
if err != nil {
log.Println("client no longer receiver web socket message, assuming it is no longer alive, closing websocket handler")
fmt.Println(err)
return
}
}
}
// swagger:route GET /commitfrombacknb backrepo GetLastCommitFromBackNb
func (controller *Controller) GetLastCommitFromBackNb(c *gin.Context) {
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetLastCommitFromBackNb", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack {{PkgPathRoot}}/models, Unkown stack", stackPath)
}
res := backRepo.GetLastCommitFromBackNb()
c.JSON(http.StatusOK, res)
}
// swagger:route GET /pushfromfrontnb backrepo GetLastPushFromFrontNb
func (controller *Controller) GetLastPushFromFrontNb(c *gin.Context) {
values := c.Request.URL.Query()
stackPath := ""
if len(values) == 1 {
value := values["GONG__StackPath"]
if len(value) == 1 {
stackPath = value[0]
// log.Println("GetLastPushFromFrontNb", "GONG__StackPath", stackPath)
}
}
backRepo := controller.Map_BackRepos[stackPath]
if backRepo == nil {
log.Panic("Stack {{PkgPathRoot}}/models, Unkown stack", stackPath)
}
res := backRepo.GetLastPushFromFrontNb()
c.JSON(http.StatusOK, res)
}
`
type ControllersRegistrationsSubTemplateInsertions int
const (
ControllersDeclaration ControllersRegistrationsSubTemplateInsertions = iota
)
var ControllersRegistrationsSubTemplate map[string]string = // new line
map[string]string{
string(rune(ControllersDeclaration)): `
v1.GET("/v1/{{structname}}s", GetController().Get{{Structname}}s)
v1.GET("/v1/{{structname}}s/:id", GetController().Get{{Structname}})
v1.POST("/v1/{{structname}}s", GetController().Post{{Structname}})
v1.PATCH("/v1/{{structname}}s/:id", GetController().Update{{Structname}})
v1.PUT("/v1/{{structname}}s/:id", GetController().Update{{Structname}})
v1.DELETE("/v1/{{structname}}s/:id", GetController().Delete{{Structname}})
`,
}