-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (116 loc) · 3.97 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
package main
import (
"os"
"os/signal"
"path"
"syscall"
"github.com/blesswinsamuel/media-proxy/internal/cache"
"github.com/blesswinsamuel/media-proxy/internal/config"
"github.com/blesswinsamuel/media-proxy/internal/loader"
"github.com/blesswinsamuel/media-proxy/internal/mediaprocessor"
"github.com/blesswinsamuel/media-proxy/internal/server"
"github.com/davidbyttow/govips/v2/vips"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
)
// NewLogger creates a new logger based on the current configuration
func NewLogger(logLevel string) zerolog.Logger {
// Setup logger
log := zerolog.New(os.Stderr).With().Timestamp().Logger()
ll, err := zerolog.ParseLevel(logLevel)
if err != nil {
ll = zerolog.DebugLevel
}
return log.With().Logger().Level(ll)
}
func main() {
config, err := config.ParseConfig(nil)
if err != nil {
log := NewLogger(zerolog.DebugLevel.String())
log.Fatal().Err(err).Msg("failed to parse config")
}
// Setup logger
log := NewLogger(config.LogLevel)
// Perform config validation
config.Validate()
// Set up libvips concurrency level
vips.LoggingSettings(func(messageDomain string, messageLevel vips.LogLevel, message string) {
var messageLevelDescription string
switch messageLevel {
case vips.LogLevelError:
messageLevelDescription = "error"
case vips.LogLevelCritical:
messageLevelDescription = "critical"
case vips.LogLevelWarning:
messageLevelDescription = "warning"
case vips.LogLevelMessage:
messageLevelDescription = "message"
case vips.LogLevelInfo:
messageLevelDescription = "info"
case vips.LogLevelDebug:
messageLevelDescription = "debug"
}
log.Debug().Str("domain", messageDomain).Str("level", messageLevelDescription).Msg(message)
}, vips.LogLevelWarning)
vips.Startup(&vips.Config{
// https://www.libvips.org/API/current/VipsOperation.html#vips-concurrency-set
ConcurrencyLevel: 4,
// https://www.libvips.org/API/current/VipsOperation.html#vips-cache-set-max-files
MaxCacheFiles: 0,
// https://www.libvips.org/API/current/VipsOperation.html#vips-cache-set-max-mem
MaxCacheMem: 50 * 1024 * 1024,
// https://www.libvips.org/API/current/VipsOperation.html#vips-cache-set-max
MaxCacheSize: 100,
// https://www.libvips.org/API/current/libvips-vips.html#vips-leak-set
ReportLeaks: true,
// https://www.libvips.org/API/current/VipsOperation.html#vips-cache-set-trace
// CacheTrace :
CollectStats: true,
})
defer vips.Shutdown()
prometheus.MustRegister(mediaprocessor.NewVipsPrometheusCollector())
// go func() {
// for {
// // runtimeStats := vips.RuntimeStats{}
// // vips.ReadRuntimeStats(&runtimeStats)
// // fmt.Println(runtimeStats)
// // memoryStats := vips.MemoryStats{}
// // vips.ReadVipsMemStats(&memoryStats)
// // fmt.Println(memoryStats)
// time.Sleep(5 * time.Second)
// vips.PrintObjectReport("main")
// }
// }()
var loaderCache, metadataCache, resultCache cache.Cache
if config.EnableLoaderCache.Value {
loaderCache = cache.NewFsCache(path.Join(config.CacheDir, "original"))
} else {
loaderCache = cache.NewNoopCache()
}
if config.EnableResultCache.Value {
metadataCache = cache.NewFsCache(path.Join(config.CacheDir, "metadata"))
resultCache = cache.NewFsCache(path.Join(config.CacheDir, "result"))
} else {
metadataCache = cache.NewNoopCache()
resultCache = cache.NewNoopCache()
}
mediaProcessor := mediaprocessor.NewMediaProcessor()
loader := loader.NewHTTPLoader(config.BaseURL)
server := server.NewServer(server.ServerConfig{
Port: config.Port,
MetricsPort: config.MetricsPort,
Secret: config.Secret,
EnableUnsafe: bool(config.EnableUnsafe.Value),
AutoAvif: true,
AutoWebp: true,
Concurrency: config.Concurrency,
}, mediaProcessor, loader, loaderCache, metadataCache, resultCache)
// Start the server
server.Start()
// graceful shutdown
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
log.Info().Msg("Shutting down...")
server.Stop()
}