forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
73 lines (63 loc) · 1.75 KB
/
server.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
package main
import (
"net/http"
"net/url"
"os"
"strconv"
"time"
)
type ServerOptions struct {
Port int
Burst int
Concurrency int
HttpCacheTtl int
HttpReadTimeout int
HttpWriteTimeout int
CORS bool
Gzip bool
EnableURLSource bool
Address string
ApiKey string
Mount string
CertFile string
KeyFile string
AlloweOrigins []*url.URL
}
func Server(o ServerOptions) error {
addr := o.Address + ":" + strconv.Itoa(o.Port)
handler := NewLog(NewServerMux(o), os.Stdout)
server := &http.Server{
Addr: addr,
Handler: handler,
MaxHeaderBytes: 1 << 20,
ReadTimeout: time.Duration(o.HttpReadTimeout) * time.Second,
WriteTimeout: time.Duration(o.HttpWriteTimeout) * time.Second,
}
return listenAndServe(server, o)
}
func listenAndServe(s *http.Server, o ServerOptions) error {
if o.CertFile != "" && o.KeyFile != "" {
return s.ListenAndServeTLS(o.CertFile, o.KeyFile)
}
return s.ListenAndServe()
}
func NewServerMux(o ServerOptions) http.Handler {
mux := http.NewServeMux()
mux.Handle("/", Middleware(indexController, o))
mux.Handle("/form", Middleware(formController, o))
mux.Handle("/health", Middleware(healthController, o))
image := ImageMiddleware(o)
mux.Handle("/resize", image(Resize))
mux.Handle("/enlarge", image(Enlarge))
mux.Handle("/extract", image(Extract))
mux.Handle("/crop", image(Crop))
mux.Handle("/rotate", image(Rotate))
mux.Handle("/flip", image(Flip))
mux.Handle("/flop", image(Flop))
mux.Handle("/thumbnail", image(Thumbnail))
mux.Handle("/zoom", image(Zoom))
mux.Handle("/convert", image(Convert))
mux.Handle("/watermark", image(Watermark))
mux.Handle("/info", image(Info))
return mux
}