add admin panel
This commit is contained in:
@@ -9,131 +9,60 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Server manages snapshots from multiple clients with S3 support
|
||||
type Server struct {
|
||||
clients map[string]*ClientConfig
|
||||
snapshots map[string][]*SnapshotMetadata
|
||||
mu sync.RWMutex
|
||||
db *Database
|
||||
s3Backend *S3Backend
|
||||
localBackend *LocalBackend
|
||||
metadataFile string
|
||||
configFile string
|
||||
}
|
||||
|
||||
// New creates a new snapshot server
|
||||
func New(configFile, metadataFile string, s3Backend *S3Backend, localBackend *LocalBackend) *Server {
|
||||
// New creates a new snapshot server with SQLite database
|
||||
func New(dbPath string, s3Backend *S3Backend, localBackend *LocalBackend) (*Server, error) {
|
||||
db, err := NewDatabase(dbPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize database: %v", err)
|
||||
}
|
||||
|
||||
// Create default client if none exists
|
||||
if err := db.CreateDefaultClient(); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("failed to create default client: %v", err)
|
||||
}
|
||||
|
||||
// Create default admin if none exists
|
||||
if err := db.CreateDefaultAdmin(); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("failed to create default admin: %v", err)
|
||||
}
|
||||
|
||||
// Clean expired sessions
|
||||
db.CleanExpiredSessions()
|
||||
|
||||
s := &Server{
|
||||
clients: make(map[string]*ClientConfig),
|
||||
snapshots: make(map[string][]*SnapshotMetadata),
|
||||
db: db,
|
||||
s3Backend: s3Backend,
|
||||
localBackend: localBackend,
|
||||
metadataFile: metadataFile,
|
||||
configFile: configFile,
|
||||
}
|
||||
|
||||
s.loadConfig()
|
||||
s.loadMetadata()
|
||||
|
||||
return s
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Server) loadConfig() {
|
||||
data, err := os.ReadFile(s.configFile)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Could not read config file: %v", err)
|
||||
// Create default config
|
||||
s.clients["client1"] = &ClientConfig{
|
||||
ClientID: "client1",
|
||||
APIKey: hashAPIKey("secret123"),
|
||||
MaxSizeBytes: 100 * 1024 * 1024 * 1024,
|
||||
Dataset: "backup/client1",
|
||||
Enabled: true,
|
||||
StorageType: "s3",
|
||||
}
|
||||
s.saveConfig()
|
||||
return
|
||||
}
|
||||
|
||||
var clients []*ClientConfig
|
||||
if err := json.Unmarshal(data, &clients); err != nil {
|
||||
log.Printf("Error parsing config: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, client := range clients {
|
||||
s.clients[client.ClientID] = client
|
||||
}
|
||||
|
||||
log.Printf("Loaded %d client configurations", len(s.clients))
|
||||
}
|
||||
|
||||
func (s *Server) saveConfig() {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var clients []*ClientConfig
|
||||
for _, client := range s.clients {
|
||||
clients = append(clients, client)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(clients, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("Error marshaling config: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.WriteFile(s.configFile, data, 0600); err != nil {
|
||||
log.Printf("Error writing config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) loadMetadata() {
|
||||
data, err := os.ReadFile(s.metadataFile)
|
||||
if err != nil {
|
||||
log.Printf("No existing metadata file, starting fresh")
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &s.snapshots); err != nil {
|
||||
log.Printf("Error parsing metadata: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
totalSnapshots := 0
|
||||
for _, snaps := range s.snapshots {
|
||||
totalSnapshots += len(snaps)
|
||||
}
|
||||
log.Printf("Loaded metadata for %d snapshots", totalSnapshots)
|
||||
}
|
||||
|
||||
func (s *Server) saveMetadata() {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
data, err := json.MarshalIndent(s.snapshots, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("Error marshaling metadata: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.WriteFile(s.metadataFile, data, 0600); err != nil {
|
||||
log.Printf("Error writing metadata: %v", err)
|
||||
}
|
||||
// Close closes the database connection
|
||||
func (s *Server) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
func (s *Server) authenticate(clientID, apiKey string) bool {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
client, exists := s.clients[clientID]
|
||||
if !exists || !client.Enabled {
|
||||
if !client.Enabled {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -141,22 +70,13 @@ func (s *Server) authenticate(clientID, apiKey string) bool {
|
||||
}
|
||||
|
||||
func (s *Server) getClientUsage(clientID string) int64 {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var total int64
|
||||
for _, snap := range s.snapshots[clientID] {
|
||||
total += snap.SizeBytes
|
||||
}
|
||||
return total
|
||||
usage, _ := s.db.GetClientUsage(clientID)
|
||||
return usage
|
||||
}
|
||||
|
||||
func (s *Server) canAcceptSnapshot(clientID string, estimatedSize int64) (bool, string) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
client, exists := s.clients[clientID]
|
||||
if !exists {
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
return false, "Client not found"
|
||||
}
|
||||
|
||||
@@ -171,74 +91,69 @@ func (s *Server) canAcceptSnapshot(clientID string, estimatedSize int64) (bool,
|
||||
}
|
||||
|
||||
func (s *Server) rotateSnapshots(clientID string) (int, int64) {
|
||||
// First pass: collect snapshots to delete while holding lock
|
||||
s.mu.Lock()
|
||||
client, exists := s.clients[clientID]
|
||||
if !exists {
|
||||
s.mu.Unlock()
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
snapshots := s.snapshots[clientID]
|
||||
if len(snapshots) == 0 {
|
||||
s.mu.Unlock()
|
||||
currentUsage := s.getClientUsage(clientID)
|
||||
if currentUsage <= client.MaxSizeBytes {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// Sort by timestamp (oldest first)
|
||||
sort.Slice(snapshots, func(i, j int) bool {
|
||||
return snapshots[i].Timestamp.Before(snapshots[j].Timestamp)
|
||||
})
|
||||
// Calculate how many bytes we need to free
|
||||
bytesToFree := currentUsage - client.MaxSizeBytes
|
||||
var deletedCount int
|
||||
var reclaimedBytes int64
|
||||
|
||||
currentUsage := int64(0)
|
||||
for _, snap := range snapshots {
|
||||
currentUsage += snap.SizeBytes
|
||||
// Get oldest snapshots and delete until we're under quota
|
||||
snapshots, err := s.db.GetOldestSnapshots(clientID, 100) // Get up to 100 oldest
|
||||
if err != nil {
|
||||
log.Printf("Error getting oldest snapshots: %v", err)
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// Collect snapshots to delete
|
||||
var toDelete []*SnapshotMetadata
|
||||
for currentUsage > client.MaxSizeBytes && len(snapshots) > 1 {
|
||||
oldest := snapshots[0]
|
||||
toDelete = append(toDelete, oldest)
|
||||
currentUsage -= oldest.SizeBytes
|
||||
snapshots = snapshots[1:]
|
||||
for _, snap := range snapshots {
|
||||
if reclaimedBytes >= bytesToFree {
|
||||
break
|
||||
}
|
||||
toDelete = append(toDelete, snap)
|
||||
reclaimedBytes += snap.SizeBytes
|
||||
}
|
||||
|
||||
// Update state before I/O
|
||||
s.snapshots[clientID] = snapshots
|
||||
s.mu.Unlock()
|
||||
|
||||
if len(toDelete) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// Select appropriate backend
|
||||
var backend StorageBackend
|
||||
if client.StorageType == "s3" {
|
||||
if s.s3Backend != nil {
|
||||
backend = s.s3Backend
|
||||
} else {
|
||||
} else if s.localBackend != nil {
|
||||
backend = s.localBackend
|
||||
} else {
|
||||
log.Printf("No storage backend available for rotation")
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// Second pass: delete without holding lock
|
||||
deletedCount := 0
|
||||
reclaimedBytes := int64(0)
|
||||
// Delete snapshots
|
||||
ctx := context.Background()
|
||||
|
||||
for _, snap := range toDelete {
|
||||
if err := backend.Delete(ctx, snap.StorageKey); err != nil {
|
||||
log.Printf("Error deleting snapshot %s: %v", snap.StorageKey, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := s.db.DeleteSnapshot(clientID, snap.SnapshotID); err != nil {
|
||||
log.Printf("Error deleting snapshot record %s: %v", snap.SnapshotID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("Rotated out snapshot: %s (freed %d bytes)", snap.StorageKey, snap.SizeBytes)
|
||||
reclaimedBytes += snap.SizeBytes
|
||||
deletedCount++
|
||||
}
|
||||
|
||||
// Save metadata after deletions
|
||||
s.saveMetadata()
|
||||
|
||||
return deletedCount, reclaimedBytes
|
||||
}
|
||||
|
||||
@@ -283,9 +198,14 @@ func (s *Server) HandleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
client := s.clients[req.ClientID]
|
||||
s.mu.RUnlock()
|
||||
client, err := s.db.GetClient(req.ClientID)
|
||||
if err != nil || client == nil {
|
||||
respondJSON(w, http.StatusInternalServerError, UploadResponse{
|
||||
Success: false,
|
||||
Message: "Failed to get client configuration",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("2006-01-02_15:04:05")
|
||||
|
||||
@@ -379,8 +299,7 @@ func (s *Server) HandleUploadStream(w http.ResponseWriter, r *http.Request) {
|
||||
actualSize = size
|
||||
}
|
||||
|
||||
// Save metadata
|
||||
s.mu.Lock()
|
||||
// Save metadata to database
|
||||
metadata := &SnapshotMetadata{
|
||||
ClientID: clientID,
|
||||
SnapshotID: storageKey,
|
||||
@@ -393,10 +312,10 @@ func (s *Server) HandleUploadStream(w http.ResponseWriter, r *http.Request) {
|
||||
Incremental: incrementalStr == "true",
|
||||
BaseSnapshot: baseSnapshot,
|
||||
}
|
||||
s.snapshots[clientID] = append(s.snapshots[clientID], metadata)
|
||||
s.mu.Unlock()
|
||||
|
||||
s.saveMetadata()
|
||||
if err := s.db.SaveSnapshot(metadata); err != nil {
|
||||
log.Printf("Error saving snapshot metadata: %v", err)
|
||||
}
|
||||
|
||||
respondJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"success": true,
|
||||
@@ -415,10 +334,17 @@ func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
client := s.clients[clientID]
|
||||
snapshots := s.snapshots[clientID]
|
||||
s.mu.RUnlock()
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
respondJSON(w, http.StatusInternalServerError, StatusResponse{Success: false})
|
||||
return
|
||||
}
|
||||
|
||||
snapshots, err := s.db.GetSnapshotsByClient(clientID)
|
||||
if err != nil {
|
||||
log.Printf("Error getting snapshots: %v", err)
|
||||
snapshots = []*SnapshotMetadata{}
|
||||
}
|
||||
|
||||
usedBytes := s.getClientUsage(clientID)
|
||||
percentUsed := float64(usedBytes) / float64(client.MaxSizeBytes) * 100
|
||||
@@ -477,18 +403,14 @@ func (s *Server) HandleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Find snapshot metadata
|
||||
s.mu.RLock()
|
||||
client := s.clients[clientID]
|
||||
var targetSnapshot *SnapshotMetadata
|
||||
for _, snap := range s.snapshots[clientID] {
|
||||
if snap.SnapshotID == snapshotID {
|
||||
targetSnapshot = snap
|
||||
break
|
||||
}
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
http.Error(w, "Client not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
|
||||
if targetSnapshot == nil {
|
||||
targetSnapshot, err := s.db.GetSnapshotByID(clientID, snapshotID)
|
||||
if err != nil || targetSnapshot == nil {
|
||||
http.Error(w, "Snapshot not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -496,10 +418,13 @@ func (s *Server) HandleDownload(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
var backend StorageBackend
|
||||
|
||||
if client.StorageType == "s3" {
|
||||
if client.StorageType == "s3" && s.s3Backend != nil {
|
||||
backend = s.s3Backend
|
||||
} else {
|
||||
} else if s.localBackend != nil {
|
||||
backend = s.localBackend
|
||||
} else {
|
||||
http.Error(w, "No storage backend available", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Download from storage
|
||||
@@ -543,11 +468,8 @@ func (s *Server) HandleRotationPolicy(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
client, exists := s.clients[clientID]
|
||||
s.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
client, err := s.db.GetClient(clientID)
|
||||
if err != nil || client == nil {
|
||||
respondJSON(w, http.StatusNotFound, RotationPolicyResponse{
|
||||
Success: false,
|
||||
Message: "Client not found",
|
||||
@@ -577,6 +499,7 @@ func (s *Server) HandleRotationPolicy(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// RegisterRoutes registers all HTTP routes
|
||||
func (s *Server) RegisterRoutes(mux *http.ServeMux) {
|
||||
// Client API routes
|
||||
mux.HandleFunc("/upload", s.HandleUpload)
|
||||
mux.HandleFunc("/upload-stream/", s.HandleUploadStream)
|
||||
mux.HandleFunc("/status", s.HandleStatus)
|
||||
@@ -584,6 +507,25 @@ func (s *Server) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/download", s.HandleDownload)
|
||||
mux.HandleFunc("/health", s.HandleHealth)
|
||||
mux.HandleFunc("/rotation-policy", s.HandleRotationPolicy)
|
||||
|
||||
// Admin API routes
|
||||
mux.HandleFunc("/admin/login", s.handleAdminLogin)
|
||||
mux.HandleFunc("/admin/logout", s.handleAdminLogout)
|
||||
mux.HandleFunc("/admin/check", s.handleAdminCheck)
|
||||
mux.HandleFunc("/admin/clients", s.handleAdminGetClients)
|
||||
mux.HandleFunc("/admin/client", s.handleAdminGetClient)
|
||||
mux.HandleFunc("/admin/client/create", s.handleAdminCreateClient)
|
||||
mux.HandleFunc("/admin/client/update", s.handleAdminUpdateClient)
|
||||
mux.HandleFunc("/admin/client/delete", s.handleAdminDeleteClient)
|
||||
mux.HandleFunc("/admin/snapshots", s.handleAdminGetSnapshots)
|
||||
mux.HandleFunc("/admin/snapshot/delete", s.handleAdminDeleteSnapshot)
|
||||
mux.HandleFunc("/admin/stats", s.handleAdminGetStats)
|
||||
mux.HandleFunc("/admin/admins", s.handleAdminGetAdmins)
|
||||
mux.HandleFunc("/admin/admin/create", s.handleAdminCreateAdmin)
|
||||
mux.HandleFunc("/admin/admin/delete", s.handleAdminDeleteAdmin)
|
||||
|
||||
// Admin UI (static files served from /admin/)
|
||||
mux.HandleFunc("/admin/", s.handleAdminUI)
|
||||
}
|
||||
|
||||
func respondJSON(w http.ResponseWriter, status int, data interface{}) {
|
||||
|
||||
Reference in New Issue
Block a user