-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (67 loc) · 2.26 KB
/
Copy pathmain.go
File metadata and controls
82 lines (67 loc) · 2.26 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
)
func printBanner() {
banner := `
_____ _ _____ _____
| __ \ | | |_ _| / ____|
| | | | ___ ___| | _____ _ __| | _ __ ___ __ _ __ _ ___| (___ __ ___ _____
| | | |/ _ \ / __| |/ / _ \ '__| | | '_ ' _ \ / _' |/ _' |/ _ \\___ \ / _' \ \ / / _ \
| |__| | (_) | (__| < __/ | _| |_| | | | | | (_| | (_| | __/____) | (_| |\ V / __/
|_____/ \___/ \___|_|\_\___|_||_____|_| |_| |_|\__,_|\__, |\___|_____/ \__,_| \_/ \___|
for Cuban developers, by Cuban developers __/ |
|___/
`
fmt.Println(banner)
}
func main() {
configPath := flag.String("config", "config.yaml", "Path to YAML configuration file")
flag.Parse()
printBanner()
var addr string
var cacheDir string
var maxCacheAge time.Duration
config, err := LoadConfig(*configPath)
if err != nil {
log.WithError(err).Warn("No config file loaded, using defaults")
addr = ":8080"
cacheDir = ""
} else {
addr = fmt.Sprintf(":%d", config.Port)
cacheDir = config.CacheDir
config.ApplyCredentials()
maxCacheAge = config.MaxCacheAge
log.WithField("path", *configPath).Info("Loaded configuration")
log.WithFields(log.Fields{
"cache_dir": cacheDir,
"max_age": maxCacheAge,
}).Info("Using cache directory")
}
server := NewServer(addr, cacheDir, maxCacheAge)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
srv, err := server.Start(ctx)
if err != nil {
log.WithError(err).Fatal("Failed to start server")
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
sig := <-quit
log.WithField("signal", sig).Info("Received signal, shutting down gracefully")
// Cancel the context for background tasks
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.WithError(err).Fatal("Server forced to shutdown")
}
log.Info("Server stopped")
}