-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_server.go
151 lines (108 loc) · 2.95 KB
/
lib_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
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
package main
import (
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"regexp"
"net/url"
"image"
"strings"
log "github.com/sirupsen/logrus"
)
type server struct {
cfg config
watermarks map[string]image.Image
}
func (s *server) init() error {
var err error
s.watermarks = make(map[string]image.Image)
for key, host := range s.cfg.Hosts {
if len(host.Watermark) > 0 {
s.watermarks[key], err = getWatermark(host.Watermark)
if err != nil {
return err
}
}
if s.watermarks[key] != nil {
log.Infof("loaded watermark (%s): %v", key, host.Watermark)
}
}
return nil
}
func (s *server) start(h http.Handler) (io.Closer, error) {
var err error
var addr string
var listener net.Listener
srv := &http.Server{Addr: addr, Handler: h}
addr = fmt.Sprintf("%s:%d", s.cfg.IP, s.cfg.Port)
listener, err = net.Listen("tcp", addr)
if err != nil {
return nil, err
}
go func() {
err := srv.Serve(listener)
if err != nil {
log.Printf("HTTP Server Error: %v ", err)
}
}()
return io.Closer(listener), nil
}
func (s *server) getImageRequest(host string, url *url.URL) *imageRequest {
var result imageRequest
var ver = url.Query().Get("v")
if matched, _ := regexp.MatchString("^[0-9]+$", ver); len(ver) > 0 && !matched {
return nil
}
if len(ver) == 0 {
ver = "default"
}
result.RequestPath = url.Path
result.CachedFilePath = path.Join(s.cfg.CacheDir, host, ver, url.Path)
signSize := rune(url.Path[1])
result.Size = uint(s.cfg.Sizes[signSize])
RequestPattern := "^/[1-9]/.+\\.jpg$"
if matched, _ := regexp.MatchString(RequestPattern, url.Path); matched {
result.SourceFilePath = path.Join(s.cfg.Hosts[host].BaseDir, url.Path[2:])
return &result
}
return nil
}
func (s *server) handle(w http.ResponseWriter, r *http.Request) {
var err error
serverName := strings.ToLower(strings.Split(r.Host, ":")[0])
if _, ok := s.cfg.Hosts[serverName]; !ok || r.URL.Path == "/favicon.ico" {
w.WriteHeader(http.StatusNotFound)
log.Infof("404: %s", r.URL.Path)
return
}
reqImage := s.getImageRequest(serverName, r.URL)
if reqImage == nil {
w.WriteHeader(http.StatusBadRequest)
log.Infof("400: %s", r.URL.Path)
return
}
if _, err = os.Stat(reqImage.SourceFilePath); os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
log.Infof("404: %s", reqImage.RequestPath)
return
}
_, err = os.Stat(reqImage.CachedFilePath)
if os.IsNotExist(err) {
log.Infof("scale image: %s -> %s", reqImage.SourceFilePath, reqImage.CachedFilePath)
if reqImage.resize(s.watermarks[serverName]) != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorf("500: cannot resize: %s", reqImage.SourceFilePath)
return
}
}
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", s.cfg.Hosts[serverName].CacheAge))
http.ServeFile(w, r, reqImage.CachedFilePath)
log.Infof("200: %s", reqImage.RequestPath)
return
}
func (s *server) status(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}