-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
348 lines (306 loc) · 12 KB
/
Copy pathmain.go
File metadata and controls
348 lines (306 loc) · 12 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"context"
"embed"
"fmt"
"os"
logging "github.com/omniviewdev/plugin-sdk/log"
pkgsettings "github.com/omniviewdev/plugin-sdk/settings"
"github.com/wailsapp/wails/v3/pkg/application"
"go.uber.org/zap"
"github.com/omniviewdev/omniview/backend/diagnostics"
"github.com/omniviewdev/omniview/backend/menus"
"github.com/omniviewdev/omniview/backend/pkg/plugin"
"github.com/omniviewdev/omniview/backend/pkg/plugin/data"
"github.com/omniviewdev/omniview/backend/pkg/plugin/devserver"
"github.com/omniviewdev/omniview/backend/pkg/plugin/exec"
pluginlogs "github.com/omniviewdev/omniview/backend/pkg/plugin/logs"
pluginmetric "github.com/omniviewdev/omniview/backend/pkg/plugin/metric"
"github.com/omniviewdev/omniview/backend/pkg/plugin/networker"
"github.com/omniviewdev/omniview/backend/pkg/plugin/pluginlog"
"github.com/omniviewdev/omniview/backend/pkg/plugin/registry"
"github.com/omniviewdev/omniview/backend/pkg/plugin/resource"
"github.com/omniviewdev/omniview/backend/pkg/plugin/settings"
"github.com/omniviewdev/omniview/backend/pkg/plugin/types"
"github.com/omniviewdev/omniview/backend/pkg/plugin/ui"
"github.com/omniviewdev/omniview/backend/pkg/plugin/utils"
"github.com/omniviewdev/omniview/backend/window"
"github.com/omniviewdev/omniview/internal/appstate"
"github.com/omniviewdev/omniview/internal/bootstrap"
coresettings "github.com/omniviewdev/omniview/internal/settings"
settingsstore "github.com/omniviewdev/omniview/internal/settings/store"
"github.com/omniviewdev/omniview/internal/telemetry"
"github.com/omniviewdev/omniview/internal/version"
)
const (
DefaultWebviewWidth = 1920
DefaultWebviewHeight = 1080
MinWebviewWidth = 1280
MinWebviewHeight = 800
)
//go:embed all:dist
var assets embed.FS
//go:embed build/appicon.png
var icon []byte
//nolint:funlen // main function is expected to be long
func main() {
// Initialize unified state directory.
stateDir, err := appstate.New()
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: failed to initialize state directory: %v\n", err)
os.Exit(1)
}
defer stateDir.Close()
// Open bbolt settings store.
logDir := stateDir.Logs().ResolvePath("")
settingsStore, err := settingsstore.Open(stateDir.RootDir().ResolvePath("settings.db"))
if err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to open settings store: %v\n", err)
}
if settingsStore != nil {
defer settingsStore.Close()
if migrateErr := settingsstore.MigrateFromGOB(stateDir.Root(), settingsStore); migrateErr != nil {
fmt.Fprintf(os.Stderr, "warning: settings migration had errors: %v\n", migrateErr)
}
}
// Seed telemetry config from persisted bbolt settings so that telemetry
// starts with the user's saved preferences rather than defaults.
telemetryCfg := telemetry.DefaultConfig(version.IsDevelopment())
if settingsStore != nil {
if telVals, loadErr := settingsStore.LoadCategory("telemetry"); loadErr == nil && len(telVals) > 0 {
if v, ok := telVals["enabled"].(bool); ok {
telemetryCfg.Enabled = v
}
if v, ok := telVals["traces"].(bool); ok {
telemetryCfg.Traces = v
}
if v, ok := telVals["metrics"].(bool); ok {
telemetryCfg.Metrics = v
}
if v, ok := telVals["logs_ship"].(bool); ok {
telemetryCfg.LogsShip = v
}
if v, ok := telVals["logs_ship_level"].(string); ok {
telemetryCfg.LogsShipLevel = v
}
if v, ok := telVals["profiling"].(bool); ok {
telemetryCfg.Profiling = v
}
if v, ok := telVals["endpoint_otlp"].(string); ok {
telemetryCfg.OTLPEndpoint = v
}
if v, ok := telVals["endpoint_pyroscope"].(string); ok {
telemetryCfg.PyroscopeEndpoint = v
}
if v, ok := telVals["auth_header"].(string); ok {
telemetryCfg.AuthHeader = v
}
if v, ok := telVals["auth_value"].(string); ok {
telemetryCfg.AuthValue = v
}
}
}
// Bootstrap telemetry (tracing, metrics, log shipping, profiling).
telemetrySvc := telemetry.New(
telemetryCfg,
version.Version,
version.GitCommit,
version.BuildDate,
version.IsDevelopment(),
logDir,
)
if err := telemetrySvc.Init(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "telemetry init failed, continuing without telemetry: %v\n", err)
}
zapLogger := telemetrySvc.ZapLogger()
if zapLogger == nil {
zapLogger, _ = zap.NewProduction()
}
log := logging.New(logging.Config{
Name: "omniview",
Backend: telemetry.NewZapBackend(zapLogger),
Level: logging.NewLevelController(logging.LevelDebug),
})
if settingsStore == nil {
log.Warnw(context.Background(), "settings persistence disabled; all settings will use defaults")
}
diagnosticsClient := diagnostics.NewDiagnosticsClient(version.IsDevelopment(), logDir)
settingsProvider := pkgsettings.NewProvider(pkgsettings.ProviderOpts{
Logger: zapLogger.Sugar(),
})
// Create our plugin system managers
uiManager := ui.NewComponentManager(log)
managers := map[string]types.PluginManager{
"ui": uiManager,
}
utilsClient := utils.NewClient()
// Setup the plugin systems
resourceController := resource.NewController(log, settingsProvider, stateDir.PluginStore)
graphService := resource.NewGraphService(resourceController.Graph())
settingsController := settings.NewController(log, settingsProvider, settingsStore)
execController := exec.NewController(log, settingsProvider, resourceController)
networkerController := networker.NewController(log, settingsProvider, resourceController)
logsController := pluginlogs.NewController(log, settingsProvider, resourceController)
metricController := pluginmetric.NewController(log, settingsProvider, resourceController)
dataController := data.NewController(log, stateDir.PluginData)
// Initialize per-plugin log manager for capturing plugin process stderr.
// Created here so it can be bound to Wails for UI access.
pluginLogManager, pluginLogErr := pluginlog.NewManager(
logDir,
pluginlog.DefaultRotation(),
)
if pluginLogErr != nil {
log.Warnw(context.Background(), "failed to initialize plugin log manager", "error", pluginLogErr)
}
pluginRegistryClient := registry.NewClient("")
pluginManager := plugin.NewManager(
log,
stateDir.RootDir(),
stateDir.Plugins(),
resourceController,
settingsController,
execController,
networkerController,
logsController,
metricController,
managers,
settingsProvider,
pluginRegistryClient,
func() plugin.TelemetryEnvConfig {
cfg := telemetrySvc.Config()
return plugin.TelemetryEnvConfig{
Enabled: cfg.Enabled,
OTLPEndpoint: cfg.OTLPEndpoint,
Profiling: cfg.Profiling,
PyroscopeEndpoint: cfg.PyroscopeEndpoint,
}
},
)
// Wire crash recovery: when a plugin crash is detected by the resource
// controller's event listeners, the manager will attempt to reload it.
resourceController.SetCrashCallback(pluginManager.HandlePluginCrash)
devServerManager := devserver.NewDevServerManager(
log,
stateDir.RootDir(),
stateDir.Plugins(),
&plugin.PluginRefAdapter{Mgr: pluginManager},
&plugin.PluginReloaderAdapter{Mgr: pluginManager},
settingsProvider,
)
// Wire the dev server checker into the plugin manager so the old watcher
// skips plugins managed by DevServerManager.
pluginManager.SetDevServerChecker(devServerManager)
pluginManager.SetDevServerManager(devServerManager)
if pluginLogManager != nil {
pluginManager.SetPluginLogManager(pluginLogManager)
}
// Create the AppService
appService := NewAppService()
// Create the bootstrap service that wraps startup/shutdown logic
bootstrapSvc := &bootstrap.Service{
Log: log,
SettingsProvider: settingsProvider,
SettingsStore: settingsStore,
TelemetrySvc: telemetrySvc,
PluginManager: pluginManager,
PluginRegistryClient: pluginRegistryClient,
}
// Set up plugin asset handler middleware
pluginAssetHandler := NewPluginAssetHandler(log, stateDir.RootDir())
// Wrap the plugin manager interface in a concrete struct for v3 service
// registration (NewService requires a concrete pointer type).
pluginManagerSvc := &plugin.ServiceWrapper{Mgr: pluginManager}
// Build the service list. All concrete pointer types use NewService directly.
// BootstrapService is registered first so startup logic runs before other
// services that might depend on initialized controllers. It has no
// frontend-facing methods — only ServiceStartup/ServiceShutdown.
// Service registration order matters: Wails calls ServiceStartup in order.
// Controllers must be initialized (receive ctx + app) BEFORE the bootstrap
// service, because bootstrap calls pluginManager.Initialize() which triggers
// OnPluginStart on all controllers — they need ctx for gRPC streams.
services := []application.Service{
// 1. Controllers — need ctx before plugin loading
application.NewService(&resource.ServiceWrapper{Ctrl: resourceController}),
application.NewService(graphService),
application.NewService(&settings.ServiceWrapper{Ctrl: settingsController}),
application.NewService(&exec.ServiceWrapper{Ctrl: execController}),
application.NewService(&networker.ServiceWrapper{Ctrl: networkerController}),
application.NewService(&pluginlogs.ServiceWrapper{Ctrl: logsController}),
application.NewService(&pluginmetric.ServiceWrapper{Ctrl: metricController}),
application.NewService(&data.ServiceWrapper{Ctrl: dataController}),
application.NewService(ui.NewServiceWrapper(uiManager)),
application.NewService(utilsClient),
application.NewService(&devserver.ServiceWrapper{Mgr: devServerManager}),
// 2. Bootstrap — initializes settings, telemetry, loads plugins
application.NewService(bootstrapSvc),
// 3. Frontend-facing services (no startup order dependency)
application.NewService(appService),
application.NewService(diagnosticsClient),
application.NewService(telemetry.NewTelemetryBinding(telemetrySvc)),
application.NewService(&coresettings.ServiceWrapper{
Provider: settingsProvider,
CategoryMeta: map[string]pkgsettings.Category{
coresettings.General.ID: coresettings.General,
coresettings.Appearance.ID: coresettings.Appearance,
coresettings.Terminal.ID: coresettings.Terminal,
coresettings.Editor.ID: coresettings.Editor,
coresettings.Developer.ID: coresettings.Developer,
coresettings.Telemetry.ID: coresettings.Telemetry,
},
}),
application.NewService(pluginManagerSvc),
}
if pluginLogManager != nil {
services = append(services, application.NewService(&pluginlog.ServiceWrapper{Mgr: pluginLogManager}))
}
// Create the Wails v3 application
app := application.New(application.Options{
Name: "Omniview",
Description: fmt.Sprintf("Omniview %s", version.Version),
Icon: icon,
Services: services,
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
Middleware: pluginAssetHandler.Middleware,
},
// Server mode options — used when built with -tags server for
// headless/CI testing (e.g. Playwright E2E).
Server: application.ServerOptions{
Host: "localhost",
Port: 34115,
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
Linux: application.LinuxOptions{
ProgramName: "Omniview",
},
})
// Create the main window
mainWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
Name: "main",
Title: "Omniview",
Width: DefaultWebviewWidth,
Height: DefaultWebviewHeight,
MinWidth: MinWebviewWidth,
MinHeight: MinWebviewHeight,
//nolint:gomnd // #0D1117 dark theme background
BackgroundColour: application.NewRGBA(13, 17, 23, 255),
UseApplicationMenu: true,
Mac: application.MacWindow{
TitleBar: application.MacTitleBarHiddenInset,
Appearance: application.NSAppearanceNameDarkAqua,
Backdrop: application.MacBackdropTranslucent,
},
})
// Set up menus, keybindings, and context menus
menus.SetupAppMenu(app, mainWindow)
menus.SetupKeyBindings(app)
menus.SetupContextMenus(app)
// Set up window manager (registers hide-on-close hook, etc.)
_ = window.NewManager(app, mainWindow)
// Run the application
if err := app.Run(); err != nil {
log.Fatalw(context.Background(), "wails app exited with error", "error", err)
}
}