Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
greycodee committed Jan 31, 2023
1 parent 4d4df41 commit f4cc7d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
33 changes: 26 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/greycodee/wechat-backup/db"
)

var wcdb *db.WCDB

const (
ListApi = "/api/chat/list"
DetailApi = "/api/chat/detail"
Expand All @@ -20,19 +18,40 @@ const (
VoiceApi = "/api/media/voice"
)

func ListHandler(c *gin.Context) {
type Api struct {
wcdb *db.WCDB
}

func New(dbPath string) *Api {
a := &Api{}
a.wcdb = db.InitWCDB(dbPath)
return a
}

func (a Api) listHandler(c *gin.Context) {
pageIndex, _ := strconv.Atoi(c.DefaultQuery("pageIndex", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
name := c.Query("name")
all, _ := strconv.ParseBool(c.Query("all"))
all, _ := strconv.ParseBool(c.DefaultQuery("all", "false"))

result := a.wcdb.ChatList(pageIndex-1, pageSize, all, name)
// 聊天列表
c.JSON(200, wcdb.ChatList(pageIndex-1, pageSize, all, name))
c.JSON(200, result)
}

func ApiRouter() http.Handler {
func (a Api) detailHandler(c *gin.Context) {
pageIndex, _ := strconv.Atoi(c.DefaultQuery("pageIndex", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
talker := c.Query("talker")
c.JSON(200, a.wcdb.ChatDetailList(talker, pageIndex-1, pageSize))
}

func (a Api) Router() http.Handler {
g := gin.New()
g.Use(gin.Recovery())
g.GET(ListApi, ListHandler)

g.GET(ListApi, a.listHandler)
g.GET(DetailApi, a.detailHandler)

return g
}
18 changes: 1 addition & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@ func htmlRouter() http.Handler {
return e
}

// func apiRouter() http.Handler {
// e := gin.New()
// e.Use(gin.Recovery())
// e.GET("/", func(c *gin.Context) {
// c.JSON(
// http.StatusOK,
// gin.H{
// "code": http.StatusOK,
// "message": "Welcome server 02",
// },
// )
// })

// return e
// }

func main() {
htmlRouter := &http.Server{
Addr: fmt.Sprintf(":%s", *htmlPort),
Expand All @@ -60,7 +44,7 @@ func main() {

apiRouter := &http.Server{
Addr: fmt.Sprintf(":%s", *apiPort),
Handler: api.ApiRouter(),
Handler: api.New(*basePath).Router(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
Expand Down

0 comments on commit f4cc7d4

Please sign in to comment.