forked from DiceDB/playground-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (51 loc) · 1.59 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
package main
import (
"context"
"log/slog"
"net/http"
"os"
"server/config"
"server/internal/db"
"server/internal/server"
"sync"
_ "github.com/joho/godotenv/autoload"
)
func main() {
configValue := config.LoadConfig()
diceDBAdminClient, err := db.InitDiceClient(configValue, true)
if err != nil {
slog.Error("Failed to initialize DiceDB Admin client: %v", slog.Any("err", err))
os.Exit(1)
}
diceDBClient, err := db.InitDiceClient(configValue, false)
if err != nil {
slog.Error("Failed to initialize DiceDB client: %v", slog.Any("err", err))
os.Exit(1)
}
// Graceful shutdown context
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
// Register a cleanup manager, this runs user DiceDB instance cleanup job at configured frequency
cleanupManager := server.NewCleanupManager(diceDBAdminClient, diceDBClient, configValue.Server.CronCleanupFrequency)
wg.Add(1)
go cleanupManager.Run(ctx, &wg)
// Create mux and register routes
mux := http.NewServeMux()
httpServer := server.NewHTTPServer(":8080", mux, diceDBAdminClient, diceDBClient, configValue.Server.RequestLimitPerMin,
configValue.Server.RequestWindowSec)
mux.HandleFunc("/health", httpServer.HealthCheck)
mux.HandleFunc("/shell/exec/{cmd}", httpServer.CliHandler)
mux.HandleFunc("/search", httpServer.SearchHandler)
wg.Add(1)
go func() {
defer wg.Done()
// Run the HTTP Server
if err := httpServer.Run(ctx); err != nil {
slog.Error("server failed: %v\n", slog.Any("err", err))
diceDBAdminClient.CloseDiceDB()
cancel()
}
}()
wg.Wait()
slog.Info("Server has shut down gracefully")
}