Skip to content

Commit

Permalink
add fileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
greycodee committed Feb 1, 2023
1 parent f4cc7d4 commit 1a13370
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 83 deletions.
44 changes: 37 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ const (
)

type Api struct {
wcdb *db.WCDB
wcdb *db.WCDB
Engine *gin.Engine
}

func New(dbPath string) *Api {
a := &Api{}
a.wcdb = db.InitWCDB(dbPath)
a.Engine = gin.New()
a.Engine.Use(gin.Recovery())

return a
}

Expand All @@ -46,12 +50,38 @@ func (a Api) detailHandler(c *gin.Context) {
c.JSON(200, a.wcdb.ChatDetailList(talker, pageIndex-1, pageSize))
}

func (a Api) Router() http.Handler {
g := gin.New()
g.Use(gin.Recovery())
func (a Api) userInfoHandler(c *gin.Context) {
userName := c.Query("username")
c.JSON(200, a.wcdb.GetUserInfo(userName))
}

func (a Api) myInfoHandler(c *gin.Context) {
c.JSON(200, a.wcdb.GetMyInfo())
}

func (a Api) imgHandler(c *gin.Context) {
msgId := c.Query("msgId")
c.JSON(200, a.wcdb.GetImgPath(msgId))
}

func (a Api) videoHandler(c *gin.Context) {
msgId := c.Query("msgId")
c.JSON(200, a.wcdb.GetVideoPath(msgId))
}

g.GET(ListApi, a.listHandler)
g.GET(DetailApi, a.detailHandler)
func (a Api) voiceHandler(c *gin.Context) {
msgId := c.Query("msgId")
c.JSON(200, a.wcdb.GetVoicePath(msgId))
}

func (a Api) Router() http.Handler {
a.Engine.GET(ListApi, a.listHandler)
a.Engine.GET(DetailApi, a.detailHandler)
a.Engine.GET(UserInfoApi, a.userInfoHandler)
a.Engine.GET(MyInfoApi, a.myInfoHandler)
a.Engine.GET(ImgApi, a.imgHandler)
a.Engine.GET(VideoApi, a.videoHandler)
a.Engine.GET(VoiceApi, a.voiceHandler)

return g
return a.Engine
}
File renamed without changes.
60 changes: 27 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package main

import (
"embed"
"flag"
"fmt"
"log"
"io/fs"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/greycodee/wechat-backup/api"
"golang.org/x/sync/errgroup"
)

var apiPort = flag.String("ap", "9999", "api port")
var htmlPort = flag.String("hp", "9991", "html port")
var basePath = flag.String("f", "", "wechat bak folder")
var basePath = flag.String("f", "/home/zheng/coding/wechatbak/dest/1058116fa5360697125915314cf3c3a0", "wechat bak folder")

//go:embed static
var staticFile embed.FS

//go:embed index.html
var indexHtml []byte

func init() {
flag.Parse()
Expand All @@ -23,41 +28,30 @@ func init() {
}
}

var (
g errgroup.Group
)

func htmlRouter() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.StaticFS("/", http.Dir("./static"))
return e
}

func main() {
htmlRouter := &http.Server{
Addr: fmt.Sprintf(":%s", *htmlPort),
Handler: htmlRouter(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

apiRouter := &http.Server{
Addr: fmt.Sprintf(":%s", *apiPort),
Handler: api.New(*basePath).Router(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
fsys, _ := fs.Sub(staticFile, "static")

g.Go(func() error {
return htmlRouter.ListenAndServe()
apiRouter := api.New(*basePath)

apiRouter.Engine.StaticFS("/static", http.FS(fsys))
apiRouter.Engine.GET("/", func(ctx *gin.Context) {
ctx.Header("Content-Type", "text/html")
ctx.String(http.StatusOK, string(indexHtml))
})

g.Go(func() error {
return apiRouter.ListenAndServe()
apiRouter.Engine.Static("/media/", *basePath)

apiRouter.Engine.NoRoute(func(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, "/")
})

if err := g.Wait(); err != nil {
log.Fatal(err)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%s", *apiPort),
Handler: apiRouter.Router(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

httpServer.ListenAndServe()
}
Binary file removed static/.DS_Store
Binary file not shown.
15 changes: 0 additions & 15 deletions static/asset-manifest.json

This file was deleted.

File renamed without changes.
File renamed without changes.
Binary file removed static/favicon.ico
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed static/logo192.png
Binary file not shown.
Binary file removed static/logo512.png
Binary file not shown.
25 changes: 0 additions & 25 deletions static/manifest.json

This file was deleted.

3 changes: 0 additions & 3 deletions static/robots.txt

This file was deleted.

0 comments on commit 1a13370

Please sign in to comment.