-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
66 lines (59 loc) · 1.39 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"github.com/suconghou/videoproxy/route"
"github.com/suconghou/videoproxy/util"
)
func main() {
var (
port = flag.Int("p", 6060, "listen port")
host = flag.String("h", "", "bind address")
)
flag.Parse()
util.Log.Fatal(serve(*host, *port))
}
func serve(host string, port int) error {
http.HandleFunc("/", routeMatch)
util.Log.Printf("Starting up on port %d", port)
return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
}
func routeMatch(w http.ResponseWriter, r *http.Request) {
for _, p := range route.Route {
if p.Reg.MatchString(r.URL.Path) {
if err := p.Handler(w, r, p.Reg.FindStringSubmatch(r.URL.Path)); err != nil {
util.Log.Print(r.URL.Path, " ", err)
}
return
}
}
fallback(w, r)
}
func fallback(w http.ResponseWriter, r *http.Request) {
const index = "index.html"
files := []string{index}
if r.URL.Path != "/" {
files = []string{r.URL.Path, path.Join(r.URL.Path, index)}
}
if !tryFiles(files, w, r) {
if !tryFiles([]string{index}, w, r) {
http.NotFound(w, r)
}
}
}
func tryFiles(files []string, w http.ResponseWriter, r *http.Request) bool {
for _, file := range files {
realpath := filepath.Join("./public", file)
if f, err := os.Stat(realpath); err == nil {
if f.Mode().IsRegular() {
http.ServeFile(w, r, realpath)
return true
}
}
}
return false
}