-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (99 loc) · 3.13 KB
/
main.go
File metadata and controls
116 lines (99 loc) · 3.13 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"embed"
"flag"
"fmt"
"os"
"videoarchiver/backend/domains/lockfile"
"videoarchiver/backend/domains/logging"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
)
//go:embed all:frontend/dist
var assets embed.FS
func main() {
mode := flag.String("mode", "", "Startup mode: ui, daemon (defaults to ui)")
flag.Parse()
// Early logging to track startup mode
fmt.Printf("Starting application in mode: %s\n", *mode)
switch *mode {
case "daemon":
fmt.Println("Initializing daemon mode...")
app := NewApp(false, "daemon")
runDaemon(app)
case "ui", "":
fmt.Println("Initializing UI mode...")
app := NewApp(true, "ui")
runUI(app)
default:
fmt.Println("LOG: Application exiting due to invalid startup mode")
println("Invalid startup mode. Valid modes: ui, daemon")
os.Exit(1)
}
}
func runDaemon(app *App) {
// Create early logger for daemon startup messages
earlyLogger := logging.NewLogService("daemon")
defer earlyLogger.Close()
// Handle daemon locking before initialization - but only check, don't create yet
earlyLogger.Info("Checking for existing daemon instances...")
// Check if lock already exists
locked, err := lockfile.IsLocked()
if err != nil {
earlyLogger.Error(fmt.Sprintf("Failed to check lock status: %v", err))
earlyLogger.Error("LOG: Daemon exiting due to lock status check failure")
os.Exit(1)
}
if locked {
// Another daemon is starting up or running, exit this instance
earlyLogger.Info("Another daemon instance is starting up. Exiting...")
earlyLogger.Info("LOG: Daemon exiting due to existing daemon lock")
os.Exit(0)
}
// Clean up old log entries before starting daemon
earlyLogger.Info("Cleaning up log entries older than 30 days...")
if err := earlyLogger.ClearLogsOlderThanDays("daemon.log", 30); err != nil {
earlyLogger.Warn(fmt.Sprintf("Failed to clean up old daemon logs: %v", err))
}
if err := earlyLogger.ClearLogsOlderThanDays("ui.log", 30); err != nil {
earlyLogger.Warn(fmt.Sprintf("Failed to clean up old UI logs: %v", err))
}
earlyLogger.Info("Log cleanup completed")
earlyLogger.Info(fmt.Sprintf("Application version: %s", GetVersionInfo()))
earlyLogger.Info("Initializing application")
app.startup(context.Background())
app.LogService.Info("Daemon starting")
startDaemonLoop(app)
}
func runUI(app *App) {
fmt.Println("Starting Wails UI...")
err := wails.Run(&options.App{
Title: "Video Archiver",
Width: 1024,
Height: 900,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup,
OnBeforeClose: func(ctx context.Context) (prevent bool) {
// Use the CloseConfirmService if available
if app.CloseConfirmService != nil {
// Return true to prevent close if user cancels
return !app.CloseConfirmService.ShouldConfirmClose()
}
// Fallback: allow close
return false
},
Bind: []interface{}{
app,
},
})
if err != nil {
fmt.Printf("LOG: UI exiting due to Wails error: %v\n", err)
println("Error:", err.Error())
os.Exit(1)
}
}