-
Notifications
You must be signed in to change notification settings - Fork 198
/
advanced.go
171 lines (158 loc) · 4.93 KB
/
advanced.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Package advanced provides an advanced example.
package main
import (
"crypto/sha256"
"flag"
"net/http"
"os"
"runtime"
"time"
"github.com/disintegration/gift"
"github.com/pierrre/githubhook"
"github.com/pierrre/imageserver"
imageserver_cache "github.com/pierrre/imageserver/cache"
imageserver_cache_memory "github.com/pierrre/imageserver/cache/memory"
imageserver_http "github.com/pierrre/imageserver/http"
imageserver_http_crop "github.com/pierrre/imageserver/http/crop"
imageserver_http_gamma "github.com/pierrre/imageserver/http/gamma"
imageserver_http_gift "github.com/pierrre/imageserver/http/gift"
imageserver_http_image "github.com/pierrre/imageserver/http/image"
imageserver_image "github.com/pierrre/imageserver/image"
_ "github.com/pierrre/imageserver/image/bmp"
imageserver_image_crop "github.com/pierrre/imageserver/image/crop"
imageserver_image_gamma "github.com/pierrre/imageserver/image/gamma"
imageserver_image_gif "github.com/pierrre/imageserver/image/gif"
imageserver_image_gift "github.com/pierrre/imageserver/image/gift"
_ "github.com/pierrre/imageserver/image/jpeg"
_ "github.com/pierrre/imageserver/image/png"
_ "github.com/pierrre/imageserver/image/tiff"
imageserver_testdata "github.com/pierrre/imageserver/testdata"
)
var (
flagHTTP = ":8080"
flagGitHubWebhookSecret string
flagCache = int64(128 * (1 << 20))
)
func main() {
parseFlags()
startHTTPServer()
}
func parseFlags() {
flag.StringVar(&flagHTTP, "http", flagHTTP, "HTTP")
flag.StringVar(&flagGitHubWebhookSecret, "github-webhook-secret", flagGitHubWebhookSecret, "GitHub webhook secret")
flag.Int64Var(&flagCache, "cache", flagCache, "Cache")
flag.Parse()
}
func startHTTPServer() {
err := http.ListenAndServe(flagHTTP, newHTTPHandler())
if err != nil {
panic(err)
}
}
func newHTTPHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/", http.StripPrefix("/", newImageHTTPHandler()))
mux.Handle("/favicon.ico", http.NotFoundHandler())
if h := newGitHubWebhookHTTPHandler(); h != nil {
mux.Handle("/github_webhook", h)
}
return mux
}
func newGitHubWebhookHTTPHandler() http.Handler {
if flagGitHubWebhookSecret == "" {
return nil
}
return &githubhook.Handler{
Secret: flagGitHubWebhookSecret,
Delivery: func(event string, deliveryID string, payload any) {
if event == "push" {
time.AfterFunc(5*time.Second, func() {
os.Exit(0)
})
}
},
}
}
func newImageHTTPHandler() http.Handler {
var handler http.Handler = &imageserver_http.Handler{
Parser: imageserver_http.ListParser([]imageserver_http.Parser{
&imageserver_http.SourcePathParser{},
&imageserver_http_crop.Parser{},
&imageserver_http_gift.RotateParser{},
&imageserver_http_gift.ResizeParser{},
&imageserver_http_image.FormatParser{},
&imageserver_http_image.QualityParser{},
&imageserver_http_gamma.CorrectionParser{},
}),
Server: newServer(),
ETagFunc: imageserver_http.NewParamsHashETagFunc(sha256.New),
}
handler = &imageserver_http.ExpiresHandler{
Handler: handler,
Expires: 7 * 24 * time.Hour,
}
handler = &imageserver_http.CacheControlPublicHandler{
Handler: handler,
}
return handler
}
func newServer() imageserver.Server {
srv := imageserver_testdata.Server
srv = newServerImage(srv)
srv = newServerLimit(srv)
srv = newServerCacheMemory(srv)
return srv
}
func newServerImage(srv imageserver.Server) imageserver.Server {
basicHdr := &imageserver_image.Handler{
Processor: imageserver_image_gamma.NewCorrectionProcessor(
imageserver_image.ListProcessor([]imageserver_image.Processor{
&imageserver_image_crop.Processor{},
&imageserver_image_gift.RotateProcessor{
DefaultInterpolation: gift.CubicInterpolation,
},
&imageserver_image_gift.ResizeProcessor{
DefaultResampling: gift.LanczosResampling,
MaxWidth: 2048,
MaxHeight: 2048,
},
}),
true,
),
}
gifHdr := &imageserver_image_gif.FallbackHandler{
Handler: &imageserver_image_gif.Handler{
Processor: &imageserver_image_gif.SimpleProcessor{
Processor: imageserver_image.ListProcessor([]imageserver_image.Processor{
&imageserver_image_crop.Processor{},
&imageserver_image_gift.RotateProcessor{
DefaultInterpolation: gift.NearestNeighborInterpolation,
},
&imageserver_image_gift.ResizeProcessor{
DefaultResampling: gift.NearestNeighborResampling,
MaxWidth: 1024,
MaxHeight: 1024,
},
}),
},
},
Fallback: basicHdr,
}
return &imageserver.HandlerServer{
Server: srv,
Handler: gifHdr,
}
}
func newServerLimit(srv imageserver.Server) imageserver.Server {
return imageserver.NewLimitServer(srv, runtime.GOMAXPROCS(0)*2)
}
func newServerCacheMemory(srv imageserver.Server) imageserver.Server {
if flagCache <= 0 {
return srv
}
return &imageserver_cache.Server{
Server: srv,
Cache: imageserver_cache_memory.New(flagCache),
KeyGenerator: imageserver_cache.NewParamsHashKeyGenerator(sha256.New),
}
}