forked from Moonlight-Zhao/go-project-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sever.go
53 lines (48 loc) · 1.09 KB
/
sever.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
package main
import (
"github.com/Moonlight-Zhao/go-project-example/handler"
"github.com/Moonlight-Zhao/go-project-example/repository"
"github.com/Moonlight-Zhao/go-project-example/util"
"gopkg.in/gin-gonic/gin.v1"
"os"
)
func main() {
if Init() != nil {
os.Exit(-1)
}
r := gin.Default()
// gin.Default()默认添加了日志中间件
//r.Use(gin.Logger())
// 测试接口
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
// page信息获取接口
r.GET("/community/page/get/:id", func(c *gin.Context) {
topicId := c.Param("id")
data := handler.QueryPageInfo(topicId)
c.JSON(200, data)
})
// 新增post接口
r.POST("/community/post/do", func(c *gin.Context) {
uid, _ := c.GetPostForm("uid")
topicId, _ := c.GetPostForm("topic_id")
content, _ := c.GetPostForm("content")
data := handler.PublishPost(uid, topicId, content)
c.JSON(200, data)
})
if r.Run() != nil {
return
}
}
func Init() error {
if err := repository.Init(); err != nil {
return err
}
if err := util.InitLogger(); err != nil {
return err
}
return nil
}