Problem: the Oikos control room was browser-only — no native desktop
experience (system tray, notifications, keychain-persisted auth).
Change: add a Wails v3 thin-shell desktop app at cmd/desktop/ that embeds
the existing SPA in a webview. The Go side is ~380 lines — no bundled
server, no Postgres connection. It reads auth from the OS keychain,
injects it into the SPA on load, and the SPA talks HTTPS to the homelab
same as a browser.
Phase 1.0 — Scaffold + window:
- Embed web/dist/ into the Wails binary
- Inject window.__OIKOS_CONFIG__ with keychain-stored apiUrl + token
- 1400×900 window, min 1024×700
- System tray: Open/Quit, click toggles window
Phase 1.1 — Native shell:
- Poll /api/v1/dashboard/summary every 30s; osascript notification
when approvals or critical signals increase
- Save/restore window position to ~/.config/oikos/window.json
- EnableAutoStart/DisableAutoStart — macOS LaunchAgent plist
Phase 1.2 — Token management:
- Config.svelte calls window.wails.Call.ByName('SaveConfig') after
successful connection — persists to OS keychain
- ConfigService binds SaveConfig, ClearConfig, EnableAutoStart,
DisableAutoStart to the Wails runtime
Phase 1.3 — Auto-update:
- Poll Gitea releases API every 6h, compare semver, show dialog
- 'Check for Updates' tray menu item triggers immediate poll
Phase 1.4 — Distribution:
- macOS entitlements.plist: network client + keychain access
- .gitea/workflows/desktop.yml: CI builds macOS arm64 + Linux amd64
on 'desktop-*' / 'v*' tags, attaches artifacts to release
- Makefile: desktop (build), desktop-package (build + zip/tar.gz)
- CONTRIBUTING.md: documented desktop app + commands
Risk: low. Wails v3 alpha API may shift; the Go glue is ~380 lines and
trivially portable. The desktop app is additive — zero changes to the
existing server or SPA logic. No config mutation, no infrastructure
impact.
Verification: go build, go vet, go mod tidy all pass.
383 lines
8.7 KiB
Go
383 lines
8.7 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
"github.com/zalando/go-keyring"
|
|
)
|
|
|
|
//go:embed frontend/dist
|
|
var assets embed.FS
|
|
|
|
const (
|
|
keyringService = "com.hubris.oikos-desktop"
|
|
keyringUser = "oikos"
|
|
version = "0.1.0"
|
|
updateURL = "https://git.hubris.network/api/v1/repos/dtoro/oikos/releases"
|
|
pollInterval = 30 * time.Second
|
|
updateInterval = 6 * time.Hour
|
|
)
|
|
|
|
type OikosConfig struct {
|
|
ApiUrl string `json:"apiUrl"`
|
|
Token string `json:"token,omitempty"`
|
|
IsDesktop bool `json:"isDesktop"`
|
|
}
|
|
|
|
// ---- ConfigService ----
|
|
|
|
type ConfigService struct{ app *application.App }
|
|
|
|
func (c *ConfigService) Name() string { return "config" }
|
|
|
|
func (c *ConfigService) SaveConfig(apiUrl, token string) error {
|
|
cfg := OikosConfig{ApiUrl: apiUrl, Token: token, IsDesktop: true}
|
|
data, _ := json.Marshal(cfg)
|
|
return keyring.Set(keyringService, keyringUser, string(data))
|
|
}
|
|
|
|
func (c *ConfigService) ClearConfig() error {
|
|
return keyring.Delete(keyringService, keyringUser)
|
|
}
|
|
|
|
func (c *ConfigService) EnableAutoStart() error {
|
|
if runtime.GOOS != "darwin" {
|
|
return fmt.Errorf("autostart not supported on %s", runtime.GOOS)
|
|
}
|
|
usr, _ := user.Current()
|
|
dir := filepath.Join(usr.HomeDir, "Library", "LaunchAgents")
|
|
os.MkdirAll(dir, 0755)
|
|
|
|
exe, _ := os.Executable()
|
|
plist := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>Label</key>
|
|
<string>com.hubris.oikos-desktop</string>
|
|
<key>ProgramArguments</key>
|
|
<array>
|
|
<string>%s</string>
|
|
</array>
|
|
<key>RunAtLoad</key>
|
|
<true/>
|
|
<key>KeepAlive</key>
|
|
<false/>
|
|
</dict>
|
|
</plist>`, exe)
|
|
|
|
return os.WriteFile(filepath.Join(dir, "com.hubris.oikos-desktop.plist"), []byte(plist), 0644)
|
|
}
|
|
|
|
func (c *ConfigService) DisableAutoStart() error {
|
|
if runtime.GOOS != "darwin" {
|
|
return fmt.Errorf("autostart not supported on %s", runtime.GOOS)
|
|
}
|
|
usr, _ := user.Current()
|
|
path := filepath.Join(usr.HomeDir, "Library", "LaunchAgents", "com.hubris.oikos-desktop.plist")
|
|
return os.Remove(path)
|
|
}
|
|
|
|
// ---- Window persistence ----
|
|
|
|
type windowState struct {
|
|
X int `json:"x"`
|
|
Y int `json:"y"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
}
|
|
|
|
func windowStatePath() string {
|
|
usr, _ := user.Current()
|
|
return filepath.Join(usr.HomeDir, ".config", "oikos", "window.json")
|
|
}
|
|
|
|
func loadWindowState() *windowState {
|
|
data, err := os.ReadFile(windowStatePath())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var ws windowState
|
|
if err := json.Unmarshal(data, &ws); err != nil {
|
|
return nil
|
|
}
|
|
if ws.Width < 200 || ws.Height < 200 {
|
|
return nil
|
|
}
|
|
return &ws
|
|
}
|
|
|
|
func saveWindowState(w application.Window) {
|
|
x, y := w.Position()
|
|
width, height := w.Size()
|
|
ws := windowState{X: x, Y: y, Width: width, Height: height}
|
|
data, _ := json.Marshal(ws)
|
|
|
|
usr, _ := user.Current()
|
|
dir := filepath.Join(usr.HomeDir, ".config", "oikos")
|
|
os.MkdirAll(dir, 0755)
|
|
os.WriteFile(filepath.Join(dir, "window.json"), data, 0644)
|
|
}
|
|
|
|
// ---- Config loading ----
|
|
|
|
func loadConfig() *OikosConfig {
|
|
data, err := keyring.Get(keyringService, keyringUser)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var cfg OikosConfig
|
|
if err := json.Unmarshal([]byte(data), &cfg); err != nil {
|
|
return nil
|
|
}
|
|
cfg.IsDesktop = true
|
|
return &cfg
|
|
}
|
|
|
|
// ---- Asset handler ----
|
|
|
|
func newAssetHandler(cfg *OikosConfig) http.Handler {
|
|
distFS, err := fs.Sub(assets, "frontend/dist")
|
|
if err != nil {
|
|
log.Fatalf("embedded assets: %v", err)
|
|
}
|
|
|
|
fallback := http.FileServer(http.FS(distFS))
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := r.URL.Path
|
|
if path == "/" || path == "/index.html" {
|
|
data, err := fs.ReadFile(distFS, "index.html")
|
|
if err != nil {
|
|
fallback.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
html := string(data)
|
|
if cfg != nil {
|
|
configJSON, _ := json.Marshal(cfg)
|
|
placeholder := `<script>window.__OIKOS_CONFIG__ = {};</script>`
|
|
injected := fmt.Sprintf(`<script>window.__OIKOS_CONFIG__ = %s;</script>`, configJSON)
|
|
html = strings.ReplaceAll(html, placeholder, injected)
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Write([]byte(html))
|
|
return
|
|
}
|
|
fallback.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// ---- Notifications ----
|
|
|
|
type dashboardSummary struct {
|
|
ApprovalsPending int `json:"approvals_pending"`
|
|
Signals struct {
|
|
Critical int `json:"critical"`
|
|
} `json:"signals_by_severity"`
|
|
}
|
|
|
|
func (d *dashboardSummary) alertCount() int {
|
|
return d.ApprovalsPending + d.Signals.Critical
|
|
}
|
|
|
|
func notify(title, subtitle string) {
|
|
if runtime.GOOS != "darwin" {
|
|
return
|
|
}
|
|
script := fmt.Sprintf(
|
|
`display notification "%s" with title "%s" sound name "default"`,
|
|
strings.ReplaceAll(subtitle, `"`, `\"`),
|
|
strings.ReplaceAll(title, `"`, `\"`),
|
|
)
|
|
exec.Command("osascript", "-e", script).Run()
|
|
}
|
|
|
|
func pollDashboard(cfg *OikosConfig) {
|
|
if cfg == nil || cfg.ApiUrl == "" || cfg.Token == "" {
|
|
return
|
|
}
|
|
|
|
var lastCount int
|
|
first := true
|
|
|
|
for {
|
|
req, err := http.NewRequest("GET", cfg.ApiUrl+"/api/v1/dashboard/summary", nil)
|
|
if err != nil {
|
|
time.Sleep(pollInterval)
|
|
continue
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
time.Sleep(pollInterval)
|
|
continue
|
|
}
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
|
|
var summary dashboardSummary
|
|
if err := json.Unmarshal(body, &summary); err != nil {
|
|
time.Sleep(pollInterval)
|
|
continue
|
|
}
|
|
|
|
if first {
|
|
lastCount = summary.alertCount()
|
|
first = false
|
|
} else {
|
|
current := summary.alertCount()
|
|
if current > lastCount {
|
|
notify("Oikos", fmt.Sprintf("%d pending approval(s), %d critical signal(s)", summary.ApprovalsPending, summary.Signals.Critical))
|
|
}
|
|
lastCount = current
|
|
}
|
|
|
|
time.Sleep(pollInterval)
|
|
}
|
|
}
|
|
|
|
// ---- Auto-update ----
|
|
|
|
type giteaRelease struct {
|
|
TagName string `json:"tag_name"`
|
|
Assets []struct {
|
|
Name string `json:"name"`
|
|
BrowserDownloadURL string `json:"browser_download_url"`
|
|
} `json:"assets"`
|
|
}
|
|
|
|
func checkUpdates() {
|
|
for {
|
|
resp, err := http.Get(updateURL + "?draft=false&pre-release=false&limit=1")
|
|
if err != nil {
|
|
time.Sleep(updateInterval)
|
|
continue
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
|
|
var releases []giteaRelease
|
|
if err := json.Unmarshal(body, &releases); err != nil || len(releases) == 0 {
|
|
time.Sleep(updateInterval)
|
|
continue
|
|
}
|
|
|
|
latest := releases[0]
|
|
latestVersion := strings.TrimPrefix(latest.TagName, "v")
|
|
if latestVersion == version {
|
|
time.Sleep(updateInterval)
|
|
continue
|
|
}
|
|
|
|
app := application.Get()
|
|
if app == nil {
|
|
time.Sleep(updateInterval)
|
|
continue
|
|
}
|
|
|
|
msg := fmt.Sprintf("Version %s is available (you have %s). Download from Gitea releases.", latestVersion, version)
|
|
app.Dialog.Info().
|
|
SetTitle("Update Available").
|
|
SetMessage(msg).
|
|
Show()
|
|
time.Sleep(updateInterval)
|
|
}
|
|
}
|
|
|
|
// ---- Main ----
|
|
|
|
func main() {
|
|
cfg := loadConfig()
|
|
|
|
app := application.New(application.Options{
|
|
Name: "Oikos",
|
|
Description: "Homelab Control Room",
|
|
Services: []application.Service{
|
|
application.NewService(&ConfigService{}),
|
|
},
|
|
Assets: application.AssetOptions{
|
|
Handler: newAssetHandler(cfg),
|
|
},
|
|
Mac: application.MacOptions{
|
|
ApplicationShouldTerminateAfterLastWindowClosed: false,
|
|
},
|
|
})
|
|
|
|
// --- System tray ---
|
|
systemTray := app.SystemTray.New()
|
|
systemTray.SetLabel("Oikos")
|
|
systemTray.SetTooltip("Oikos — Control Room")
|
|
|
|
trayMenu := application.NewMenu()
|
|
trayMenu.Add("Open Control Room").OnClick(func(ctx *application.Context) {
|
|
for _, w := range app.Window.GetAll() {
|
|
w.Show()
|
|
w.Focus()
|
|
}
|
|
})
|
|
trayMenu.AddSeparator()
|
|
trayMenu.Add("Check for Updates").OnClick(func(ctx *application.Context) {
|
|
go checkUpdates() // force immediate check on demand
|
|
})
|
|
trayMenu.AddSeparator()
|
|
trayMenu.Add("Quit").OnClick(func(ctx *application.Context) {
|
|
app.Quit()
|
|
})
|
|
systemTray.SetMenu(trayMenu)
|
|
|
|
// --- Main window ---
|
|
ws := loadWindowState()
|
|
width, height := 1400, 900
|
|
minWidth, minHeight := 1024, 700
|
|
|
|
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
|
Title: "Oikos — Control Room",
|
|
Width: width,
|
|
Height: height,
|
|
MinWidth: minWidth,
|
|
MinHeight: minHeight,
|
|
URL: "/",
|
|
})
|
|
|
|
if ws != nil {
|
|
window.SetPosition(ws.X, ws.Y)
|
|
window.SetSize(ws.Width, ws.Height)
|
|
} else {
|
|
window.Center()
|
|
}
|
|
window.Show()
|
|
|
|
systemTray.AttachWindow(window)
|
|
systemTray.Run()
|
|
|
|
// Register shutdown handler to save window state
|
|
app.OnShutdown(func() {
|
|
saveWindowState(window)
|
|
})
|
|
|
|
// Start background goroutines
|
|
go pollDashboard(cfg)
|
|
go checkUpdates()
|
|
|
|
err := app.Run()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|