1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/main.go

139 lines
2.3 KiB
Go
Raw Normal View History

2017-06-20 15:58:55 +02:00
package main
import (
2020-02-27 17:44:59 +02:00
"context"
2021-11-15 11:33:32 +02:00
"flag"
2020-02-27 17:44:59 +02:00
"fmt"
2017-10-17 14:23:48 +02:00
"os"
"os/signal"
"syscall"
"time"
2021-04-26 13:52:50 +02:00
log "github.com/sirupsen/logrus"
"go.uber.org/automaxprocs/maxprocs"
2021-04-26 13:52:50 +02:00
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/errorreport"
2022-07-29 11:16:24 +02:00
"github.com/imgproxy/imgproxy/v3/gliblog"
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/logger"
"github.com/imgproxy/imgproxy/v3/memory"
"github.com/imgproxy/imgproxy/v3/metrics"
"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
"github.com/imgproxy/imgproxy/v3/options"
2022-07-18 13:49:04 +02:00
"github.com/imgproxy/imgproxy/v3/processing"
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/version"
"github.com/imgproxy/imgproxy/v3/vips"
2021-04-26 13:52:50 +02:00
)
2018-10-05 17:17:36 +02:00
2020-02-27 17:44:59 +02:00
func initialize() error {
2021-04-26 13:52:50 +02:00
if err := logger.Init(); err != nil {
return err
}
2019-08-09 12:12:04 +02:00
2022-07-29 11:16:24 +02:00
gliblog.Init()
maxprocs.Set(maxprocs.Logger(log.Debugf))
2021-04-26 13:52:50 +02:00
if err := config.Configure(); err != nil {
2020-02-27 17:44:59 +02:00
return err
}
2021-04-26 13:52:50 +02:00
if err := metrics.Init(); err != nil {
2020-02-27 17:44:59 +02:00
return err
}
2021-04-26 13:52:50 +02:00
if err := imagedata.Init(); err != nil {
2020-02-27 17:44:59 +02:00
return err
}
2021-04-26 13:52:50 +02:00
initProcessingHandler()
2021-03-23 11:37:25 +02:00
2021-04-26 13:52:50 +02:00
errorreport.Init()
2020-02-27 17:44:59 +02:00
2021-04-26 13:52:50 +02:00
if err := vips.Init(); err != nil {
2020-02-27 17:44:59 +02:00
return err
}
2022-07-18 13:49:04 +02:00
if err := processing.ValidatePreferredFormats(); err != nil {
vips.Shutdown()
return err
}
2021-04-26 13:52:50 +02:00
if err := options.ParsePresets(config.Presets); err != nil {
vips.Shutdown()
2020-02-27 17:44:59 +02:00
return err
}
2019-11-11 13:54:46 +02:00
2021-04-26 13:52:50 +02:00
if err := options.ValidatePresets(); err != nil {
vips.Shutdown()
2020-02-27 17:44:59 +02:00
return err
2019-11-11 13:54:46 +02:00
}
2020-02-27 17:44:59 +02:00
return nil
2019-06-06 15:26:00 +02:00
}
2021-04-26 13:52:50 +02:00
func shutdown() {
vips.Shutdown()
metrics.Stop()
errorreport.Close()
2021-04-26 13:52:50 +02:00
}
2020-02-27 17:44:59 +02:00
func run() error {
if err := initialize(); err != nil {
return err
2020-02-04 13:48:33 +02:00
}
2021-04-26 13:52:50 +02:00
defer shutdown()
2021-03-23 11:37:25 +02:00
go func() {
var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
2021-04-26 13:52:50 +02:00
for range time.Tick(time.Duration(config.FreeMemoryInterval) * time.Second) {
memory.Free()
if logMemStats {
2021-04-26 13:52:50 +02:00
memory.LogStats()
}
}
}()
2020-02-27 17:44:59 +02:00
ctx, cancel := context.WithCancel(context.Background())
2021-04-26 13:52:50 +02:00
if err := prometheus.StartServer(cancel); err != nil {
2021-03-23 11:36:08 +02:00
return err
2020-02-27 17:44:59 +02:00
}
s, err := startServer(cancel)
if err != nil {
return err
}
2020-02-04 12:18:09 +02:00
defer shutdownServer(s)
2017-06-20 15:58:55 +02:00
2017-10-17 14:23:48 +02:00
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
2017-06-20 15:58:55 +02:00
2020-02-27 17:44:59 +02:00
select {
case <-ctx.Done():
case <-stop:
}
return nil
}
func main() {
2021-11-15 11:33:32 +02:00
flag.Parse()
switch flag.Arg(0) {
case "health":
os.Exit(healthcheck())
case "version":
fmt.Println(version.Version())
os.Exit(0)
2020-02-27 17:44:59 +02:00
}
if err := run(); err != nil {
2021-04-26 13:52:50 +02:00
log.Fatal(err)
2020-02-27 17:44:59 +02:00
}
2017-06-20 15:58:55 +02:00
}