-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (41 loc) · 899 Bytes
/
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
package main
import (
"fmt"
"github.com/gobuffalo/packr/v2"
cache "github.com/victorspringer/http-cache"
"github.com/victorspringer/http-cache/adapter/memory"
"net/http"
"os"
"time"
)
var PORT = os.Getenv("PORT")
func checkEnv() {
if PORT == "" {
PORT = "8812"
}
}
func main() {
checkEnv()
memcached, err := memory.NewAdapter(
memory.AdapterWithAlgorithm(memory.LFU),
memory.AdapterWithCapacity(10000000),
)
if err != nil {
fmt.Println(err)
}
cacheClient, err := cache.NewClient(
cache.ClientWithAdapter(memcached),
cache.ClientWithTTL(10 * time.Minute),
cache.ClientWithRefreshKey("opn"),
)
if err != nil {
fmt.Println(err)
}
box := packr.New("Assets", "./assets")
fileServer := http.FileServer(box)
cached := cacheClient.Middleware(fileServer)
http.Handle("/", cached)
if err := http.ListenAndServe(":" + PORT, nil); err != nil {
panic(err)
}
}