-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
87 lines (71 loc) · 2.01 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
84
85
86
87
package main
import (
"embed"
"errors"
"fmt"
"os"
"runtime/pprof"
"time"
"github.com/spf13/viper"
"github.com/tphakala/birdnet-go/cmd"
"github.com/tphakala/birdnet-go/internal/analysis"
"github.com/tphakala/birdnet-go/internal/conf"
"github.com/tphakala/birdnet-go/internal/httpcontroller"
)
// buildTime is the time when the binary was built.
var buildDate string
// version holds the Git version tag
var version string
//go:embed assets/*
var assetsFs embed.FS
//go:embed views/*
var viewsFs embed.FS
func main() {
exitCode := mainWithExitCode()
os.Exit(exitCode)
}
func mainWithExitCode() int {
// Check if profiling is enabled
if os.Getenv("BIRDNET_GO_PROFILE") == "1" {
fmt.Println("Profiling enabled")
// Create a unique profile name with timestamp
now := time.Now()
profilePath := fmt.Sprintf("profile_%s.pprof", now.Format("20060102_150405"))
f, err := os.Create(profilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating profile file: %v\n", err)
return 1
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Error starting CPU profile: %v\n", err)
return 1
}
defer pprof.StopCPUProfile()
}
// publish the embedded assets and views directories to controller package
httpcontroller.AssetsFs = assetsFs
httpcontroller.ViewsFs = viewsFs
// Load the configuration
settings := conf.Setting()
if settings == nil {
fmt.Fprintf(os.Stderr, "Error loading configuration\n")
return 1
}
// Set runtime values
settings.Version = version
settings.BuildDate = buildDate
fmt.Printf("🐦 \033[37mBirdNET-Go v%s (built: %s), using config file: %s\033[0m\n",
settings.Version, settings.BuildDate, viper.ConfigFileUsed())
// Execute the root command
rootCmd := cmd.RootCommand(settings)
if err := rootCmd.Execute(); err != nil {
if errors.Is(err, analysis.ErrAnalysisCanceled) {
// Clean exit for user-initiated cancellation
return 0
}
fmt.Fprintf(os.Stderr, "Command execution error: %v\n", err)
return 1
}
return 0
}