-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (236 loc) · 7.75 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/rs/cors"
"github.com/siddhartham/imageutil-thumbor/action"
"github.com/siddhartham/imageutil-thumbor/model"
"github.com/siddhartham/imageutil-thumbor/thumbor"
"github.com/siddhartham/imageutil-thumbor/util"
)
func generateProxy(conf model.Config) http.Handler {
proxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
vars := mux.Vars(req)
// to be fetched from db
projectID := vars["project_id"]
// models
var project model.Project
var image model.Image
var analytic model.Analytic
// get projects
projectImageOrigin, err := action.GetProject(conf.MysqlServerConn, projectID, &project)
if err != nil {
util.LogError("generateProxy : GetProject : SELECT", err.Error())
return
}
imgPath := vars["image"]
//is media storage
if conf.IsMedia {
imgPath = fmt.Sprintf("https://%s.%s/%s/%s", conf.BucketName, conf.MediaEndpoint, conf.MediaStorage, imgPath)
}
util.LogInfo("generateProxy : GetImage : Image Path", imgPath)
// get image
err = action.GetImage(conf.MysqlServerConn, conf.IsSmart, projectImageOrigin, imgPath, vars["transformation"], &project, &image, &analytic)
if err != nil {
util.LogWarning("generateProxy : GetImage : SELECT", err.Error())
}
// get or generate thumbor
finalScheme := project.Protocol
finalHost := conf.CdnOrigin
finalPath := strings.Replace(image.CdnPath, fmt.Sprintf("%s/", conf.ResultStorage), "", 1)
image.ImgURL = fmt.Sprintf("%s://%s%s", finalScheme, finalHost, finalPath)
if finalPath == "" {
finalScheme = "http" //thumbor is internal
finalHost = conf.Host
finalPath = thumbor.GetThumborUrl(conf, projectImageOrigin, image, analytic)
} else {
req.Host = conf.CdnOrigin
analytic.ImageID = image.ID
go action.SaveAnalytic(conf.MysqlServerConn, image, analytic, 0, 1, 0)
}
//rewrite url
req.URL = &url.URL{
Scheme: finalScheme,
Host: finalHost,
Path: finalPath,
RawPath: finalPath,
}
util.LogInfo("generateProxy : FinalURL", fmt.Sprintf("%s", req.URL))
//set headers
req.Header.Add("X-Forwarded-Host", req.Host)
req.Header.Add("X-Origin-Host", finalHost)
util.LogInfo("generateProxy : X-Forwarded-Host", req.Host)
util.LogInfo("generateProxy : X-Origin-Host", finalHost)
}, Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
}}
return proxy
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
args := os.Args[1:]
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
port := os.Getenv("PORT")
thumborHost := os.Getenv("THUMBORHOST")
if len(args) > 0 {
p, err := strconv.Atoi(args[0])
if err == nil {
port = fmt.Sprintf("%s%d", ":900", p)
thumborHost = fmt.Sprintf("%s%d", "127.0.0.1:800", p)
}
}
//General config, should load from env in prod
sc := &model.ServerConf{
Host: os.Getenv("HOST"),
Port: port,
ThumborHost: thumborHost,
ThumborSecret: os.Getenv("THUMBORSECRET"),
MysqlServerHost: os.Getenv("MYSQLSERVERHOST"),
MysqlServerPort: os.Getenv("MYSQLSERVERPORT"),
MysqlServerUsername: os.Getenv("MYSQLSERVERUSERNAME"),
MysqlServerPassword: os.Getenv("MYSQLSERVERPASSWORD"),
MysqlServerDatabase: os.Getenv("MYSQLSERVERDATABASE"),
CdnOrigin: os.Getenv("CDNORIGIN"), //"cdn.imageutil.io",
BucketName: os.Getenv("BUCKETNAME"),
ResultStorage: os.Getenv("RESULTSTORAGE"),
MediaStorage: os.Getenv("MEDIASTORAGE"),
MediaEndpoint: os.Getenv("MEDIAENDPOINT"),
}
//Mysql connection
mysqlConnStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", sc.MysqlServerUsername, sc.MysqlServerPassword, sc.MysqlServerHost, sc.MysqlServerPort, sc.MysqlServerDatabase)
db, err := sql.Open("mysql", mysqlConnStr)
if err != nil {
panic(err.Error())
}
defer db.Close()
//main router
r := mux.NewRouter()
//fixed routes
r.HandleFunc("/health", action.HealthCheckHandler)
r.HandleFunc("/upload/{uploadToken}/{fileName}", func(w http.ResponseWriter, r *http.Request) {
action.UploadHandler(db, w, r)
})
//reverse proxy routes
configuration := []model.Config{
model.Config{
Path: "/{project_id}/media/{transformation}/smart/{image:.*}",
Host: sc.ThumborHost,
IsSmart: true,
IsMedia: true,
Secret: sc.ThumborSecret,
MysqlServerConn: db,
CdnOrigin: sc.CdnOrigin,
BucketName: sc.BucketName,
ResultStorage: sc.ResultStorage,
MediaStorage: sc.MediaStorage,
MediaEndpoint: sc.MediaEndpoint,
},
model.Config{
Path: "/{project_id}/media/{transformation}/{image:.*}",
Host: sc.ThumborHost,
IsSmart: false,
IsMedia: true,
Secret: sc.ThumborSecret,
MysqlServerConn: db,
CdnOrigin: sc.CdnOrigin,
BucketName: sc.BucketName,
ResultStorage: sc.ResultStorage,
MediaStorage: sc.MediaStorage,
MediaEndpoint: sc.MediaEndpoint,
},
model.Config{
Path: "/{project_id}/{transformation}/smart/{image:.*}",
Host: sc.ThumborHost,
IsSmart: true,
IsMedia: false,
Secret: sc.ThumborSecret,
MysqlServerConn: db,
CdnOrigin: sc.CdnOrigin,
BucketName: sc.BucketName,
ResultStorage: sc.ResultStorage,
MediaStorage: sc.MediaStorage,
MediaEndpoint: sc.MediaEndpoint,
},
model.Config{
Path: "/{project_id}/{transformation}/{image:.*}",
Host: sc.ThumborHost,
IsSmart: false,
IsMedia: false,
Secret: sc.ThumborSecret,
MysqlServerConn: db,
CdnOrigin: sc.CdnOrigin,
BucketName: sc.BucketName,
ResultStorage: sc.ResultStorage,
MediaStorage: sc.MediaStorage,
MediaEndpoint: sc.MediaEndpoint,
},
}
for _, conf := range configuration {
proxy := generateProxy(conf)
r.HandleFunc(conf.Path, func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
})
}
//Start server
util.LogInfo("Starting imageutil server on port", sc.Port)
// log.Fatal(http.ListenAndServe(sc.Port, r))
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
corsObj := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
handler := corsObj.Handler(r)
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0%s", sc.Port),
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15, //upload
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: handler, // Pass our instance of gorilla/mux in.
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
util.LogError("main : server", err.Error())
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
util.LogInfo("main : server", "shutting down")
os.Exit(0)
}