-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (85 loc) · 3.52 KB
/
main.go
File metadata and controls
100 lines (85 loc) · 3.52 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
// Package main is the entry point for the xray-subscription service.
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/alchemylink/raven-subscribe/internal/api"
"github.com/alchemylink/raven-subscribe/internal/config"
"github.com/alchemylink/raven-subscribe/internal/database"
"github.com/alchemylink/raven-subscribe/internal/syncer"
)
func main() {
configPath := flag.String("config", "config.json", "path to config file")
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
// ── Config ──────────────────────────────────────────────────────────────
cfg, err := config.Load(*configPath)
if err != nil {
log.Fatalf("Config error: %v", err)
}
log.Printf("Server host: %s | Config dir: %s | Listen: %s",
cfg.ServerHost, cfg.ConfigDir, cfg.ListenAddr)
// ── Database ────────────────────────────────────────────────────────────
db, err := database.New(cfg.DBPath)
if err != nil {
log.Fatalf("Database error: %v", err)
}
defer func() {
if err := db.Close(); err != nil {
log.Printf("DB close error: %v", err)
}
}()
log.Printf("Database: %s", cfg.DBPath)
// ── Syncer ──────────────────────────────────────────────────────────────
sync := syncer.New(cfg, db)
log.Println("Running initial sync...")
if err := sync.Sync(); err != nil {
log.Printf("Initial sync warning: %v", err)
}
// Restore API-created users to Xray after restart
sync.RestoreOnStartup()
// API server needs sync capability
srv := api.NewServer(cfg, db, sync)
// Apply current killswitch state to Xray inbounds via gRPC (idempotent).
// When killswitch is disabled but Xray loaded the fallback inbound from its
// config files on its own startup, this removes them so the listener state
// matches the DB flag. Safe no-op when xray_api_addr or fallback tags unset.
srv.ReconcileKillSwitchOnStartup()
// Start background sync
syncCtx, syncCancel := context.WithCancel(context.Background())
defer syncCancel()
go sync.Start(syncCtx)
// ── HTTP Server ─────────────────────────────────────────────────────────
httpServer := &http.Server{
Addr: cfg.ListenAddr,
Handler: srv.Router(),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
go func() {
log.Printf("Listening on %s", cfg.ListenAddr)
log.Printf("Subscription URL format: %s/sub/<token>", cfg.BaseURL)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server error: %v", err)
}
}()
// ── Graceful Shutdown ───────────────────────────────────────────────────
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down...")
syncCancel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Printf("HTTP shutdown error: %v", err)
}
log.Println("Stopped")
}