forked from greycodee/wechat-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (121 loc) · 3.32 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"embed"
"encoding/json"
"flag"
"io/fs"
"log"
"net"
"net/http"
"strconv"
"github.com/greycodee/wechat-backup/db"
)
var wcdb *db.WCDB
//go:embed static
var htmlFile embed.FS
var serverPort = flag.String("p", "9999", "server port")
var basePath = flag.String("f", "", "wechat bak folder")
func init() {
flag.Parse()
if basePath == nil || *basePath == "" {
panic("please specify basePath")
}
}
func main() {
wcdb = db.InitWCDB(*basePath)
fsys, _ := fs.Sub(htmlFile, "static")
staticHandle := http.FileServer(http.FS(fsys))
// 文件路由
fs := http.FileServer(http.Dir(*basePath))
http.Handle(db.MediaPathPrefix, http.StripPrefix(db.MediaPathPrefix, fs))
http.Handle("/", staticHandle)
http.Handle("/api/", route())
log.Println("server start")
interfaceAddr, _ := net.InterfaceAddrs()
for _, address := range interfaceAddr {
ipNet, _ := address.(*net.IPNet)
if ipNet.IP.To4() != nil {
log.Printf("server addr http://%s:%s", ipNet.IP.String(), *serverPort)
}
}
err := http.ListenAndServe(":"+*serverPort, nil)
if err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}
func route() http.Handler {
return &API{}
}
type API struct {
}
func (api *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
apiMap[path](w, r)
}
var apiMap = map[string]func(w http.ResponseWriter, r *http.Request){
"/api/chat/list": func(w http.ResponseWriter, r *http.Request) {
// 聊天列表
params := r.URL.Query()
pageIndex, _ := strconv.Atoi(params["pageIndex"][0])
pageSize, _ := strconv.Atoi(params["pageSize"][0])
names, _ := params["name"]
name := ""
if len(names) > 0 {
name = names[0]
}
all, _ := strconv.ParseBool(params["all"][0])
result, err := json.Marshal(wcdb.ChatList(pageIndex-1, pageSize, all, name))
if err != nil {
log.Fatalf("json marshal error: %v", err)
}
w.Write(result)
},
"/api/chat/detail": func(w http.ResponseWriter, r *http.Request) {
//聊天记录
params := r.URL.Query()
talker := params["talker"][0]
pageIndex, _ := strconv.Atoi(params["pageIndex"][0])
pageSize, _ := strconv.Atoi(params["pageSize"][0])
result, err := json.Marshal(wcdb.ChatDetailList(talker, pageIndex-1, pageSize))
if err != nil {
log.Fatalf("json marshal error: %v", err)
}
w.Write(result)
},
"/api/user/info": func(w http.ResponseWriter, r *http.Request) {
// 用户信息
params := r.URL.Query()
username := params["username"][0]
result, err := json.Marshal(wcdb.GetUserInfo(username))
if err != nil {
log.Fatalf("json marshal error: %v", err)
}
w.Write(result)
},
"/api/user/myinfo": func(w http.ResponseWriter, r *http.Request) {
// 自己的信息
result, err := json.Marshal(wcdb.GetMyInfo())
if err != nil {
log.Fatalf("json marshal error: %v", err)
}
w.Write(result)
},
"/api/media/img": func(w http.ResponseWriter, r *http.Request) {
// 图片
params := r.URL.Query()
msgId := params["msgId"][0]
w.Write([]byte(wcdb.GetImgPath(msgId)))
},
"/api/media/video": func(w http.ResponseWriter, r *http.Request) {
// 视频
params := r.URL.Query()
msgId := params["msgId"][0]
w.Write([]byte(wcdb.GetVideoPath(msgId)))
},
"/api/media/voice": func(w http.ResponseWriter, r *http.Request) {
// 语音
params := r.URL.Query()
msgId := params["msgId"][0]
w.Write([]byte(wcdb.GetVoicePath(msgId)))
},
}