-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (84 loc) · 2.86 KB
/
Copy pathmain.go
File metadata and controls
96 lines (84 loc) · 2.86 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
package main
import (
"embed"
"log"
"time"
"github.com/AdemarTellecher/ipmonitorapp/internal/model"
"github.com/AdemarTellecher/ipmonitorapp/internal/service"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
//go:embed all:frontend
var assets embed.FS
//go:embed internal/assets/icons/app-icon.png
var appIcon []byte
func main() {
// 1. Resolve dinamicamente o caminho do banco SQLite na pasta do executável
dbPath := model.ResolveDBPath("ipMonitorDB.db")
repo, err := model.NewRepository(dbPath)
if err != nil {
log.Fatalf("Erro ao inicializar o banco SQLite portátil: %v", err)
}
defer repo.Close()
// 2. Cria o serviço de monitoramento exposto para o frontend
monitorService := service.NewMonitorService(repo)
// 3. Inicializa o aplicativo Wails v3 com o ícone oficial
app := application.New(application.Options{
Name: "IP Monitor App",
Description: "Monitoramento de conectividade ICMP Ping portátil e ultra-leve",
Icon: appIcon,
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: false,
},
Services: []application.Service{
application.NewServiceWithOptions(monitorService, application.ServiceOptions{
Route: "/api",
}),
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
})
// 4. Cria a janela nativa elegante
win := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "IP Monitor",
Width: 540,
Height: 740,
MinWidth: 480,
MinHeight: 640,
InitialPosition: application.WindowCentered,
BackgroundColour: application.NewRGB(16, 19, 26),
URL: "/",
})
// No macOS, ao fechar a janela (botão X vermelho), oculta a janela em vez de destruí-la,
// permitindo reabri-la instantaneamente ao clicar no ícone na Dock.
win.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
win.Hide()
e.Cancel()
})
// Reabre e foca a janela ao clicar no ícone na Dock do macOS
app.Event.OnApplicationEvent(events.Mac.ApplicationShouldHandleReopen, func(event *application.ApplicationEvent) {
win.Show()
win.Focus()
})
// 5. Rotina periódica de ping a cada 1 minuto com emissão de evento
go func() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
log.Println("[IP Monitor] Iniciando varredura periódica automática de 1 minuto...")
updated, err := monitorService.UpdateAllStatuses()
if err != nil {
log.Printf("[IP Monitor] Erro na varredura periódica: %v\n", err)
continue
}
log.Printf("[IP Monitor] Varredura periódica concluída com sucesso (%d hosts cadastrados). Emitindo evento ips-updated...\n", updated.Total)
app.Event.Emit("ips-updated", updated)
}
}()
// 6. Executa a aplicação
err = app.Run()
if err != nil {
log.Fatal(err)
}
}