-
Notifications
You must be signed in to change notification settings - Fork 345
/
main.go
57 lines (43 loc) · 1.08 KB
/
main.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
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/greycodee/wechat-backup/api"
)
var apiPort = flag.String("p", "9999", "api port")
var basePath = flag.String("f", "", "wechat bak folder")
//go:embed static
var staticFile embed.FS
//go:embed index.html
var indexHtml []byte
func init() {
flag.Parse()
if basePath == nil || *basePath == "" {
panic("please specify basePath")
}
}
func main() {
fsys, _ := fs.Sub(staticFile, "static")
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))
})
apiRouter.Engine.Static("/media/", *basePath)
apiRouter.Engine.NoRoute(func(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, "/")
})
httpServer := &http.Server{
Addr: fmt.Sprintf(":%s", *apiPort),
Handler: apiRouter.Router(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
httpServer.ListenAndServe()
}