-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (92 loc) · 2.55 KB
/
Copy pathmain.go
File metadata and controls
112 lines (92 loc) · 2.55 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/b0bbywan/go-odio-api/api"
"github.com/b0bbywan/go-odio-api/backend"
"github.com/b0bbywan/go-odio-api/config"
"github.com/b0bbywan/go-odio-api/logger"
)
func main() {
if os.Getuid() == 0 {
logger.Fatal("[%s] root user is strictly forbidden! Odio cannot and will not run as root.", config.AppName)
}
flag.Usage = usage
configFile := flag.String("config", "", "path to configuration file")
versionFlag := flag.Bool("version", false, "Print version")
flag.Parse()
if *versionFlag {
fmt.Printf("%s %s\n", config.AppName, config.AppVersion)
return
}
cfg, err := config.New(configFile)
if err != nil {
logger.Fatal("[%s] Failed to load config: %v", config.AppName, err)
}
// Set log level from config
logger.SetLevel(cfg.LogLevel)
// Global context for the entire application
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Initialize backends
b, err := backend.New(
ctx,
cfg.Bluetooth,
cfg.Login1,
cfg.MPRIS,
cfg.Pulseaudio,
cfg.Systemd,
cfg.Upgrade,
cfg.Zeroconf,
)
if err != nil {
logger.Fatal("[%s] Backend initialization failed: %v", config.AppName, err)
}
// Start enabled backend
if err := b.Start(); err != nil {
logger.Fatal("[%s] Backend start failed: %v", config.AppName, err)
}
// New api server
server := api.NewServer(cfg.Api, b)
// Channel to synchronize shutdown
shutdownDone := make(chan struct{})
// Goroutine for signal handling
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
logger.Info("[%s] Shutdown signal received, stopping server...", config.AppName)
clear(b, cancel, shutdownDone)
}()
logger.Info("[%s] started", config.AppName)
if server != nil {
if err := server.Run(ctx); err != nil && err != http.ErrServerClosed {
logger.Error("[%s] http server error: %v", config.AppName, err)
clear(b, cancel, shutdownDone)
}
}
<-shutdownDone
logger.Info("[%s] stopped", config.AppName)
}
func clear(b *backend.Backend, cancel context.CancelFunc, shutdown chan struct{}) {
// Cancel the global context - stops all listeners
cancel()
// Cleanup backends
b.Close()
// Signal that cleanup is complete
close(shutdown)
}
func usage() {
fmt.Println("Usage:")
fmt.Println(" odio-api [options]")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" --config <path> configuration file to use")
fmt.Println(" --version Display version")
fmt.Println(" -h, --help this help message")
}