mirror of
https://github.com/Xevion/HATray.git
synced 2025-12-05 23:15:09 -06:00
refactor: remove confusing double app.go internal files, move setup details to main.go
This commit is contained in:
62
cmd/main.go
62
cmd/main.go
@@ -2,24 +2,68 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"ha-tray/internal"
|
"ha-tray/internal/app"
|
||||||
|
"ha-tray/internal/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create new application instance
|
// Setup logging
|
||||||
app := internal.NewApp()
|
logger, logFile, err := setupLogging()
|
||||||
|
if err != nil {
|
||||||
// Setup the application (logging, panic handling, service initialization)
|
log.Fatalf("Failed to setup logging: %v", err)
|
||||||
if err := app.Setup(); err != nil {
|
|
||||||
log.Fatalf("Failed to setup application: %v", err)
|
|
||||||
}
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
|
||||||
// Run the application
|
// Setup panic recovery
|
||||||
if err := app.Run(); err != nil {
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
logger.Error("Panic recovered", "panic", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Create app layer
|
||||||
|
appLayer := app.NewApp(logger)
|
||||||
|
|
||||||
|
// Create and run service
|
||||||
|
svc := service.NewService(logger, appLayer)
|
||||||
|
|
||||||
|
logger.Info("Starting HATray application")
|
||||||
|
|
||||||
|
if err := svc.Run(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Application error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Application error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupLogging initializes structured logging
|
||||||
|
func setupLogging() (*slog.Logger, *os.File, error) {
|
||||||
|
// Get the directory where the executable is located
|
||||||
|
exePath, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to get executable path: %v", err)
|
||||||
|
}
|
||||||
|
exeDir := filepath.Dir(exePath)
|
||||||
|
|
||||||
|
// Open log file in the same directory as the executable
|
||||||
|
logFile, err := os.OpenFile(filepath.Join(exeDir, "current.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to open log file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create multi-writer to log to both file and stdout
|
||||||
|
multiWriter := io.MultiWriter(logFile, os.Stdout)
|
||||||
|
|
||||||
|
// Create JSON handler for structured logging
|
||||||
|
handler := slog.NewJSONHandler(multiWriter, &slog.HandlerOptions{
|
||||||
|
Level: slog.LevelDebug,
|
||||||
|
})
|
||||||
|
logger := slog.New(handler)
|
||||||
|
|
||||||
|
return logger, logFile, nil
|
||||||
|
}
|
||||||
|
|||||||
110
internal/app.go
110
internal/app.go
@@ -1,110 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"ha-tray/internal/app"
|
|
||||||
"ha-tray/internal/service"
|
|
||||||
)
|
|
||||||
|
|
||||||
// App represents the main application
|
|
||||||
type App struct {
|
|
||||||
appLayer *app.App
|
|
||||||
service service.Service
|
|
||||||
logger *slog.Logger
|
|
||||||
logFile *os.File
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewApp creates a new application instance
|
|
||||||
func NewApp() *App {
|
|
||||||
return &App{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup initializes the application with logging and panic handling
|
|
||||||
func (a *App) Setup() error {
|
|
||||||
// Setup panic recovery
|
|
||||||
defer a.recoverPanic()
|
|
||||||
|
|
||||||
// Setup logging
|
|
||||||
if err := a.setupLogging(); err != nil {
|
|
||||||
return fmt.Errorf("failed to setup logging: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup app layer
|
|
||||||
a.appLayer = app.NewApp(a.logger)
|
|
||||||
|
|
||||||
// Setup service
|
|
||||||
if err := a.setupService(); err != nil {
|
|
||||||
return fmt.Errorf("failed to setup service: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run starts the application
|
|
||||||
func (a *App) Run() error {
|
|
||||||
defer a.cleanup()
|
|
||||||
|
|
||||||
a.logger.Info("Starting HATray application")
|
|
||||||
|
|
||||||
// Run the service
|
|
||||||
return a.service.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
// setupLogging initializes structured logging
|
|
||||||
func (a *App) setupLogging() error {
|
|
||||||
// Get the directory where the executable is located
|
|
||||||
exePath, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get executable path: %v", err)
|
|
||||||
}
|
|
||||||
exeDir := filepath.Dir(exePath)
|
|
||||||
|
|
||||||
// Open log file in the same directory as the executable
|
|
||||||
logFile, err := os.OpenFile(filepath.Join(exeDir, "current.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to open log file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
a.logFile = logFile
|
|
||||||
|
|
||||||
// Create multi-writer to log to both file and stdout
|
|
||||||
multiWriter := io.MultiWriter(logFile, os.Stdout)
|
|
||||||
|
|
||||||
// Create JSON handler for structured logging
|
|
||||||
handler := slog.NewJSONHandler(multiWriter, &slog.HandlerOptions{
|
|
||||||
Level: slog.LevelDebug,
|
|
||||||
})
|
|
||||||
a.logger = slog.New(handler)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// setupService initializes the platform-specific service
|
|
||||||
func (a *App) setupService() error {
|
|
||||||
// Platform-specific service initialization using build flags
|
|
||||||
a.service = service.NewService(a.logger, a.appLayer)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// recoverPanic handles panic recovery and logs the error
|
|
||||||
func (a *App) recoverPanic() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
if a.logger != nil {
|
|
||||||
a.logger.Error("Panic recovered", "panic", r)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Panic recovered: %v\n", r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleanup performs cleanup operations
|
|
||||||
func (a *App) cleanup() {
|
|
||||||
if a.logFile != nil {
|
|
||||||
a.logFile.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user