-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
87 lines (72 loc) · 1.79 KB
/
handlers.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
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type handlers struct {
dbIsReady bool
storage *dbClient
}
type User struct {
UUID string `json:"uuid"`
UserID string `json:"user_id"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FullName string `json:"full_name"`
PasswordFormat string `json:"password_format"`
Password string `json:"password"`
Email string `json:"email"`
}
func (h *handlers) waitForDB() {
h.storage = getDBClient()
for {
logger.Println("Checking if DB is ready")
h.dbIsReady = h.storage.isReady()
if h.dbIsReady {
logger.Println("DB is ready")
break
}
time.Sleep(2 * time.Second)
}
}
func (h *handlers) handlePing(c *gin.Context) {
if !h.dbIsReady {
c.AbortWithStatus(http.StatusServiceUnavailable)
} else {
c.String(http.StatusNoContent, "")
}
}
func (h *handlers) handleCreate(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err := h.storage.createUser(user); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusCreated, "")
}
func (h *handlers) handleModify(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
if err := h.storage.updateUser(user); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusNoContent, "")
}
func (h *handlers) handleDelete(c *gin.Context) {
uuid := c.Param("uuid")
if err := h.storage.deleteUser(uuid); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusNoContent, "")
}