/
main.go
157 lines (130 loc) · 3.18 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"time"
expirableCache "github.com/odeke-em/cache"
watnot "github.com/odeke-em/watnot-dev/src"
)
const (
Byte = 1
KByte = 1024 * Byte
MByte = 1024 * KByte
)
var MaxThresholdCacheSize = int64(80 * MByte)
var globalDataCache = expirableCache.New()
func relToPublicPath(p string) string {
return path.Join(".", "public", p)
}
func newExpirableValue30MinuteOffset(data interface{}) *expirableCache.ExpirableValue {
return expirableCache.NewExpirableValueWithOffset(data, uint64(time.Hour))
}
func retrievePublicResource(subPath string) (data string, err error) {
key := relToPublicPath(subPath)
retr, ok := globalDataCache.Get(key)
if ok {
value := retr.Value()
data, ok = value.(string)
if ok {
fmt.Printf("cache hit for %v\n", key)
return
}
// TODO: Decide if a failed cast should
// fall through and graduate to a cache miss
}
return memoizePublicResource(subPath)
}
func memoizePublicResource(subPath string) (data string, err error) {
key := relToPublicPath(subPath)
fInfo, statErr := os.Stat(key)
if statErr != nil {
err = statErr
return
}
if fInfo == nil {
err = fmt.Errorf("nil fInfo")
return
}
if fInfo.IsDir() {
err = fmt.Errorf("dir cannot be read from")
return
}
fileSize := fInfo.Size()
if fileSize >= MaxThresholdCacheSize {
err = fmt.Errorf("entity too large, cannot fit in cache")
return
}
fh, fErr := os.Open(key)
if fErr != nil {
err = fErr
return
}
inMemoryBuffer := make([]byte, fileSize)
io.ReadAtLeast(fh, inMemoryBuffer, 1)
fh.Close()
data = string(inMemoryBuffer)
value := newExpirableValue30MinuteOffset(data)
if inserted := globalDataCache.Put(key, value); inserted {
fmt.Printf("cached: %v\n", key)
}
return
}
func main() {
http.HandleFunc("/", rootHandler)
go func() {
cancellation := make(chan bool)
publicDirCacheEvicter(cancellation)
}()
err := http.ListenAndServe(":8000", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "running server encountered err: %v\n", err)
}
}
func publicDirCacheEvicter(done chan bool) {
go func() {
publicRawPath, err := filepath.Abs("./public")
fmt.Println(publicRawPath)
if err != nil {
fmt.Fprintf(os.Stderr, "publicDirCacheEvicter: absPath err: %v\n", err)
return
}
changedFilesChan := watnot.NewWatcher(publicRawPath)
for absPath := range changedFilesChan {
// Check to see if we've been cancelled
select {
case sig := <-done:
if sig {
return
}
default:
}
relPath := filepath.Base(absPath)
if err != nil {
fmt.Fprintf(os.Stderr, "publicDirCacheEvicter: %q %q err: %v\n", absPath, relPath, err)
continue
}
toPublicPath := relToPublicPath(relPath)
// Invalidate the cache
if _, existed := globalDataCache.Remove(toPublicPath); existed {
fmt.Println("evicted", toPublicPath)
}
}
}()
}
func rootHandler(res http.ResponseWriter, req *http.Request) {
url := req.URL.String()
if url == "" || url == "/" {
url = "index.html"
}
data, err := retrievePublicResource(url)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
http.Error(res, "error encountered please try again", 500)
return
}
fmt.Fprintf(res, "%s", data)
}