feat: sync db

This commit is contained in:
fedorHub 2025-08-28 16:13:36 +02:00
parent 5d404d5c3a
commit 1827efe475
No known key found for this signature in database
GPG Key ID: 7FDE5B4177850E7D
2 changed files with 41 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
"github.com/fedor-git/wg-portal-2/internal/app/webhooks"
"github.com/fedor-git/wg-portal-2/internal/app/wireguard"
"github.com/fedor-git/wg-portal-2/internal/config"
"github.com/fedor-git/wg-portal-2/internal/sync"
)
// main entry point for WireGuard Portal
@ -182,6 +183,11 @@ func main() {
go metricsServer.Run(ctx)
go webSrv.Run(ctx, cfg.Web.ListeningAddress)
// sync WireGuard state periodically in the background
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go sync.StartPeriodicSync(ctx, wgManager, 30*time.Second)
slog.Info("Application startup complete")
// wait until context gets cancelled

35
internal/sync/periodic.go Normal file
View File

@ -0,0 +1,35 @@
package sync
import (
"context"
"log"
"time"
)
func startPeriodicSync(ctx context.Context, wgManager WireguardSynchronizer, interval time.Duration) {
log.Printf("✅ Starting periodic WireGuard sync every %s", interval)
ticker := time.NewTicker(interval)
defer ticker.Stop()
log.Println("Running initial sync on startup...")
if err := wgManager.SyncDevice(); err != nil {
log.Printf("ERROR during initial sync: %v", err)
}
for {
select {
case <-ticker.C:
log.Println("⚙️ Ticker fired: running periodic sync...")
if err := wgManager.SyncDevice(); err != nil {
log.Printf("ERROR during periodic sync: %v", err)
}
case <-ctx.Done():
log.Println("Stopping periodic sync.")
return
}
}
}
type WireguardSynchronizer interface {
SyncDevice() error
}