-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
74 lines (68 loc) · 1.58 KB
/
Copy pathapi.go
File metadata and controls
74 lines (68 loc) · 1.58 KB
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
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func StartApi() {
r := gin.Default()
r.Static("/html", "html")
r.StaticFile("/", "html/index.html")
gr := r.Group("/api")
gr.POST("/user", Set)
gr.GET("/user/search", Search)
gr.DELETE("/user/:id", Del)
gin.SetMode(gin.DebugMode)
httpServer := &http.Server{
Addr: Config.Server.APIListen,
Handler: r,
ReadTimeout: time.Duration(Config.Server.ReadTimeOut) * time.Second,
WriteTimeout: time.Duration(Config.Server.WriteTimeOut) * time.Second,
MaxHeaderBytes: 1 << 20,
}
logrus.Infof("Start HTTP Service Listening %s", Config.Server.APIListen)
httpServer.ListenAndServe()
}
func Search(c *gin.Context) {
email, ok := c.GetQuery("email")
if !ok {
c.Status(http.StatusBadRequest)
return
}
result := make([]*userResp, 0)
for _, v := range userStorage.List() {
if v.Email == email {
result = append(result, v.TOVO())
}
}
c.JSON(http.StatusOK, result)
}
func Set(c *gin.Context) {
param := &userParam{}
if err := c.ShouldBind(param); err != nil {
c.Status(http.StatusBadRequest)
return
}
user := param.TOUser()
check := userStorage.Get(user.Sign())
if check != nil {
c.Status(http.StatusNotAcceptable)
return
}
err := userStorage.Set(user)
if err != nil {
c.Status(http.StatusInternalServerError)
return
}
GitWork(user)
c.JSON(http.StatusOK, user.Sign())
}
func Del(c *gin.Context) {
id := c.Param("id")
if err := userStorage.Del(id); err != nil {
c.Status(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
}