-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
60 lines (49 loc) · 999 Bytes
/
services.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
package services
import (
"fmt"
"net/http"
"github.com/jdxj/words/config"
v1 "github.com/jdxj/words/services/api/v1"
"github.com/gin-gonic/gin"
)
func NewRouter() *gin.Engine {
mode := config.GetMode()
if mode == gin.ReleaseMode {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
r.GET("", Home)
r.POST("sessions", PostSessions)
apiRG := r.Group("api")
apiRG.Use(CheckToken)
v1RG := apiRG.Group("v1")
{
// favorites
v1RG.GET("favorites/:userID", v1.GetFavorites)
v1RG.POST("favorites/:word")
v1RG.DELETE("favorites/:word")
// words
wordsRG := v1RG.Group("words")
wordsRG.Use(v1.CheckWordMW)
{
wordsRG.GET(":word", v1.Search)
wordsRG.GET(":word/voice", v1.Voice)
}
}
return r
}
// home
func Home(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}
func NewServer() *http.Server {
bind := fmt.Sprintf(":%s", config.GetPort())
router := NewRouter()
srv := &http.Server{
Addr: bind,
Handler: router,
}
return srv
}