-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
405 lines (338 loc) · 11.4 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"archive/zip"
"bytes"
"compress/flate"
"context"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v2"
"github.com/go-chi/render"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/gographics/imagick.v2/imagick"
)
// Version will be set during build.
var Version = "(unknown)"
// CmdMain defines the root command.
var CmdMain = &cobra.Command{
Use: "magick-server [flags]",
Long: "A simple API server to convert (multi-page) images using ImageMagick.",
Args: cobra.NoArgs,
Version: Version,
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
PersistentPreRunE: setup,
Run: runMain,
}
// Initialize command options
func init() {
// Logging
CmdMain.Flags().String("log-level", "info", "verbosity of logging output")
CmdMain.Flags().Bool("log-json", false, "change logging format to JSON")
// Backend
CmdMain.Flags().String("listen", ":8081", "address the server should listen to")
}
// runMain is called when the main command is used.
func runMain(_ *cobra.Command, _ []string) {
// Initialization ImageMagick
imagick.Initialize()
defer imagick.Terminate()
// Create routing
router := chi.NewRouter()
router.Use(middleware.RedirectSlashes)
router.Use(middleware.RealIP)
router.Use(httplog.RequestLogger(&httplog.Logger{Logger: slog.Default()}))
router.Use(middleware.NoCache)
router.Use(middleware.Recoverer)
router.Get("/health", healthHandler())
router.Get("/version", versionHandler())
router.Post("/convert", convertHandler())
// Start HTTP server
srv := &http.Server{
Addr: viper.GetString("listen"),
Handler: router,
}
go func() {
err := srv.ListenAndServe()
if (err != nil) && (err != http.ErrServerClosed) {
slog.Error("Failed to start server", slog.Any("error", err))
os.Exit(1) //nolint:revive
}
}()
slog.Info("Server is listening...", slog.String("address", srv.Addr))
// Wait for user termination
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done
// Stop server
slog.Info("Server shutting down gracefully...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
slog.Error("Failed to gracefully shut down server", slog.Any("error", err))
os.Exit(1) //nolint:revive
}
}
// setup will set up configuration management and logging.
//
// Configuration options can be set via the command line, via a configuration file (in the current folder, at
// "/etc/magck-server/config.yaml" or at "~/.config/magick-server/config.yaml"), and via environment variables
// (all uppercase and prefixed with "MAGICK_SERVER_").
func setup(cmd *cobra.Command, _ []string) error {
// Connect all options to Viper
err := viper.BindPFlags(cmd.Flags())
if err != nil {
return fmt.Errorf("bind command line flags: %w", err)
}
// Environment variables
viper.SetEnvPrefix("MAGICK_SERVER")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
// Configuration file
viper.SetConfigName("config")
viper.AddConfigPath("/etc/magick-server")
viper.AddConfigPath("$HOME/.config/magick-server")
viper.AddConfigPath(".")
viper.ReadInConfig() //nolint:errcheck
// Logging
var level slog.Level
err = level.UnmarshalText([]byte(viper.GetString("log-level")))
if err != nil {
return fmt.Errorf("parse log level: %w", err)
}
var handler slog.Handler
if viper.GetBool("log-json") {
// Use JSON handler
handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: level})
} else {
// Use text handler
handler = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level})
}
slog.SetDefault(slog.New(handler))
return nil
}
// main is the main entry point of the command.
func main() {
if err := CmdMain.Execute(); err != nil {
slog.Error("Failed to execute command", slog.Any("error", err))
}
}
// healthHandler returns the health status.
func healthHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Return JSON with health
render.Status(r, http.StatusOK)
render.JSON(w, r, map[string]any{"status": "OK"})
}
}
// versionHandler returns the server version.
func versionHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Return JSON with version
render.Status(r, http.StatusOK)
render.JSON(w, r, Version)
}
}
// formatExtensionMap defines the supported output formats and their file extensions.
var formatExtensionMap = map[string]string{
"JPEG": "jpg", // JPEG File Interchange Format
"PNG": "png", // Portable Network Graphics
"TIFF": "tiff", // Tagged Image File Format
}
// layoutType defines the output layout to enforce.
type layoutType string
const (
layoutTypeLandscape layoutType = "LANDSCAPE" // layoutTypeLandscape forces a landscape layout.
layoutTypePortrait layoutType = "PORTRAIT" // layoutTypePortrait forces a portrait layout.
layoutTypeKeep layoutType = "KEEP" // layoutTypeKeep keeps the original layout.
)
// convertHandler converts a (multi-page) image into a Zip archive.
func convertHandler() http.HandlerFunc { //nolint
return func(w http.ResponseWriter, r *http.Request) {
// Parse density
density := 300.0
if v := r.URL.Query().Get("density"); v != "" {
d, err := strconv.ParseFloat(v, 64)
if err != nil {
slog.Error("Failed to parse validate density", slog.Any("error", err), slog.String("value", v))
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, map[string]any{"error": "invalid density"})
return
}
density = d
}
// Parse compression quality
quality := uint(85)
if v := r.URL.Query().Get("quality"); v != "" {
q, err := strconv.ParseUint(v, 10, 64)
if err != nil {
slog.Error("Failed to parse compression quality", slog.Any("error", err), slog.String("value", v))
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, map[string]any{"error": "invalid compression quality"})
return
}
quality = uint(q)
}
// Parse output format
format := "JPEG"
if v := r.URL.Query().Get("format"); v != "" {
v = strings.ToUpper(v)
if _, ok := formatExtensionMap[v]; !ok {
slog.Error("Failed to parse output format", slog.String("value", v))
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, map[string]any{"error": "invalid output format"})
return
}
format = v
}
// Parse output layout
layout := layoutTypeKeep
if v := r.URL.Query().Get("layout"); v != "" {
v = strings.ToUpper(v)
if (v != string(layoutTypeLandscape)) && (v != string(layoutTypePortrait)) && (v != string(layoutTypeKeep)) {
slog.Error("Failed to parse output layout", slog.String("value", v))
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, map[string]any{"error": "invalid output layout"})
return
}
layout = layoutType(v)
}
// Read request body
in, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("Failed to read request body", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to read request body"})
return
}
// Get a new magick wand
mw := imagick.NewMagickWand()
defer mw.Destroy()
// Set density
err = mw.SetResolution(density, density)
if err != nil {
slog.Error("Failed to set density", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to set density"})
return
}
// Read image
err = mw.ReadImageBlob(in)
if err != nil {
slog.Error("Failed to read image", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to read image"})
return
}
// Set up Zip archive
buf := &bytes.Buffer{}
zipWriter := zip.NewWriter(buf)
zipWriter.RegisterCompressor(zip.Deflate, func(o io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(o, flate.BestSpeed)
})
// Iterate through all pages
mw.ResetIterator()
for page := 0; mw.NextImage(); page++ {
// Pull current image into its own magick wand
mwi := mw.GetImage()
defer mwi.Destroy()
// Flatten image
mwm := mwi.MergeImageLayers(imagick.IMAGE_LAYER_FLATTEN)
defer mwm.Destroy()
// Set compression quality
err = mwm.SetImageCompressionQuality(quality)
if err != nil {
slog.Error("Failed to set compression quality", slog.Any("error", err), slog.Any("quality", quality))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to set compression quality"})
return
}
// Set output format
err = mwm.SetImageFormat(format)
if err != nil {
slog.Error("Failed to set output format", slog.Any("error", err), slog.String("format", format))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to set output format"})
return
}
// Force output layout
switch layout {
case layoutTypeLandscape:
// Get dimensions
width := mwm.GetImageWidth()
height := mwm.GetImageHeight()
if width < height {
// Rotate image
err := mwm.RotateImage(imagick.NewPixelWand(), -90.0)
if err != nil {
slog.Error("Failed to rotate image", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to rotate image"})
return
}
}
case layoutTypePortrait:
// Get dimensions
width := mwm.GetImageWidth()
height := mwm.GetImageHeight()
if height < width {
// Rotate image
err := mwm.RotateImage(imagick.NewPixelWand(), -90.0)
if err != nil {
slog.Error("Failed to rotate image", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to rotate image"})
return
}
}
case layoutTypeKeep:
// Do nothing
}
// Get output blob
out, err := mwm.GetImageBlob()
if err != nil {
slog.Error("Failed to get output blob", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to set output format"})
return
}
// Create new Zip archive entry
f, err := zipWriter.Create(fmt.Sprintf("%04d.%s", page, formatExtensionMap[format]))
if err != nil {
slog.Error("Failed to create new Zip archive entry", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to create new Zip archive entry"})
return
}
// Write image into Zip archive
_, err = f.Write(out)
if err != nil {
slog.Error("Failed to write image into Zip archive", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to write image into Zip archive"})
return
}
}
// Close Zip archive
err = zipWriter.Close()
if err != nil {
slog.Error("Failed to close Zip archive", slog.Any("error", err))
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, map[string]any{"error": "failed to close Zip archive"})
return
}
// We're good
render.Status(r, http.StatusOK)
render.Data(w, r, buf.Bytes())
}
}