-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
83 lines (74 loc) · 1.55 KB
/
main.go
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
package main
import (
"context"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"time"
"github.com/dancnb/sonicradio/browser"
"github.com/dancnb/sonicradio/config"
"github.com/dancnb/sonicradio/player"
"github.com/dancnb/sonicradio/ui"
)
func main() {
run()
}
func run() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg, err := config.Load()
if err != nil {
fmt.Println(err.Error())
}
if cfg.IsRunning {
panic("application is already running")
}
go func() {
cfg.IsRunning = true
err = cfg.Save()
if err != nil {
fmt.Printf("error saving config: %v\n", err)
}
}()
var logW io.Writer
if cfg.Debug {
logFile := fmt.Sprintf("sonicradio-%d.log", time.Now().UnixMilli())
lp := filepath.Join(cfg.LogPath, logFile)
f, err := os.Create(lp)
if err != nil {
panic("could not create log file " + lp)
}
defer f.Close()
logW = f
} else {
logW = io.Discard
}
opts := &slog.HandlerOptions{
Level: slog.LevelDebug,
}
handler := slog.NewTextHandler(logW, opts)
logger := slog.New(handler)
log.SetFlags(log.Flags() &^ (log.Ldate))
slog.SetDefault(logger)
slog.Info("----------------------Starting----------------------")
slog.Debug("loaded", "config", cfg.String())
b, err := browser.NewApi(ctx, cfg)
if err != nil {
panic(err)
}
p, err := player.NewPlayer(ctx, cfg)
if err != nil {
panic(err)
}
m := ui.NewModel(ctx, cfg, b, p)
defer func() {
m.Quit()
}()
if _, err := m.Progr.Run(); err != nil {
slog.Info(fmt.Sprintf("Error running program: %s", err.Error()))
os.Exit(1)
}
}