-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
66 lines (54 loc) · 1.27 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 (
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
"willnorris.com/go/imageproxy"
)
func main() {
// Set server address
addr := os.Getenv("ADDRESS")
if addr == "" {
log.Fatal("No address provided for the imageproxy")
}
// Set cache
var cache httpcache.Cache
d := diskv.New(diskv.Options{
BasePath: "/tmp/imageproxy",
CacheSizeMax: 500 * 1024 * 1024,
// For file "c0ffee", store file as "c0/ff/c0ffee"
Transform: func(s string) []string { return []string{s[0:2], s[2:4]} },
})
cache = diskcache.NewWithDiskv(d)
// Create proxy
p := imageproxy.NewProxy(nil, cache)
// Create whitelist
if os.Getenv("WHITELIST") != "" {
p.Referrers = strings.Split(os.Getenv("WHITELIST"), ",")
}
// Create baseurl
if os.Getenv("BASEURL") != "" {
base, err := url.Parse(os.Getenv("BASEURL"))
if err != nil {
log.Fatalf("error parsing baseURL: %v", err)
}
p.DefaultBaseURL = base
p.AllowHosts = []string{base.Host}
}
p.ScaleUp = true
server := &http.Server{
Addr: addr,
Handler: p,
}
fmt.Printf("imageproxy listening on " + addr)
err := server.ListenAndServe()
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}