-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
180 lines (147 loc) · 4.45 KB
/
main.go
File metadata and controls
180 lines (147 loc) · 4.45 KB
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
package main
import (
"log"
"strings"
"time"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/healthcheck"
"go.uber.org/zap"
"github.com/caarlos0/env/v11"
"github.com/gofiber/fiber/v2"
"media-proxy/config"
"media-proxy/metrics"
fiberprometheus "media-proxy/middlewares/prometheus"
"media-proxy/routes"
"media-proxy/storage"
"github.com/dgraph-io/ristretto/v2"
)
var logger *zap.Logger
func main() {
logger, _ = zap.NewProduction()
defer func(logger *zap.Logger) {
err := logger.Sync()
if err != nil {
log.Fatal(err)
}
}(logger)
config, err := env.ParseAs[config.Config]()
if err != nil {
logger.Fatal(err.Error())
}
if config.Metrics == nil {
metrics := true
config.Metrics = &metrics
}
cacheConfig := &ristretto.Config[string, routes.CacheValue]{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
}
// HTTP cache configuration for middleware
httpCacheConfig := &ristretto.Config[string, []byte]{
NumCounters: 1e6, // number of keys to track frequency of (1M).
MaxCost: 1 << 28, // maximum cost of cache (256MB).
BufferItems: 64, // number of keys per Get buffer.
}
if config.HTTPCacheTTL == 0 {
config.HTTPCacheTTL = 1800 // 30 minutes
}
if config.CacheBufferItems > 0 {
cacheConfig.BufferItems = config.CacheBufferItems
}
if config.CacheMaxCost > 0 {
cacheConfig.MaxCost = config.CacheMaxCost
}
if config.CacheNumCounters > 0 {
cacheConfig.NumCounters = config.CacheNumCounters
}
if config.CacheTTL == 0 {
config.CacheTTL = 1800 // 30 minutes
}
cacheStore, err := ristretto.NewCache(cacheConfig)
if err != nil {
logger.Fatal(err.Error())
}
// Create HTTP cache for middleware
httpCacheStore, err := ristretto.NewCache(httpCacheConfig)
if err != nil {
logger.Fatal(err.Error())
}
// Initialize optional S3 cache
s3cache, s3err := routes.NewS3Cache(
config.S3Enabled,
config.S3Endpoint,
config.S3AccessKeyID,
config.S3SecretAccessKey,
config.S3Bucket,
config.S3SSL,
config.S3Prefix,
)
if s3err != nil {
logger.Warn("failed to initialize S3 cache", zap.Error(s3err))
}
// Initialize optional Redis upload tracker
uploadTracker, redisErr := routes.NewRedisUploadTracker(
config.RedisAddr,
config.RedisPassword,
config.RedisDB,
)
if redisErr != nil {
logger.Warn("failed to initialize Redis upload tracker", zap.Error(redisErr))
}
if uploadTracker != nil {
defer uploadTracker.Close()
}
// Configure body limit based on chunk size or max video size
bodyLimit := 4 * 1024 * 1024 // Default 4MB
if config.UploadingEnabled {
if config.ChunkSize > 0 {
bodyLimit = int(config.ChunkSize) + (1 * 1024 * 1024) // ChunkSize + 1MB buffer
} else if config.MaxVideoSize > 0 {
bodyLimit = config.MaxVideoSize * 1024 * 1024
} else {
bodyLimit = 100 * 1024 * 1024 // Default 100MB if no limits set
}
}
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Prefork: config.Prefork,
BodyLimit: bodyLimit,
})
prometheusModule := fiberprometheus.New("media-proxy")
prometheusModule.RegisterAt(app, "/metrics")
prometheusRegistry := prometheusModule.GetRegistry()
metrics := metrics.InitializeMetrics(prometheusRegistry, prometheusModule.GetConstLabels())
if *config.Metrics {
app.Use(prometheusModule.Middleware)
}
app.Use(healthcheck.New())
app.Use(compress.New())
app.Use(etag.New())
app.Use(cache.New(cache.Config{
Expiration: time.Minute * 10,
Storage: storage.NewRistrettoStorage(httpCacheStore),
Next: func(c *fiber.Ctx) bool {
if strings.HasPrefix(c.Path(), "/videos/") && !strings.HasPrefix(c.Path(), "/videos/preview/") {
return true
}
return false
},
KeyGenerator: func(c *fiber.Ctx) string {
if strings.HasPrefix(c.Path(), "/videos/") && !strings.HasPrefix(c.Path(), "/videos/preview/") {
return c.Path() + "?" + string(c.Request().Header.Peek("Range"))
}
return c.Path()
},
}))
routes.RegisterImageRoutes(logger, cacheStore, &config, app, metrics, s3cache)
routes.RegisterVideoRoutes(logger, cacheStore, &config, app, metrics, s3cache, uploadTracker)
address := config.Address
if address == "" {
address = ":3000"
}
log.Fatal(app.Listen(address))
logger.Info("server started", zap.String("address", address))
}