-
Notifications
You must be signed in to change notification settings - Fork 2
/
genhtml.go
118 lines (97 loc) · 2.41 KB
/
genhtml.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
package main
import (
"fmt"
"os"
"strings"
"io/ioutil"
"os/exec"
"encoding/json"
"log"
"net/http"
"flag"
"path/filepath"
)
func listDir(dirPth string) (files []string, names []string, err error) {
suffixList := []string{".mp4", ".avi", ".wmv", ".mkv", ".mov", ".flv"}
files = make([]string, 0, 10)
names = make([]string, 0, 10)
dir, err := ioutil.ReadDir(dirPth)
if err != nil {
return nil, nil, err
}
pthSep := string(os.PathSeparator)
for _, fi := range dir {
if fi.IsDir() {
// 忽略目录
continue
}
for _, suffix := range suffixList {
suffix = strings.ToUpper(suffix)
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
//匹配文件
files = append(files, dirPth + pthSep + fi.Name())
names = append(names, fi.Name())
}
}
}
return files, names, nil
}
func getCurrentPath() string {
file, err := exec.LookPath(os.Args[0])
if err != nil {
fmt.Printf("exec.LookPath(%s), err: %s\n", os.Args[0], err)
return ""
}
path, err := filepath.Abs(file)
if err != nil {
fmt.Printf("filepath.Abs(%s), err: %s\n", file, err)
return ""
}
dir := filepath.Dir(path)
return dir
}
type TraceHandler struct {
h http.Handler
}
func genHtmlFile() {
path := getCurrentPath()
_, names, err := listDir(path)
jsonData, err := json.Marshal(names)
if err != nil {
log.Println(err)
return
}
fmt.Println(string(jsonData))
fmt.Println(path)
templateHtml, err := ioutil.ReadFile("template.html")
html := strings.Replace(string(templateHtml), "[[NAMES]]", string(jsonData), -1)
f, err := os.Create("index.html")
f.WriteString(html)
f.Close()
}
const RET_CODE_OK = 100;
func handCmdRefresh(w http.ResponseWriter, r *http.Request) {
fmt.Println("start refresh")
genHtmlFile()
w.Write([]byte{RET_CODE_OK})
}
func (r TraceHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
println("get", req.URL.Path, " from ", req.RemoteAddr)
r.h.ServeHTTP(w, req)
}
func main() {
fmt.Println("genhtml")
path := getCurrentPath()
genHtmlFile()
isServer := flag.Bool("server", false, "Run as http server")
port := flag.String("port", "8083", "Server listen port")
flag.Parse()
if *isServer {
fmt.Println("start server")
h := http.FileServer(http.Dir(path))
http.Handle("/", TraceHandler{h})
http.HandleFunc("/cmd-refresh", handCmdRefresh)
fmt.Println("Listening on port ", *port, "...")
log.Fatal("ListenAndServe: ", http.ListenAndServe(":" + *port, nil))
}
}