forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcsweb.go
58 lines (47 loc) · 1.3 KB
/
pcsweb.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
// Package pcsweb web前端包
package pcsweb
import (
"fmt"
"github.com/GeertJohan/go.rice"
"net/http"
)
var (
staticBox *rice.Box // go.rice 文件盒子
templatesBox *rice.Box // go.rice 文件盒子
)
func boxInit() (err error) {
staticBox, err = rice.FindBox("static")
if err != nil {
return
}
templatesBox, err = rice.FindBox("template")
if err != nil {
return
}
return nil
}
// StartServer 开启web服务
func StartServer(port uint) error {
if port <= 0 || port > 65535 {
return fmt.Errorf("invalid port %d", port)
}
err := boxInit()
if err != nil {
return err
}
http.HandleFunc("/", rootMiddleware)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox())))
http.HandleFunc("/about.html", middleware(aboutPage))
http.HandleFunc("/index.html", middleware(indexPage))
http.HandleFunc("/cgi-bin/baidu/pcs/file/list", activeAuthMiddleware(fileList))
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func aboutPage(w http.ResponseWriter, r *http.Request) {
tmpl := boxTmplParse("index", "index.html", "about.html")
checkErr(tmpl.Execute(w, nil))
}
func indexPage(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
tmpl := boxTmplParse("index", "index.html", "baidu/userinfo.html")
checkErr(tmpl.Execute(w, r.Form.Get("path")))
}