-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (122 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 (
"flag"
"fmt"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/gokyle/filecache"
)
var (
loc = time.FixedZone("KST", +9*60*60)
fURLPrefix string
)
func main() {
fExpire := flag.Int("e",
filecache.DefaultExpireItem,
"maximum number of seconds between accesses a file can stay in the cache")
fGarbage := flag.Int("g", filecache.DefaultEvery,
"scan the cache for expired items every <n> seconds")
fItems := flag.Int("n",
100, //filecache.DefaultMaxItems,
"max number of files to store in the cache")
fPort := flag.Int("p", 8080, "port to listen on")
fSize := flag.Int64("s", filecache.DefaultMaxSize, "max file size to cache")
fDumpCache := flag.String("d", "",
"dump cache stats duration; by default, this is turned off. Must be parsable with time.ParseDuration.")
flag.StringVar(&fURLPrefix, "f", "", "remove prefix subpath in url.PATH")
flag.Parse()
if !strings.HasPrefix(fURLPrefix, "/") {
fURLPrefix = "/" + fURLPrefix
}
srvWD := "."
if flag.NArg() > 0 {
srvWD = flag.Arg(0)
}
srvWD, err := filepath.Abs(srvWD)
if err != nil {
log.Fatal(err)
}
cache := &filecache.FileCache{
Root: srvWD,
MaxSize: *fSize,
MaxItems: *fItems,
ExpireItem: *fExpire,
Every: *fGarbage,
}
cache.Start()
if *fDumpCache != "" {
go func() {
dur, err := time.ParseDuration(*fDumpCache)
if err != nil {
fmt.Printf("[-] couldn't parse %s: %v\n",
*fDumpCache, err)
return
}
tk := time.NewTicker(dur)
defer tk.Stop()
for range tk.C {
displayCacheStats(cache)
}
}()
}
http.HandleFunc("/",
func(w http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path
if !strings.HasPrefix(urlPath, "/") {
urlPath = "/" + urlPath
}
urlPathPre, urlPathSur := splitURLPath(urlPath)
if urlPathSur == "" || strings.Compare(fURLPrefix, urlPathPre) != 0 {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "404")
return
}
// log.Printf(
// "urlPrefix=%s, urlPath=%s, urlPathPre=%s, urlPathSur=%s",
// urlPrefix, urlPath, urlPathPre, urlPathSur,
// )
r.URL.Path = urlPathSur
filecache.HttpHandler(cache)(w, r)
},
)
srvAddr := fmt.Sprintf(":%d", *fPort)
fmt.Printf("serving %s on %s\n", srvWD, srvAddr)
if err := http.ListenAndServe(srvAddr, nil); err != nil {
log.Fatal(err)
}
}
func displayCacheStats(cache *filecache.FileCache) {
fmt.Printf("-----[ cache stats: %s ]-----\n",
time.Now().In(loc).Format(time.RFC3339))
fmt.Printf("files cached: %d (max: %d)\n", cache.Size(),
cache.MaxItems)
fmt.Printf("cache size: %d bytes (will cache files up to %d bytes)\n",
cache.FileSize(), cache.MaxSize)
cachedFiles := cache.StoredFiles()
fmt.Println("[ cached files ]")
for _, name := range cachedFiles {
fmt.Printf("\t* %s\n", name)
}
fmt.Printf("\n\n")
}
/*
func splitURLPath(urlPath string) (urlPathPre, urlPathSur string) {
firstURLPrefixEndIdx := strings.Index(urlPath[1:], "/") + 1
switch firstURLPrefixEndIdx {
case 0, len(urlPath): // wholeURL is prefix
urlPathPre = urlPath[:firstURLPrefixEndIdx]
urlPathSur = ""
default:
urlPathPre = urlPath[:firstURLPrefixEndIdx]
urlPathSur = urlPath[firstURLPrefixEndIdx:]
}
return
}
*/
func splitURLPath(urlPath string) (urlPathPre, urlPathSur string) {
prefixLen := len(fURLPrefix)
return urlPath[:prefixLen], urlPath[prefixLen:]
}