diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
index d3f1da870..4b0b0246f 100644
--- a/.github/FUNDING.yml
+++ b/.github/FUNDING.yml
@@ -1,2 +1,2 @@
-github: [glanceapp]
-patreon: glanceapp
+github: [frozendark01]
+
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index bdd4fe601..3950a4715 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.yml
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -9,8 +9,7 @@ body:
>
> Do not prefix your title with "[BUG]", "[Bug report]", etc., a label will be added automatically.
- If you're unsure whether you're experiencing a bug or not, consider using the [Discussions](https://github.com/glanceapp/glance/discussions) or [Discord](https://discord.com/invite/7KQ7Xa9kJd) to ask for help.
-
+ If you're unsure whether you're experiencing a bug or not, consider using the [Discussions](https://github.com/frozendark01/glance/discussions) to ask for help.
Please include only the information you think is relevant to the bug:
* How did you install Glance? (Docker container, manual binary install, etc)
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
index e8c34af04..abd2aa8b2 100644
--- a/.github/ISSUE_TEMPLATE/config.yml
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -1,8 +1,6 @@
blank_issues_enabled: false
contact_links:
- name: Discussions
- url: https://github.com/glanceapp/glance/discussions
+ url: https://github.com/frozendark01/glance/discussions
about: For help, feedback, guides, resources and more
- - name: Discord
- url: https://discord.com/invite/7KQ7Xa9kJd
- about: Much like the discussions but more chatty
+
diff --git a/.github/LIVE_EVENTS_IMPLEMENTATION.md b/.github/LIVE_EVENTS_IMPLEMENTATION.md
new file mode 100644
index 000000000..78fcaecc4
--- /dev/null
+++ b/.github/LIVE_EVENTS_IMPLEMENTATION.md
@@ -0,0 +1,604 @@
+# Live Events Implementation – Complete Documentation
+
+## Overview
+
+This document describes the **Server-Sent Events (SSE) based real-time dashboard update system** implemented in Glance. The system enables instant reflection of service status changes (e.g., DNS outage, container restart) on the dashboard without requiring manual page refresh.
+
+### Problem Statement
+Previously, when a monitored service changed status, users had to manually refresh the page to see the updated state. This created a poor user experience for services with frequently changing states.
+
+### Solution
+Implemented a push-based real-time notification system using:
+- **Server-Sent Events (SSE)** for bidirectional server-to-client communication
+- **Background monitoring worker** that polls service status at regular intervals
+- **Event debouncing** to prevent notification spam during service flapping
+- **Partial DOM updates** to efficiently refresh only affected widgets
+- **Guard mechanisms** to prevent duplicate initialization of UI components
+
+---
+
+## Architecture Overview
+
+```
+┌─────────────────────────────────────────────────────┐
+│ BROWSER / CLIENT SIDE │
+├─────────────────────────────────────────────────────┤
+│ EventSource Listener (/api/events) │
+│ ├─ Listens for monitor:site_changed events │
+│ ├─ Fetches /api/widgets/{id}/content/ │
+│ └─ Updates DOM with new widget HTML │
+│ │
+│ Setup Functions (with sibling checks) │
+│ ├─ setupCollapsibleLists() │
+│ ├─ setupCollapsibleGrids() │
+│ ├─ setupClocks() │
+│ └─ ... other initializers │
+└─────────────────────────────────────────────────────┘
+ ▲
+ │ SSE Messages
+ │ {type, time, data}
+ │
+┌─────────────────────────────────────────────────────┐
+│ SERVER SIDE (Go Backend) │
+├─────────────────────────────────────────────────────┤
+│ Event Hub (events.go) │
+│ ├─ SSE broadcast to all connected clients │
+│ ├─ Per-widget debounce tracking (5s window) │
+│ └─ Keep-alive pings (30s interval) │
+│ │
+│ Background Monitor Worker (glance.go) │
+│ ├─ Goroutine running every 15 seconds │
+│ ├─ Polls all monitor widgets │
+│ └─ Calls publishEvent() on status changes │
+│ │
+│ Widget Status Detection (widget-monitor.go) │
+│ ├─ Compares current vs previous status │
+│ ├─ Detects timeouts and errors │
+│ └─ Emits monitor:site_changed events │
+│ │
+│ HTTP Endpoints │
+│ ├─ GET /api/events (SSE stream) │
+│ └─ GET /api/widgets/{id}/content/ (partial HTML) │
+└─────────────────────────────────────────────────────┘
+```
+
+---
+
+## Server-Side Implementation
+
+### 1. Event Hub (`internal/glance/events.go`)
+
+**Purpose**: Central hub for SSE message broadcasting and event management.
+
+**Key Structures**:
+```go
+type eventHub struct {
+ clients map[*Client]bool
+ broadcast chan []byte
+ register chan *Client
+ unregister chan *Client
+ lastMonitorEventTimes map[uint64]time.Time // debounce per widget_id
+}
+```
+
+**Key Functions**:
+
+- **`newEventHub()`**: Initializes the hub, starts the broadcast goroutine that handles message distribution
+- **`register(client)`**: Adds a new SSE client connection
+- **`unregister(client)`**: Removes a client and closes its message channel
+- **`broadcast(msg []byte)`**: Queues a message for all connected clients
+- **`publishEvent(eventType, payload)`**:
+ - Publishes typed events with timestamp
+ - Applies **debouncing** for `monitor:site_changed` events
+ - Max 1 event per 5 seconds per `widget_id`
+
+**Debounce Mechanism**:
+```go
+if eventType == "monitor:site_changed" {
+ widgetID := payload["widget_id"].(uint64)
+ lastTime := hub.lastMonitorEventTimes[widgetID]
+ if time.Now().Sub(lastTime) < 5*time.Second {
+ return // skip event, too recent
+ }
+ hub.lastMonitorEventTimes[widgetID] = time.Now()
+}
+```
+
+This prevents message spam when services are flapping (rapidly changing state).
+
+---
+
+### 2. Background Monitor Worker (`internal/glance/glance.go`)
+
+**Purpose**: Continuously monitor service status and trigger updates.
+
+**Initialization**:
+```go
+// In newApplication()
+app.monitorCtx, app.monitorCancel = context.WithCancel(context.Background())
+
+// Start background worker
+go func() {
+ ticker := time.NewTicker(15 * time.Second)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-app.monitorCtx.Done():
+ return
+ case <-ticker.C:
+ // Poll all monitor widgets
+ app.pollMonitorWidgets()
+ }
+ }
+}()
+```
+
+**Lifecycle**:
+- **Startup**: Created when application starts
+- **Operation**: Polls all pages and monitor widgets every 15 seconds
+- **Polite shutdown**: Cancelled via `app.monitorCancel()` when config reloads or server stops
+- **Context usage**: Uses context cancellation for clean goroutine termination
+
+**Widget Content Endpoint**:
+```go
+// GET /api/widgets/{widgetID}/content/
+// Returns rendered HTML for a specific widget
+// Used by client to fetch updated widget after SSE notification
+```
+
+---
+
+### 3. Monitor Widget Status Detection (`internal/glance/widget-monitor.go`)
+
+**Purpose**: Detect service status changes and emit events.
+
+**Implementation**:
+```go
+type monitorWidget struct {
+ Title string
+ Url string
+ Status int // current status
+ PrevStatus int // previous status (for change detection)
+ TimedOut bool
+ Error string
+}
+
+func (w *monitorWidget) update() {
+ // Fetch and update status
+ newStatus := checkService(w.Url)
+
+ // Detect change
+ if newStatus != w.PrevStatus {
+ publishEvent("monitor:site_changed", map[string]interface{}{
+ "widget_id": w.ID,
+ "title": w.Title,
+ "url": w.Url,
+ "status": newStatus,
+ "timed_out": w.TimedOut,
+ "error": w.Error,
+ })
+ w.PrevStatus = newStatus
+ }
+}
+```
+
+**Status Values**:
+- `0`: Service up (green)
+- `1`: Service down (red)
+- `2`: Service degraded/unknown (gray)
+
+**Event Payload**:
+```json
+{
+ "widget_id": 12345,
+ "title": "DNS Server",
+ "url": "127.0.0.1:53",
+ "status": 1,
+ "timed_out": true,
+ "error": "i/o timeout"
+}
+```
+
+---
+
+### 4. HTTP Endpoints
+
+#### `GET /api/events` – SSE Stream
+
+**Purpose**: Establish SSE connection for real-time updates.
+
+**Headers**:
+```
+Content-Type: text/event-stream
+Cache-Control: no-cache
+Connection: keep-alive
+```
+
+**Message Format**:
+```
+event: monitor:site_changed
+data: {"widget_id": 12345, ...}
+
+event: page:update
+data: {"slug": "home"}
+
+: ping
+```
+
+**Keep-Alive**:
+- Sends `:` (comment) every 30 seconds to keep connection alive
+- Prevents reverse proxies/firewalls from closing idle connections
+
+**Error Handling**:
+- Returns `401 Unauthorized` if session invalid
+- Closes on read error
+
+#### `GET /api/widgets/{widgetID}/content/` – Partial Widget Fetch
+
+**Purpose**: Fetch HTML for a single widget after status change.
+
+**Response Format**:
+```html
+
+ ... rendered widget HTML ...
+
+```
+
+**used by**: Client-side JavaScript to update DOM after SSE notification.
+
+---
+
+## Client-Side Implementation
+
+### Template Changes (`internal/glance/templates/widget-base.html`)
+
+**Added Widget Identifier**:
+```html
+
+ ...widget content...
+
+```
+
+The `data-widget-id` attribute allows precise DOM targeting during partial updates.
+
+---
+
+### JavaScript Event Listener (`internal/glance/static/js/page.js`)
+
+**SSE Connection Setup** (lines ~789-810):
+```javascript
+function setupSSE() {
+ const es = new EventSource(`${pageData.baseURL}/api/events`);
+
+ es.onmessage = async function(event) {
+ const msg = JSON.parse(event.data);
+
+ if (msg.type === 'page:update') {
+ // Full page refresh fallback
+ location.reload();
+ } else if (msg.type === 'monitor:site_changed') {
+ // Partial widget update (handles the actual SSE event)
+ }
+ };
+
+ es.onerror = () => {
+ // Reconnect after 3 seconds on error
+ setTimeout(() => setupSSE(), 3000);
+ };
+}
+```
+
+**Monitor Widget Update Handler** (lines ~832-870):
+```javascript
+} else if (msg.type === 'monitor:site_changed') {
+ const widgetId = msg.data.widget_id;
+ const resp = await fetch(`${pageData.baseURL}/api/widgets/${widgetId}/content/`);
+
+ if (resp.ok) {
+ const html = await resp.text();
+ const widgetElem = document.querySelector(`[data-widget-id="${widgetId}"]`);
+
+ if (widgetElem) {
+ widgetElem.outerHTML = html; // Replace old HTML with new
+
+ // Re-initialize all components
+ setupPopovers();
+ setupClocks();
+ setupCarousels();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ // ... other initializers ...
+ }
+ }
+}
+```
+
+---
+
+### Guard Mechanisms to Prevent Duplicate Initialization
+
+**Problem**: After DOM replacement, calling setup functions could attach duplicate event listeners to collapsible elements.
+
+**Solution**: Added guard checks to detect already-initialized elements.
+
+#### setupCollapsibleLists() Guard (`line ~411`):
+```javascript
+function setupCollapsibleLists() {
+ const collapsibleLists = document.querySelectorAll(".list.collapsible-container");
+
+ for (let i = 0; i < collapsibleLists.length; i++) {
+ const list = collapsibleLists[i];
+
+ if (list.dataset.collapseAfter === undefined) continue;
+ if (parseInt(list.dataset.collapseAfter) === -1) continue;
+ if (list.children.length <= parseInt(list.dataset.collapseAfter)) continue;
+
+ // GUARD: Check if button already exists as next sibling
+ if (list.nextElementSibling &&
+ list.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue; // Already initialized, skip
+ }
+
+ attachExpandToggleButton(list);
+ }
+}
+```
+
+#### setupCollapsibleGrids() Guard (`line ~440`):
+```javascript
+function setupCollapsibleGrids() {
+ const collapsibleGridElements = document.querySelectorAll(".cards-grid.collapsible-container");
+
+ for (let i = 0; i < collapsibleGridElements.length; i++) {
+ const gridElement = collapsibleGridElements[i];
+
+ if (gridElement.dataset.collapseAfterRows === undefined) continue;
+
+ // GUARD: Check if button already exists as next sibling
+ if (gridElement.nextElementSibling &&
+ gridElement.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue; // Already initialized, skip
+ }
+
+ // ... rest of setup ...
+ }
+}
+```
+
+**Why Sibling Check?**
+
+The `attachExpandToggleButton()` function adds the button **after** the container:
+```javascript
+collapsibleContainer.after(button); // Adds as next sibling, not child
+```
+
+Therefore:
+- ❌ `querySelector(".expand-toggle-button")` – looks inside container
+- ✅ `nextElementSibling.classList.contains("expand-toggle-button")` – looks for next sibling
+
+---
+
+## Key Changes Summary
+
+### Backend Files Modified
+
+1. **`internal/glance/events.go`** (NEW)
+ - Event hub structure
+ - Broadcast mechanism with debounce
+ - Client connection management
+
+2. **`internal/glance/glance.go`**
+ - Added `monitorCtx` and `monitorCancel` fields
+ - Background worker goroutine (15s polling)
+ - New endpoint: `GET /api/widgets/{id}/content/`
+ - Properly cancelled on reload via defer
+
+3. **`internal/glance/widget-monitor.go`**
+ - Added `PrevStatus` field for change detection
+ - Event emission in `update()` method
+ - Platform-specific monitor implementations
+
+4. **`internal/glance/main.go`**
+ - Added `oldApp` tracking for cleanup
+ - Context cancellation in defer
+ - Proper goroutine shutdown on config reload
+
+5. **`internal/glance/config.go`**
+ - Added `lastRenderedContent` field for caching
+
+### Frontend Files Modified
+
+1. **`internal/glance/templates/widget-base.html`**
+ - Added `data-widget-id="{{ .GetID }}"` attribute for DOM targeting
+
+2. **`internal/glance/static/js/page.js`**
+ - New `setupSSE()` function (establishes EventSource)
+ - SSE event handler with fallback logic
+ - Partial widget update logic (fetch + outerHTML replace)
+ - Guard checks in `setupCollapsibleLists()` and `setupCollapsibleGrids()`
+ - Re-initialization calls with guard protection
+
+---
+
+## Event Flow Diagram
+
+```
+1. Service Status Changes
+ │
+ ▼
+2. Background Worker Detects Change (every 15 seconds)
+ │
+ ▼
+3. publishEvent("monitor:site_changed", {widget_id, status, ...})
+ │
+ ▼
+4. Event Hub Debounces (max 1 per 5 seconds per widget)
+ │
+ ▼
+5. Hub Broadcasts to All Connected Clients
+ │
+ ▼
+6. Client Receives SSE Message
+ │
+ ▼
+7. Client Fetches Widget HTML: /api/widgets/{id}/content/
+ │
+ ▼
+8. Client Replaces DOM: querySelector([data-widget-id]) + outerHTML
+ │
+ ▼
+9. Client Runs Setup Functions with Guard Checks
+ │
+ ▼
+10. Widget Displays Updated Status (with no duplicate buttons)
+```
+
+---
+
+## Testing & Verification
+
+### Manual Testing Procedure
+
+1. **Start Glance with monitor widgets**:
+ ```bash
+ ./glance
+ # Open browser to http://localhost:8080
+ ```
+
+2. **Monitor Widget Setup** (example `glance.yml`):
+ ```yaml
+ pages:
+ - name: home
+ columns: 2
+ widgets:
+ - type: monitor
+ title: "DNS Status"
+ sites:
+ - name: "Cloudflare DNS"
+ url: "https://1.1.1.1"
+```
+
+3. **Trigger Status Change**:
+ - Network disconnect
+ - Service restart
+ - Firewall rule change
+ - Wait for background worker (15 seconds) to detect change
+
+4. **Verify**:
+ - Status updates appear on dashboard within 15 seconds (detection) + 1 second (network roundtrip)
+ - No manual refresh required
+ - "Show more" buttons don't duplicate on collapsible widgets (hacker-news, lobsters, etc.)
+
+### Tested Widgets
+- ✅ Monitor (DNS, HTTP services)
+- ✅ Hacker News (collapsible list)
+- ✅ Lobsters (collapsible grid)
+- ✅ Reddit (collapsible grid)
+- ✅ Twitch Games (collapsible grid)
+
+### Known Limitations
+- Browser offline: SSE reconnects after 3 seconds
+- Network lag: Status update visible after network roundtrip time
+- Rapid changes: Debounce throttles to prevent spam (max 1 per 5 sec per widget)
+- Browser history: Partial updates don't affect browser history
+
+---
+
+## Configuration
+
+No configuration required. The system:
+- Automatically enabled for all pages/widgets
+- Runs in background without user interaction
+- Gracefully degrades if SSE not supported (fallback polling possible)
+- Works with all existing widgets
+
+---
+
+## Performance Characteristics
+
+| Metric | Value | Notes |
+|--------|-------|-------|
+| **Background polling interval** | 15 seconds | Configurable via code change |
+| **Debounce window per widget** | 5 seconds | Prevents flapping events |
+| **SSE keep-alive ping** | 30 seconds | Maintains connection |
+| **Client reconnect delay** | 3 seconds | On connection error |
+| **Memory per client** | ~50 bytes | Per SSE connection |
+| **Message size** | < 500 bytes | Typical status update |
+
+---
+
+## Troubleshooting
+
+### Events Not Appearing
+1. Check browser console for errors
+2. Verify SSE connection: DevTools → Network → Type: "eventsource"
+3. Ensure monitor widgets configured correctly in `glance.yml`
+4. Check server logs for background worker issues
+
+### Duplicate Buttons on Updates
+- **Cause**: Old setupCollapsibleLists/setupCollapsibleGrids without guard checks
+- **Solution**: Ensure `page.js` has `nextElementSibling.classList.contains()` checks
+- **Verify**: Browser DevTools → Elements, count "Show more" buttons (should be 1)
+
+### High CPU Usage
+- **Cause**: Background worker interval too aggressive or too many services
+- **Solution**: Increase polling interval in `glance.go` (currently 15 seconds)
+
+### Connection Timeout
+- **Cause**: Reverse proxy/firewall closing idle connections
+- **Mitigation**: Keep-alive pings every 30 seconds should prevent this
+
+---
+
+## Future Enhancements
+
+Possible improvements:
+1. Configurable polling intervals per widget
+2. Client-side retry strategy with exponential backoff
+3. Service-specific event types (e.g., `monitor:dns_changed` vs `monitor:http_changed`)
+4. Event history/audit log
+5. Webhook support for external integrations
+6. Custom event filtering per widget
+
+---
+
+## Implementation Timeline
+
+| Phase | Date | Components | Status |
+|-------|------|-----------|--------|
+| Phase 1 | Feb 7 | Event hub + background worker | ✅ Complete |
+| Phase 2 | Feb 7 | Fine-grained events + debounce | ✅ Complete |
+| Phase 3 | Feb 7 | Widget-specific endpoints | ✅ Complete |
+| Phase 4 | Feb 7 | Client SSE implementation | ✅ Complete |
+| Phase 5 | Feb 7 | Guard mechanisms (duplicate fix) | ✅ Complete |
+| Phase 6 | Feb 7 | Testing & validation | ✅ Complete |
+
+---
+
+## Code References
+
+**Backend**:
+- Event Hub: [events.go](../internal/glance/events.go)
+- Worker & Endpoints: [glance.go](../internal/glance/glance.go#L1)
+- Monitor Widget: [widget-monitor.go](../internal/glance/widget-monitor.go)
+- Application Setup: [main.go](../internal/glance/main.go)
+- Configuration: [config.go](../internal/glance/config.go)
+
+**Frontend**:
+- SSE Setup & Handlers: [page.js](../internal/glance/static/js/page.js#L789)
+- Guard Checks - Lists: [page.js](../internal/glance/static/js/page.js#L411)
+- Guard Checks - Grids: [page.js](../internal/glance/static/js/page.js#L440)
+- Widget Identifier: [widget-base.html](../templates/widget-base.html)
+
+---
+
+## Conclusion
+
+The live events implementation provides real-time dashboard updates using industry-standard SSE technology. The system is:
+- **Efficient**: Debounce prevents spam, partial updates minimize bandwidth
+- **Reliable**: Context-based cancellation for clean shutdown, SSE auto-reconnect
+- **Safe**: Guard mechanisms prevent duplicate event listeners
+- **Transparent**: Works automatically without user configuration
+
+Users now see service status changes instantly without manual refresh, significantly improving the dashboard experience for monitoring use cases.
diff --git a/.github/SECURITY.md b/.github/SECURITY.md
index b06a6e320..d06225828 100644
--- a/.github/SECURITY.md
+++ b/.github/SECURITY.md
@@ -6,4 +6,4 @@ Security updates will be applied to the latest as well as previous minor version
## Reporting a Vulnerability
-Please report any suspected security vulnerabilities to [glanceapp@duck.com](mailto:glanceapp@duck.com) and do not disclose them publicly. You should receive a response within a few days and if confirmed the issue will be resolved as soon as possible.
+Please report any suspected security vulnerabilities to [Discussions](https://github.com/frozendark01/glance/discussions) and do not disclose them publicly. You should receive a response within a few days and if confirmed the issue will be resolved as soon as possible.
diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml
new file mode 100644
index 000000000..de2f2f9b1
--- /dev/null
+++ b/.github/workflows/docker.yaml
@@ -0,0 +1,51 @@
+name: Build & Push Docker Image
+
+on:
+ push:
+ branches:
+ - "**"
+ tags:
+ - "**"
+
+env:
+ REGISTRY: ghcr.io
+ IMAGE_NAME: ${{ github.repository }}
+
+jobs:
+ build-and-push-image:
+ runs-on: ubuntu-latest
+ # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
+ permissions:
+ contents: read
+ packages: write
+
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v6
+
+ - name: Log in to the Container registry
+ uses: docker/login-action@v3
+ with:
+ registry: ${{ env.REGISTRY }}
+ username: ${{ github.actor }}
+ password: ${{ secrets.GITHUB_TOKEN }}
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+
+ - name: Extract metadata (tags, labels) for Docker
+ id: meta
+ uses: docker/metadata-action@v5
+ with:
+ images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
+
+ - name: Build and push Docker image
+ uses: docker/build-push-action@v6
+ with:
+ context: .
+ file: Dockerfile
+ push: ${{ !startsWith(github.ref_name, 'renovate/') && github.event_name != 'pull_request' }}
+ platforms: linux/amd64,linux/arm64,linux/arm/v7
+ tags: ${{ steps.meta.outputs.tags }}
+ labels: ${{ steps.meta.outputs.labels }}
+ provenance: false
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index 608d82ead..d44fcd0d2 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -2,6 +2,7 @@ name: Create release
permissions:
contents: write
+ packages: write # Necesar pentru a urca imagini pe GHCR
on:
push:
@@ -12,16 +13,17 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- - name: Checkout the target Git reference
+ - name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- - name: Log in to Docker Hub
+ - name: Log in to GHCR
uses: docker/login-action@v3
with:
- username: ${{ secrets.DOCKERHUB_USERNAME }}
- password: ${{ secrets.DOCKERHUB_TOKEN }}
+ registry: ghcr.io
+ username: ${{ github.actor }}
+ password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Golang
uses: actions/setup-go@v5
@@ -33,7 +35,9 @@ jobs:
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
- args: release
+ distribution: goreleaser
+ version: latest
+ args: release --clean
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 2cd84fc05..66306482d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,6 @@
/playground
/.idea
/glance*.yml
+glance
+glance.exe
+*.log
\ No newline at end of file
diff --git a/.goreleaser.yaml b/.goreleaser.yaml
index 7153a4f34..6b898c361 100644
--- a/.goreleaser.yaml
+++ b/.goreleaser.yaml
@@ -1,4 +1,4 @@
-project_name: glanceapp/glance
+project_name: glance
checksum:
disable: true
@@ -9,23 +9,17 @@ builds:
- CGO_ENABLED=0
goos:
- linux
- - openbsd
- - freebsd
- windows
- darwin
goarch:
- amd64
- arm64
- - arm
- - 386
- goarm:
- - 7
ldflags:
- - -s -w -X github.com/glanceapp/glance/internal/glance.buildVersion={{ .Tag }}
+ - -s -w -X github.com/frozendark01/glance/internal/glance.buildVersion={{ .Tag }}
+ main: ./main.go
archives:
- -
- name_template: "glance-{{ .Os }}-{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}"
+ - name_template: "glance-{{ .Os }}-{{ .Arch }}"
files:
- nothing*
format_overrides:
@@ -34,7 +28,7 @@ archives:
dockers:
- image_templates:
- - &amd64_image "{{ .ProjectName }}:{{ .Tag }}-amd64"
+ - &amd64_image "ghcr.io/frozendark01/glance:{{ .Tag }}-amd64"
build_flag_templates:
- --platform=linux/amd64
goarch: amd64
@@ -42,28 +36,19 @@ dockers:
dockerfile: Dockerfile.goreleaser
- image_templates:
- - &arm64v8_image "{{ .ProjectName }}:{{ .Tag }}-arm64"
+ - &arm64v8_image "ghcr.io/frozendark01/glance:{{ .Tag }}-arm64"
build_flag_templates:
- --platform=linux/arm64
goarch: arm64
use: buildx
dockerfile: Dockerfile.goreleaser
- - image_templates:
- - &armv7_image "{{ .ProjectName }}:{{ .Tag }}-armv7"
- build_flag_templates:
- - --platform=linux/arm/v7
- goarch: arm
- goarm: 7
- use: buildx
- dockerfile: Dockerfile.goreleaser
-
docker_manifests:
- - name_template: "{{ .ProjectName }}:{{ .Tag }}"
- image_templates: &multiarch_images
+ - name_template: "ghcr.io/frozendark01/glance:{{ .Tag }}"
+ image_templates:
- *amd64_image
- *arm64v8_image
- - *armv7_image
- - name_template: "{{ .ProjectName }}:latest"
- skip_push: auto
- image_templates: *multiarch_images
+ - name_template: "ghcr.io/frozendark01/glance:latest"
+ image_templates:
+ - *amd64_image
+ - *arm64v8_image
\ No newline at end of file
diff --git a/README.md b/README.md
index 9e59464d4..0eb395b51 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,9 @@

-Glance
+Glance
Install •
Configuration •
- Discord •
- Sponsor
+
Community widgets •
@@ -16,431 +15,77 @@

-## Features
-### Various widgets
-* RSS feeds
-* Subreddit posts
-* Hacker News posts
-* Weather forecasts
-* YouTube channel uploads
-* Twitch channels
-* Market prices
-* Docker containers status
-* Server stats
-* Custom widgets
-* [and many more...](docs/configuration.md#configuring-glance)
-
-### Fast and lightweight
-* Low memory usage
-* Few dependencies
-* Minimal vanilla JS
-* Single <20mb binary available for multiple OSs & architectures and just as small Docker container
-* Uncached pages usually load within ~1s (depending on internet speed and number of widgets)
-
-### Tons of customizability
-* Different layouts
-* As many pages/tabs as you need
-* Numerous configuration options for each widget
-* Multiple styles for some widgets
-* Custom CSS
-
-### Optimized for mobile devices
-Because you'll want to take it with you on the go.
-
-
-
-### Themeable
-Easily create your own theme by tweaking a few numbers or choose from one of the [already available themes](docs/themes.md).
-
-
-
-
-
-## Configuration
-Configuration is done through YAML files, to learn more about how the layout works, how to add more pages and how to configure widgets, visit the [configuration documentation](docs/configuration.md#configuring-glance).
-
-Preview example configuration file
-
-
-```yaml
-pages:
- - name: Home
- columns:
- - size: small
- widgets:
- - type: calendar
- first-day-of-week: monday
-
- - type: rss
- limit: 10
- collapse-after: 3
- cache: 12h
- feeds:
- - url: https://selfh.st/rss/
- title: selfh.st
- limit: 4
- - url: https://ciechanow.ski/atom.xml
- - url: https://www.joshwcomeau.com/rss.xml
- title: Josh Comeau
- - url: https://samwho.dev/rss.xml
- - url: https://ishadeed.com/feed.xml
- title: Ahmad Shadeed
-
- - type: twitch-channels
- channels:
- - theprimeagen
- - j_blow
- - piratesoftware
- - cohhcarnage
- - christitustech
- - EJ_SA
-
- - size: full
- widgets:
- - type: group
- widgets:
- - type: hacker-news
- - type: lobsters
-
- - type: videos
- channels:
- - UCXuqSBlHAE6Xw-yeJA0Tunw # Linus Tech Tips
- - UCR-DXc1voovS8nhAvccRZhg # Jeff Geerling
- - UCsBjURrPoezykLs9EqgamOA # Fireship
- - UCBJycsmduvYEL83R_U4JriQ # Marques Brownlee
- - UCHnyfMqiRRG1u-2MsSQLbXA # Veritasium
-
- - type: group
- widgets:
- - type: reddit
- subreddit: technology
- show-thumbnails: true
- - type: reddit
- subreddit: selfhosted
- show-thumbnails: true
-
- - size: small
- widgets:
- - type: weather
- location: London, United Kingdom
- units: metric
- hour-format: 12h
-
- - type: markets
- markets:
- - symbol: SPY
- name: S&P 500
- - symbol: BTC-USD
- name: Bitcoin
- - symbol: NVDA
- name: NVIDIA
- - symbol: AAPL
- name: Apple
- - symbol: MSFT
- name: Microsoft
-
- - type: releases
- cache: 1d
- repositories:
- - glanceapp/glance
- - go-gitea/gitea
- - immich-app/immich
- - syncthing/syncthing
-```
-
-
-
-
-## Installation
-
-Choose one of the following methods:
-
-
-Docker compose using provided directory structure (recommended)
-
-
-Create a new directory called `glance` as well as the template files within it by running:
-
-```bash
-mkdir glance && cd glance && curl -sL https://github.com/glanceapp/docker-compose-template/archive/refs/heads/main.tar.gz | tar -xzf - --strip-components 2
-```
-
-*[click here to view the files that will be created](https://github.com/glanceapp/docker-compose-template/tree/main/root)*
-
-Then, edit the following files as desired:
-* `docker-compose.yml` to configure the port, volumes and other containery things
-* `config/home.yml` to configure the widgets or layout of the home page
-* `config/glance.yml` if you want to change the theme or add more pages
-
-
-Other files you may want to edit
-
-* `.env` to configure environment variables that will be available inside configuration files
-* `assets/user.css` to add custom CSS
-
-
+### Problem Statement
+Previously, when a monitored service changed status, users had to manually refresh the page to see the updated state. This created a poor user experience for services with frequently changing states.
+
+### Solution for monitoring services eg: DNS, TCP/HTTP/ICMP
+Implemented a push-based real-time notification system using:
+
+* Server-Sent Events (SSE) for bidirectional server-to-client communication
+* Background monitoring worker that polls service status at regular intervals
+* Event debouncing to prevent notification spam during service flapping
+* Partial DOM updates to efficiently refresh only affected widgets
+* Guard mechanisms to prevent duplicate initialization of UI components
+* More detailes - LIVE_EVENTS_IMPLEMENTATION •
+* Docker compose using provided directory structure (recommended)
+
+### Solution for monitoring services eg: custopm-api widgets
+* Added recursive polling that dives into container widgets (groups, split-columns)
+* Polls monitor and custom-api widgets every 15 seconds
+* Detects content changes and emits events
+* Custom-api widget change detection (widget-custom-api.go):
+* Added PrevCompiledHTML field to track previous content
+* Compares rendered HTML to detect changes
+* Emits custom-api:data_changed event when changes detected
+* Event hub with caching (events.go):
+
+* Server-Sent Events broadcast to connected clients
+* Event caching: Stores recent events for reconnecting clients
+* Debouncing per widget to prevent event spam
+* Robust numeric type handling for widget IDs
+* Browser-side DOM updates (page.js):
+
+* Listens for custom-api:data_changed events
+* Fetches updated widget HTML via /api/widgets/{id}/content/
+* Replaces widget DOM with new content
+* Re-initializes all widget setup functions
+* Widget registration fix:
+
+* Widgets inside containers are now registered in widgetByID map
+* Allows endpoint to fetch child widgets by ID
+
+
+### Install Galance
+
+Create a new directory called `glance` and add glance.yml file in the directory
When ready, run:
```bash
-docker compose up -d
-```
-
-If you encounter any issues, you can check the logs by running:
-
-```bash
-docker compose logs
-```
-
-
-
-
-
-Docker compose manual
-
-
-Create a `docker-compose.yml` file with the following contents:
-
-```yaml
services:
glance:
+ image: ghcr.io/frozendark01/glance:main
container_name: glance
- image: glanceapp/glance
restart: unless-stopped
- volumes:
- - ./config:/app/config
+ # If you need Glance to see services running directly on the host (e.g. DNS on port 53)
+ # you can use network_mode: host or add-host
ports:
- - 8080:8080
-```
-
-Then, create a new directory called `config` and download the example starting [`glance.yml`](https://github.com/glanceapp/glance/blob/main/docs/glance.yml) file into it by running:
-
-```bash
-mkdir config && wget -O config/glance.yml https://raw.githubusercontent.com/glanceapp/glance/refs/heads/main/docs/glance.yml
+ - "8080:8080"
+ volumes:
+ - ./config:/app/config:ro # add glance.yml to config folder
+ # If you have custom assets (CSS/JS) that you want to test without rebuilding
+ # - ./public:/app/public:ro
+ environment:
+ - TZ=Etc/UTC
+ - GLANCE_CONFIG=/app/glance.yml
+ # Important for SSE (Live Events) data flow
+ logging:
+ driver: "json-file"
+ options:
+ max-size: "10mb"
+ max-file: "3"
```
-
-Feel free to edit the `glance.yml` file to your liking, and when ready run:
-
```bash
docker compose up -d
```
-If you encounter any issues, you can check the logs by running:
-
-```bash
-docker logs glance
-```
-
-
-
-
-
-Manual binary installation
-
-
-Precompiled binaries are available for Linux, Windows and macOS (x86, x86_64, ARM and ARM64 architectures).
-
-### Linux
-
-Visit the [latest release page](https://github.com/glanceapp/glance/releases/latest) for available binaries. You can place the binary in `/opt/glance/` and have it start with your server via a [systemd service](https://linuxhandbook.com/create-systemd-services/). By default, when running the binary, it will look for a `glance.yml` file in the directory it's placed in. To specify a different path for the config file, use the `--config` option:
-
-```bash
-/opt/glance/glance --config /etc/glance.yml
-```
-
-To grab a starting template for the config file, run:
-
-```bash
-wget https://raw.githubusercontent.com/glanceapp/glance/refs/heads/main/docs/glance.yml
-```
-
-### Windows
-
-Download and extract the executable from the [latest release](https://github.com/glanceapp/glance/releases/latest) (most likely the file called `glance-windows-amd64.zip` if you're on a 64-bit system) and place it in a folder of your choice. Then, create a new text file called `glance.yml` in the same folder and paste the content from [here](https://raw.githubusercontent.com/glanceapp/glance/refs/heads/main/docs/glance.yml) in it. You should then be able to run the executable and access the dashboard by visiting `http://localhost:8080` in your browser.
-
-
-
-
-
-
-
-Other
-
-
-Glance can also be installed through the following 3rd party channels:
-* [Proxmox VE Helper Script](https://community-scripts.github.io/ProxmoxVE/scripts?id=glance)
-* [NixOS package](https://search.nixos.org/packages?channel=unstable&show=glance)
-* [Coolify.io](https://coolify.io/docs/services/glance/)
-
-
-
-
-
-
-## Common issues
-
-Requests timing out
-
-The most common cause of this is when using Pi-Hole, AdGuard Home or other ad-blocking DNS services, which by default have a fairly low rate limit. Depending on the number of widgets you have in a single page, this limit can very easily be exceeded. To fix this, increase the rate limit in the settings of your DNS service.
-
-If using Podman, in some rare cases the timeout can be caused by an unknown issue, in which case it may be resolved by adding the following to the bottom of your `docker-compose.yml` file:
-```yaml
-networks:
- podman:
- external: true
-```
-
-
-
-Broken layout for markets, bookmarks or other widgets
-
-This is almost always caused by the browser extension Dark Reader. To fix this, disable dark mode for the domain where Glance is hosted.
-
-
-
-cannot unmarshal !!map into []glance.page
-
-The most common cause of this is having a `pages` key in your `glance.yml` and then also having a `pages` key inside one of your included pages. To fix this, remove the `pages` key from the top of your included pages.
-
-
-
-
-
-## FAQ
-
-Does the information on the page update automatically?
-No, a page refresh is required to update the information. Some things do dynamically update where it makes sense, like the clock widget and the relative time showing how long ago something happened.
-
-
-
-How frequently do widgets update?
-No requests are made periodically in the background, information is only fetched upon loading the page and then cached. The default cache lifetime is different for each widget and can be configured.
-
-
-
-Can I create my own widgets?
-
-Yes, there are multiple ways to create custom widgets:
-* `iframe` widget - allows you to embed things from other websites
-* `html` widget - allows you to insert your own static HTML
-* `extension` widget - fetch HTML from a URL
-* `custom-api` widget - fetch JSON from a URL and render it using custom HTML
-
-
-
-Can I change the title of a widget?
-
-Yes, the title of all widgets can be changed by specifying the `title` property in the widget's configuration:
-
-```yaml
-- type: rss
- title: My custom title
-
-- type: markets
- title: My custom title
-
-- type: videos
- title: My custom title
-
-# and so on for all widgets...
-```
-
-
-
-
-## Feature requests
-
-New feature suggestions are always welcome and will be considered, though please keep in mind that some of them may be out of scope for what the project is trying to achieve (or is reasonably capable of). If you have an idea for a new feature and would like to share it, you can do so [here](https://github.com/glanceapp/glance/issues/new?template=feature_request.yml).
-
-Feature requests are tagged with one of the following:
-
-* [Roadmap](https://github.com/glanceapp/glance/labels/roadmap) - will be implemented in a future release
-* [Backlog](https://github.com/glanceapp/glance/labels/backlog) - may be implemented in the future but needs further feedback or interest from the community
-* [Icebox](https://github.com/glanceapp/glance/labels/icebox) - no plans to implement as it doesn't currently align with the project's goals or capabilities, may be revised at a later date
-
-
-
-## Building from source
-
-Choose one of the following methods:
-
-
-Build binary with Go
-
-
-Requirements: [Go](https://go.dev/dl/) >= v1.23
-
-To build the project for your current OS and architecture, run:
-
-```bash
-go build -o build/glance .
-```
-
-To build for a specific OS and architecture, run:
-
-```bash
-GOOS=linux GOARCH=amd64 go build -o build/glance .
-```
-
-[*click here for a full list of GOOS and GOARCH combinations*](https://go.dev/doc/install/source#:~:text=$GOOS%20and%20$GOARCH)
-
-Alternatively, if you just want to run the app without creating a binary, like when you're testing out changes, you can run:
-
-```bash
-go run .
-```
-
-
-
-
-Build project and Docker image with Docker
-
-
-Requirements: [Docker](https://docs.docker.com/engine/install/)
-
-To build the project and image using just Docker, run:
-
-*(replace `owner` with your name or organization)*
-
-```bash
-docker build -t owner/glance:latest .
-```
-
-If you wish to push the image to a registry (by default Docker Hub), run:
-
-```bash
-docker push owner/glance:latest
-```
-
-
-
-
-
-
-## Contributing guidelines
-
-* Before working on a new feature it's preferable to submit a feature request first and state that you'd like to implement it yourself
-* Please don't submit PRs for feature requests that are either in the roadmap[1], backlog[2] or icebox[3]
-* Use `dev` for the base branch if you're adding new features or fixing bugs, otherwise use `main`
-* Avoid introducing new dependencies
-* Avoid making backwards-incompatible configuration changes
-* Avoid introducing new colors or hard-coding colors, use the standard `primary`, `positive` and `negative`
-* For icons, try to use [heroicons](https://heroicons.com/) where applicable
-* Provide a screenshot of the changes if UI related where possible
-* No `package.json`
-
-
-[1] [2] [3]
-
-[1] The feature likely already has work put into it that may conflict with your implementation
-
-[2] The demand, implementation or functionality for this feature is not yet clear
-
-[3] No plans to add this feature for the time being
-
-
-
-
-
-## Thank you
-
-To all the people who were generous enough to [sponsor](https://github.com/sponsors/glanceapp) the project and to everyone who has contributed in any way, be it PRs, submitting issues, helping others in the discussions or Discord server, creating guides and tools or just mentioning Glance on social media. Your support is greatly appreciated and helps keep the project going.
+This is a fork of Glance Dashboard with Live-Events. No manual refresh needed!
diff --git a/ct/glance.sh b/ct/glance.sh
new file mode 100644
index 000000000..c05a0cdf8
--- /dev/null
+++ b/ct/glance.sh
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+
+# Glance Installer Script (FrozenDark Edition)
+USER_REPO="frozendark01/glance"
+INSTALL_PATH="/opt/glance"
+
+echo -e "\e[32m[Info] Initializing Glance (FrozenDark Edition)...\e[0m"
+
+# 1. Architecture Detection
+ARCH=$(uname -m)
+case $ARCH in
+ x86_64) BINARY_ARCH="amd64" ;;
+ aarch64) BINARY_ARCH="arm64" ;;
+ *) echo -e "\e[31m[Error] Unsupported architecture: $ARCH\e[0m"; exit 1 ;;
+esac
+
+# 2. Fetching the latest version from your GitHub repository
+RELEASE=$(curl -s https://api.github.com/repos/${USER_REPO}/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3)}')
+
+if [ -z "$RELEASE" ]; then
+ echo -e "\e[31m[Error] Could not find any version in the repository ${USER_REPO}.\e[0m"
+ exit 1
+fi
+
+echo -e "\e[34m[Info] Detected version: $RELEASE\e[0m"
+
+# 3. Environment preparation
+mkdir -p $INSTALL_PATH
+if systemctl is-active --quiet glance; then
+ echo -e "\e[33m[Info] Stopping service for update...\e[0m"
+ systemctl stop glance
+fi
+
+# 4. Download and installation (according to the names in goreleaser.yaml)
+URL="https://github.com/${USER_REPO}/releases/download/${RELEASE}/glance-linux-${BINARY_ARCH}.tar.gz"
+echo -e "\e[32m[Info] Downloading binary: $URL\e[0m"
+
+if curl -L "$URL" | tar xz -C $INSTALL_PATH glance; then
+ chmod +x $INSTALL_PATH/glance
+ echo -e "\e[32m[Success] Binary installed at $INSTALL_PATH/glance\e[0m"
+else
+ echo -e "\e[31m[Error] Download failed!\e[0m"
+ exit 1
+fi
+
+# 5. Default configuration (if not already present)
+if [ ! -f "$INSTALL_PATH/glance.yml" ]; then
+ cat <$INSTALL_PATH/glance.yml
+pages:
+ - name: FrozenDark Dashboard
+ width: slim
+ columns:
+ - size: full
+ widgets:
+ - type: search
+ - type: monitors
+EOF
+fi
+
+# 6. Systemd Service Configuration
+cat </etc/systemd/system/glance.service
+[Unit]
+Description=Glance Dashboard Daemon
+After=network.target
+
+[Service]
+Type=simple
+WorkingDirectory=$INSTALL_PATH
+ExecStart=$INSTALL_PATH/glance --config $INSTALL_PATH/glance.yml
+Restart=always
+RestartSec=5
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+# 7. Log Cleanup (every 3 days) - Cron Automation
+# We look for logs in the working directory and delete them at midnight every 3 days
+if ! crontab -l 2>/dev/null | grep -q "$INSTALL_PATH/*.log"; then
+ (crontab -l 2>/dev/null; echo "0 0 */3 * * find $INSTALL_PATH -name '*.log' -delete") | crontab -
+ echo -e "\e[32m[Info] Automatic log cleanup set for every 3 days.\e[0m"
+fi
+
+# 8. Start Service
+systemctl daemon-reload
+systemctl enable -q --now glance
+
+echo -e "\e[32m--------------------------------------------------\e[0m"
+echo -e "\e[32m[DONE] Glance ${RELEASE} is now running on port 8080!\e[0m"
+echo -e "\e[32m--------------------------------------------------\e[0m"
\ No newline at end of file
diff --git a/docs/LIVE_EVENTS_IMPLEMENTATION.md b/docs/LIVE_EVENTS_IMPLEMENTATION.md
new file mode 100644
index 000000000..78fcaecc4
--- /dev/null
+++ b/docs/LIVE_EVENTS_IMPLEMENTATION.md
@@ -0,0 +1,604 @@
+# Live Events Implementation – Complete Documentation
+
+## Overview
+
+This document describes the **Server-Sent Events (SSE) based real-time dashboard update system** implemented in Glance. The system enables instant reflection of service status changes (e.g., DNS outage, container restart) on the dashboard without requiring manual page refresh.
+
+### Problem Statement
+Previously, when a monitored service changed status, users had to manually refresh the page to see the updated state. This created a poor user experience for services with frequently changing states.
+
+### Solution
+Implemented a push-based real-time notification system using:
+- **Server-Sent Events (SSE)** for bidirectional server-to-client communication
+- **Background monitoring worker** that polls service status at regular intervals
+- **Event debouncing** to prevent notification spam during service flapping
+- **Partial DOM updates** to efficiently refresh only affected widgets
+- **Guard mechanisms** to prevent duplicate initialization of UI components
+
+---
+
+## Architecture Overview
+
+```
+┌─────────────────────────────────────────────────────┐
+│ BROWSER / CLIENT SIDE │
+├─────────────────────────────────────────────────────┤
+│ EventSource Listener (/api/events) │
+│ ├─ Listens for monitor:site_changed events │
+│ ├─ Fetches /api/widgets/{id}/content/ │
+│ └─ Updates DOM with new widget HTML │
+│ │
+│ Setup Functions (with sibling checks) │
+│ ├─ setupCollapsibleLists() │
+│ ├─ setupCollapsibleGrids() │
+│ ├─ setupClocks() │
+│ └─ ... other initializers │
+└─────────────────────────────────────────────────────┘
+ ▲
+ │ SSE Messages
+ │ {type, time, data}
+ │
+┌─────────────────────────────────────────────────────┐
+│ SERVER SIDE (Go Backend) │
+├─────────────────────────────────────────────────────┤
+│ Event Hub (events.go) │
+│ ├─ SSE broadcast to all connected clients │
+│ ├─ Per-widget debounce tracking (5s window) │
+│ └─ Keep-alive pings (30s interval) │
+│ │
+│ Background Monitor Worker (glance.go) │
+│ ├─ Goroutine running every 15 seconds │
+│ ├─ Polls all monitor widgets │
+│ └─ Calls publishEvent() on status changes │
+│ │
+│ Widget Status Detection (widget-monitor.go) │
+│ ├─ Compares current vs previous status │
+│ ├─ Detects timeouts and errors │
+│ └─ Emits monitor:site_changed events │
+│ │
+│ HTTP Endpoints │
+│ ├─ GET /api/events (SSE stream) │
+│ └─ GET /api/widgets/{id}/content/ (partial HTML) │
+└─────────────────────────────────────────────────────┘
+```
+
+---
+
+## Server-Side Implementation
+
+### 1. Event Hub (`internal/glance/events.go`)
+
+**Purpose**: Central hub for SSE message broadcasting and event management.
+
+**Key Structures**:
+```go
+type eventHub struct {
+ clients map[*Client]bool
+ broadcast chan []byte
+ register chan *Client
+ unregister chan *Client
+ lastMonitorEventTimes map[uint64]time.Time // debounce per widget_id
+}
+```
+
+**Key Functions**:
+
+- **`newEventHub()`**: Initializes the hub, starts the broadcast goroutine that handles message distribution
+- **`register(client)`**: Adds a new SSE client connection
+- **`unregister(client)`**: Removes a client and closes its message channel
+- **`broadcast(msg []byte)`**: Queues a message for all connected clients
+- **`publishEvent(eventType, payload)`**:
+ - Publishes typed events with timestamp
+ - Applies **debouncing** for `monitor:site_changed` events
+ - Max 1 event per 5 seconds per `widget_id`
+
+**Debounce Mechanism**:
+```go
+if eventType == "monitor:site_changed" {
+ widgetID := payload["widget_id"].(uint64)
+ lastTime := hub.lastMonitorEventTimes[widgetID]
+ if time.Now().Sub(lastTime) < 5*time.Second {
+ return // skip event, too recent
+ }
+ hub.lastMonitorEventTimes[widgetID] = time.Now()
+}
+```
+
+This prevents message spam when services are flapping (rapidly changing state).
+
+---
+
+### 2. Background Monitor Worker (`internal/glance/glance.go`)
+
+**Purpose**: Continuously monitor service status and trigger updates.
+
+**Initialization**:
+```go
+// In newApplication()
+app.monitorCtx, app.monitorCancel = context.WithCancel(context.Background())
+
+// Start background worker
+go func() {
+ ticker := time.NewTicker(15 * time.Second)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-app.monitorCtx.Done():
+ return
+ case <-ticker.C:
+ // Poll all monitor widgets
+ app.pollMonitorWidgets()
+ }
+ }
+}()
+```
+
+**Lifecycle**:
+- **Startup**: Created when application starts
+- **Operation**: Polls all pages and monitor widgets every 15 seconds
+- **Polite shutdown**: Cancelled via `app.monitorCancel()` when config reloads or server stops
+- **Context usage**: Uses context cancellation for clean goroutine termination
+
+**Widget Content Endpoint**:
+```go
+// GET /api/widgets/{widgetID}/content/
+// Returns rendered HTML for a specific widget
+// Used by client to fetch updated widget after SSE notification
+```
+
+---
+
+### 3. Monitor Widget Status Detection (`internal/glance/widget-monitor.go`)
+
+**Purpose**: Detect service status changes and emit events.
+
+**Implementation**:
+```go
+type monitorWidget struct {
+ Title string
+ Url string
+ Status int // current status
+ PrevStatus int // previous status (for change detection)
+ TimedOut bool
+ Error string
+}
+
+func (w *monitorWidget) update() {
+ // Fetch and update status
+ newStatus := checkService(w.Url)
+
+ // Detect change
+ if newStatus != w.PrevStatus {
+ publishEvent("monitor:site_changed", map[string]interface{}{
+ "widget_id": w.ID,
+ "title": w.Title,
+ "url": w.Url,
+ "status": newStatus,
+ "timed_out": w.TimedOut,
+ "error": w.Error,
+ })
+ w.PrevStatus = newStatus
+ }
+}
+```
+
+**Status Values**:
+- `0`: Service up (green)
+- `1`: Service down (red)
+- `2`: Service degraded/unknown (gray)
+
+**Event Payload**:
+```json
+{
+ "widget_id": 12345,
+ "title": "DNS Server",
+ "url": "127.0.0.1:53",
+ "status": 1,
+ "timed_out": true,
+ "error": "i/o timeout"
+}
+```
+
+---
+
+### 4. HTTP Endpoints
+
+#### `GET /api/events` – SSE Stream
+
+**Purpose**: Establish SSE connection for real-time updates.
+
+**Headers**:
+```
+Content-Type: text/event-stream
+Cache-Control: no-cache
+Connection: keep-alive
+```
+
+**Message Format**:
+```
+event: monitor:site_changed
+data: {"widget_id": 12345, ...}
+
+event: page:update
+data: {"slug": "home"}
+
+: ping
+```
+
+**Keep-Alive**:
+- Sends `:` (comment) every 30 seconds to keep connection alive
+- Prevents reverse proxies/firewalls from closing idle connections
+
+**Error Handling**:
+- Returns `401 Unauthorized` if session invalid
+- Closes on read error
+
+#### `GET /api/widgets/{widgetID}/content/` – Partial Widget Fetch
+
+**Purpose**: Fetch HTML for a single widget after status change.
+
+**Response Format**:
+```html
+
+ ... rendered widget HTML ...
+
+```
+
+**used by**: Client-side JavaScript to update DOM after SSE notification.
+
+---
+
+## Client-Side Implementation
+
+### Template Changes (`internal/glance/templates/widget-base.html`)
+
+**Added Widget Identifier**:
+```html
+
+ ...widget content...
+
+```
+
+The `data-widget-id` attribute allows precise DOM targeting during partial updates.
+
+---
+
+### JavaScript Event Listener (`internal/glance/static/js/page.js`)
+
+**SSE Connection Setup** (lines ~789-810):
+```javascript
+function setupSSE() {
+ const es = new EventSource(`${pageData.baseURL}/api/events`);
+
+ es.onmessage = async function(event) {
+ const msg = JSON.parse(event.data);
+
+ if (msg.type === 'page:update') {
+ // Full page refresh fallback
+ location.reload();
+ } else if (msg.type === 'monitor:site_changed') {
+ // Partial widget update (handles the actual SSE event)
+ }
+ };
+
+ es.onerror = () => {
+ // Reconnect after 3 seconds on error
+ setTimeout(() => setupSSE(), 3000);
+ };
+}
+```
+
+**Monitor Widget Update Handler** (lines ~832-870):
+```javascript
+} else if (msg.type === 'monitor:site_changed') {
+ const widgetId = msg.data.widget_id;
+ const resp = await fetch(`${pageData.baseURL}/api/widgets/${widgetId}/content/`);
+
+ if (resp.ok) {
+ const html = await resp.text();
+ const widgetElem = document.querySelector(`[data-widget-id="${widgetId}"]`);
+
+ if (widgetElem) {
+ widgetElem.outerHTML = html; // Replace old HTML with new
+
+ // Re-initialize all components
+ setupPopovers();
+ setupClocks();
+ setupCarousels();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ // ... other initializers ...
+ }
+ }
+}
+```
+
+---
+
+### Guard Mechanisms to Prevent Duplicate Initialization
+
+**Problem**: After DOM replacement, calling setup functions could attach duplicate event listeners to collapsible elements.
+
+**Solution**: Added guard checks to detect already-initialized elements.
+
+#### setupCollapsibleLists() Guard (`line ~411`):
+```javascript
+function setupCollapsibleLists() {
+ const collapsibleLists = document.querySelectorAll(".list.collapsible-container");
+
+ for (let i = 0; i < collapsibleLists.length; i++) {
+ const list = collapsibleLists[i];
+
+ if (list.dataset.collapseAfter === undefined) continue;
+ if (parseInt(list.dataset.collapseAfter) === -1) continue;
+ if (list.children.length <= parseInt(list.dataset.collapseAfter)) continue;
+
+ // GUARD: Check if button already exists as next sibling
+ if (list.nextElementSibling &&
+ list.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue; // Already initialized, skip
+ }
+
+ attachExpandToggleButton(list);
+ }
+}
+```
+
+#### setupCollapsibleGrids() Guard (`line ~440`):
+```javascript
+function setupCollapsibleGrids() {
+ const collapsibleGridElements = document.querySelectorAll(".cards-grid.collapsible-container");
+
+ for (let i = 0; i < collapsibleGridElements.length; i++) {
+ const gridElement = collapsibleGridElements[i];
+
+ if (gridElement.dataset.collapseAfterRows === undefined) continue;
+
+ // GUARD: Check if button already exists as next sibling
+ if (gridElement.nextElementSibling &&
+ gridElement.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue; // Already initialized, skip
+ }
+
+ // ... rest of setup ...
+ }
+}
+```
+
+**Why Sibling Check?**
+
+The `attachExpandToggleButton()` function adds the button **after** the container:
+```javascript
+collapsibleContainer.after(button); // Adds as next sibling, not child
+```
+
+Therefore:
+- ❌ `querySelector(".expand-toggle-button")` – looks inside container
+- ✅ `nextElementSibling.classList.contains("expand-toggle-button")` – looks for next sibling
+
+---
+
+## Key Changes Summary
+
+### Backend Files Modified
+
+1. **`internal/glance/events.go`** (NEW)
+ - Event hub structure
+ - Broadcast mechanism with debounce
+ - Client connection management
+
+2. **`internal/glance/glance.go`**
+ - Added `monitorCtx` and `monitorCancel` fields
+ - Background worker goroutine (15s polling)
+ - New endpoint: `GET /api/widgets/{id}/content/`
+ - Properly cancelled on reload via defer
+
+3. **`internal/glance/widget-monitor.go`**
+ - Added `PrevStatus` field for change detection
+ - Event emission in `update()` method
+ - Platform-specific monitor implementations
+
+4. **`internal/glance/main.go`**
+ - Added `oldApp` tracking for cleanup
+ - Context cancellation in defer
+ - Proper goroutine shutdown on config reload
+
+5. **`internal/glance/config.go`**
+ - Added `lastRenderedContent` field for caching
+
+### Frontend Files Modified
+
+1. **`internal/glance/templates/widget-base.html`**
+ - Added `data-widget-id="{{ .GetID }}"` attribute for DOM targeting
+
+2. **`internal/glance/static/js/page.js`**
+ - New `setupSSE()` function (establishes EventSource)
+ - SSE event handler with fallback logic
+ - Partial widget update logic (fetch + outerHTML replace)
+ - Guard checks in `setupCollapsibleLists()` and `setupCollapsibleGrids()`
+ - Re-initialization calls with guard protection
+
+---
+
+## Event Flow Diagram
+
+```
+1. Service Status Changes
+ │
+ ▼
+2. Background Worker Detects Change (every 15 seconds)
+ │
+ ▼
+3. publishEvent("monitor:site_changed", {widget_id, status, ...})
+ │
+ ▼
+4. Event Hub Debounces (max 1 per 5 seconds per widget)
+ │
+ ▼
+5. Hub Broadcasts to All Connected Clients
+ │
+ ▼
+6. Client Receives SSE Message
+ │
+ ▼
+7. Client Fetches Widget HTML: /api/widgets/{id}/content/
+ │
+ ▼
+8. Client Replaces DOM: querySelector([data-widget-id]) + outerHTML
+ │
+ ▼
+9. Client Runs Setup Functions with Guard Checks
+ │
+ ▼
+10. Widget Displays Updated Status (with no duplicate buttons)
+```
+
+---
+
+## Testing & Verification
+
+### Manual Testing Procedure
+
+1. **Start Glance with monitor widgets**:
+ ```bash
+ ./glance
+ # Open browser to http://localhost:8080
+ ```
+
+2. **Monitor Widget Setup** (example `glance.yml`):
+ ```yaml
+ pages:
+ - name: home
+ columns: 2
+ widgets:
+ - type: monitor
+ title: "DNS Status"
+ sites:
+ - name: "Cloudflare DNS"
+ url: "https://1.1.1.1"
+```
+
+3. **Trigger Status Change**:
+ - Network disconnect
+ - Service restart
+ - Firewall rule change
+ - Wait for background worker (15 seconds) to detect change
+
+4. **Verify**:
+ - Status updates appear on dashboard within 15 seconds (detection) + 1 second (network roundtrip)
+ - No manual refresh required
+ - "Show more" buttons don't duplicate on collapsible widgets (hacker-news, lobsters, etc.)
+
+### Tested Widgets
+- ✅ Monitor (DNS, HTTP services)
+- ✅ Hacker News (collapsible list)
+- ✅ Lobsters (collapsible grid)
+- ✅ Reddit (collapsible grid)
+- ✅ Twitch Games (collapsible grid)
+
+### Known Limitations
+- Browser offline: SSE reconnects after 3 seconds
+- Network lag: Status update visible after network roundtrip time
+- Rapid changes: Debounce throttles to prevent spam (max 1 per 5 sec per widget)
+- Browser history: Partial updates don't affect browser history
+
+---
+
+## Configuration
+
+No configuration required. The system:
+- Automatically enabled for all pages/widgets
+- Runs in background without user interaction
+- Gracefully degrades if SSE not supported (fallback polling possible)
+- Works with all existing widgets
+
+---
+
+## Performance Characteristics
+
+| Metric | Value | Notes |
+|--------|-------|-------|
+| **Background polling interval** | 15 seconds | Configurable via code change |
+| **Debounce window per widget** | 5 seconds | Prevents flapping events |
+| **SSE keep-alive ping** | 30 seconds | Maintains connection |
+| **Client reconnect delay** | 3 seconds | On connection error |
+| **Memory per client** | ~50 bytes | Per SSE connection |
+| **Message size** | < 500 bytes | Typical status update |
+
+---
+
+## Troubleshooting
+
+### Events Not Appearing
+1. Check browser console for errors
+2. Verify SSE connection: DevTools → Network → Type: "eventsource"
+3. Ensure monitor widgets configured correctly in `glance.yml`
+4. Check server logs for background worker issues
+
+### Duplicate Buttons on Updates
+- **Cause**: Old setupCollapsibleLists/setupCollapsibleGrids without guard checks
+- **Solution**: Ensure `page.js` has `nextElementSibling.classList.contains()` checks
+- **Verify**: Browser DevTools → Elements, count "Show more" buttons (should be 1)
+
+### High CPU Usage
+- **Cause**: Background worker interval too aggressive or too many services
+- **Solution**: Increase polling interval in `glance.go` (currently 15 seconds)
+
+### Connection Timeout
+- **Cause**: Reverse proxy/firewall closing idle connections
+- **Mitigation**: Keep-alive pings every 30 seconds should prevent this
+
+---
+
+## Future Enhancements
+
+Possible improvements:
+1. Configurable polling intervals per widget
+2. Client-side retry strategy with exponential backoff
+3. Service-specific event types (e.g., `monitor:dns_changed` vs `monitor:http_changed`)
+4. Event history/audit log
+5. Webhook support for external integrations
+6. Custom event filtering per widget
+
+---
+
+## Implementation Timeline
+
+| Phase | Date | Components | Status |
+|-------|------|-----------|--------|
+| Phase 1 | Feb 7 | Event hub + background worker | ✅ Complete |
+| Phase 2 | Feb 7 | Fine-grained events + debounce | ✅ Complete |
+| Phase 3 | Feb 7 | Widget-specific endpoints | ✅ Complete |
+| Phase 4 | Feb 7 | Client SSE implementation | ✅ Complete |
+| Phase 5 | Feb 7 | Guard mechanisms (duplicate fix) | ✅ Complete |
+| Phase 6 | Feb 7 | Testing & validation | ✅ Complete |
+
+---
+
+## Code References
+
+**Backend**:
+- Event Hub: [events.go](../internal/glance/events.go)
+- Worker & Endpoints: [glance.go](../internal/glance/glance.go#L1)
+- Monitor Widget: [widget-monitor.go](../internal/glance/widget-monitor.go)
+- Application Setup: [main.go](../internal/glance/main.go)
+- Configuration: [config.go](../internal/glance/config.go)
+
+**Frontend**:
+- SSE Setup & Handlers: [page.js](../internal/glance/static/js/page.js#L789)
+- Guard Checks - Lists: [page.js](../internal/glance/static/js/page.js#L411)
+- Guard Checks - Grids: [page.js](../internal/glance/static/js/page.js#L440)
+- Widget Identifier: [widget-base.html](../templates/widget-base.html)
+
+---
+
+## Conclusion
+
+The live events implementation provides real-time dashboard updates using industry-standard SSE technology. The system is:
+- **Efficient**: Debounce prevents spam, partial updates minimize bandwidth
+- **Reliable**: Context-based cancellation for clean shutdown, SSE auto-reconnect
+- **Safe**: Guard mechanisms prevent duplicate event listeners
+- **Transparent**: Works automatically without user configuration
+
+Users now see service status changes instantly without manual refresh, significantly improving the dashboard experience for monitoring use cases.
diff --git a/docs/configuration.md b/docs/configuration.md
index 174de834d..b3ac57a2f 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -149,14 +149,14 @@ pages:
columns:
- size: full
widgets:
- $include: rss.yml
+ - $include: rss.yml
- name: News
columns:
- size: full
widgets:
- type: group
widgets:
- $include: rss.yml
+ - $include: rss.yml
- type: reddit
subreddit: news
```
diff --git a/docs/glance.yml b/docs/glance.yml
index b5c68c4c7..4def5f30b 100644
--- a/docs/glance.yml
+++ b/docs/glance.yml
@@ -1,105 +1,67 @@
pages:
- - name: Home
- # Optionally, if you only have a single page you can hide the desktop navigation for a cleaner look
- # hide-desktop-navigation: true
+ - name: Startpage
+ width: slim
+ hide-desktop-navigation: false
+ center-vertically: true
columns:
- size: small
widgets:
+ - type: clock
+ format: "HH:mm"
+ timezone: "America/New_York"
- type: calendar
- first-day-of-week: monday
-
- - type: rss
- limit: 10
- collapse-after: 3
- cache: 12h
- feeds:
- - url: https://selfh.st/rss/
- title: selfh.st
- limit: 4
- - url: https://ciechanow.ski/atom.xml
- - url: https://www.joshwcomeau.com/rss.xml
- title: Josh Comeau
- - url: https://samwho.dev/rss.xml
- - url: https://ishadeed.com/feed.xml
- title: Ahmad Shadeed
-
- - type: twitch-channels
- channels:
- - theprimeagen
- - j_blow
- - giantwaffle
- - cohhcarnage
- - christitustech
- - EJ_SA
- size: full
widgets:
- - type: group
- widgets:
- - type: hacker-news
- - type: lobsters
-
- - type: videos
- channels:
- - UCXuqSBlHAE6Xw-yeJA0Tunw # Linus Tech Tips
- - UCR-DXc1voovS8nhAvccRZhg # Jeff Geerling
- - UCsBjURrPoezykLs9EqgamOA # Fireship
- - UCBJycsmduvYEL83R_U4JriQ # Marques Brownlee
- - UCHnyfMqiRRG1u-2MsSQLbXA # Veritasium
-
- - type: group
- widgets:
- - type: reddit
- subreddit: technology
- show-thumbnails: true
- - type: reddit
- subreddit: selfhosted
- show-thumbnails: true
-
- - size: small
- widgets:
- - type: weather
- location: London, United Kingdom
- units: metric # alternatively "imperial"
- hour-format: 12h # alternatively "24h"
- # Optionally hide the location from being displayed in the widget
- # hide-location: true
-
- - type: markets
- markets:
- - symbol: SPY
- name: S&P 500
- - symbol: BTC-USD
- name: Bitcoin
- - symbol: NVDA
- name: NVIDIA
- - symbol: AAPL
- name: Apple
- - symbol: MSFT
- name: Microsoft
-
- - type: releases
- cache: 1d
- # Without authentication the Github API allows for up to 60 requests per hour. You can create a
- # read-only token from your Github account settings and use it here to increase the limit.
- # token: ...
- repositories:
- - glanceapp/glance
- - go-gitea/gitea
- - immich-app/immich
- - syncthing/syncthing
-
- # Add more pages here:
- # - name: Your page name
- # columns:
- # - size: small
- # widgets:
- # # Add widgets here
+ - type: search
+ autofocus: true
- # - size: full
- # widgets:
- # # Add widgets here
+ - type: monitor
+ cache: 1m
+ title: Services
+ sites:
+ - title: Jellyfin
+ url: https://yourdomain.com/
+ icon: si:jellyfin
+ - title: Gitea
+ url: https://yourdomain.com/
+ icon: si:gitea
+ - title: qBittorrent # only for Linux ISOs, of course
+ url: https://yourdomain.com/
+ icon: si:qbittorrent
+ - title: Immich
+ url: https://yourdomain.com/
+ icon: si:immich
+ - title: AdGuard Home
+ url: https://yourdomain.com/
+ icon: si:adguard
+ - title: Vaultwarden
+ url: https://yourdomain.com/
+ icon: si:vaultwarden
- # - size: small
- # widgets:
- # # Add widgets here
+ - type: bookmarks
+ groups:
+ - title: General
+ links:
+ - title: Gmail
+ url: https://mail.google.com/mail/u/0/
+ - title: Amazon
+ url: https://www.amazon.com/
+ - title: Github
+ url: https://github.com/
+ - title: Entertainment
+ links:
+ - title: YouTube
+ url: https://www.youtube.com/
+ - title: Prime Video
+ url: https://www.primevideo.com/
+ - title: Disney+
+ url: https://www.disneyplus.com/
+ - title: Social
+ links:
+ - title: Reddit
+ url: https://www.reddit.com/
+ - title: Twitter
+ url: https://twitter.com/
+ - title: Instagram
+ url: https://www.instagram.com/
\ No newline at end of file
diff --git a/internal/glance/config.go b/internal/glance/config.go
index d4d6af0e8..ff76460a8 100644
--- a/internal/glance/config.go
+++ b/internal/glance/config.go
@@ -87,8 +87,9 @@ type page struct {
Size string `yaml:"size"`
Widgets widgets `yaml:"widgets"`
} `yaml:"columns"`
- PrimaryColumnIndex int8 `yaml:"-"`
- mu sync.Mutex `yaml:"-"`
+ PrimaryColumnIndex int8 `yaml:"-"`
+ mu sync.Mutex `yaml:"-"`
+ lastRenderedContent []byte `yaml:"-"`
}
func newConfigFromYAML(contents []byte) (*config, error) {
diff --git a/internal/glance/events.go b/internal/glance/events.go
new file mode 100644
index 000000000..a463fd07b
--- /dev/null
+++ b/internal/glance/events.go
@@ -0,0 +1,250 @@
+package glance
+
+import (
+ "encoding/json"
+ "log"
+ "net/http"
+ "strconv"
+ "sync"
+ "time"
+)
+
+type eventHub struct {
+ mu sync.Mutex
+ clients map[chan []byte]struct{}
+ lastMonitorEventTimes map[uint64]time.Time // debounce per widget
+ monitorEventDebounceTime time.Duration
+ lastEvents map[string][]byte // recent events cache keyed by eventType|widget
+}
+
+func newEventHub() *eventHub {
+ return &eventHub{
+ clients: make(map[chan []byte]struct{}),
+ lastMonitorEventTimes: make(map[uint64]time.Time),
+ monitorEventDebounceTime: 5 * time.Second,
+ lastEvents: make(map[string][]byte),
+ }
+}
+
+func (h *eventHub) register() chan []byte {
+ ch := make(chan []byte, 8)
+ h.mu.Lock()
+ h.clients[ch] = struct{}{}
+ // snapshot recent events to send after unlocking
+ recent := make([][]byte, 0, len(h.lastEvents))
+ for _, v := range h.lastEvents {
+ // copy to avoid race
+ b := make([]byte, len(v))
+ copy(b, v)
+ recent = append(recent, b)
+ }
+ h.mu.Unlock()
+ // Only log in debug scenarios
+ // clientsCount := len(h.clients)
+ // log.Printf("SSE client registered (clients=%d)", clientsCount)
+
+ // deliver recent events asynchronously so register() remains non-blocking
+ go func() {
+ for _, ev := range recent {
+ select {
+ case ch <- ev:
+ default:
+ // drop if client channel is full
+ }
+ }
+ }()
+ return ch
+}
+
+func (h *eventHub) unregister(ch chan []byte) {
+ h.mu.Lock()
+ delete(h.clients, ch)
+ // clientsCount := len(h.clients)
+ h.mu.Unlock()
+ close(ch)
+ // Only log in debug scenarios
+ // log.Printf("SSE client unregistered (clients=%d)", clientsCount)
+}
+
+func (h *eventHub) broadcast(msg []byte) {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+
+ for ch := range h.clients {
+ select {
+ case ch <- msg:
+ default:
+ // drop message for slow client
+ }
+ }
+}
+
+// global hub instance
+var globalEventHub *eventHub
+
+// handleEvents serves an SSE stream to the client
+func (a *application) handleEvents(w http.ResponseWriter, r *http.Request) {
+ if globalEventHub == nil {
+ http.Error(w, "events not available", http.StatusServiceUnavailable)
+ return
+ }
+
+ flusher, ok := w.(http.Flusher)
+ if !ok {
+ http.Error(w, "streaming unsupported", http.StatusInternalServerError)
+ return
+ }
+
+ // security: simple auth via same cookie handling as other endpoints
+ if a.handleUnauthorizedResponse(w, r, showUnauthorizedJSON) {
+ return
+ }
+
+ // set SSE headers
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Connection", "keep-alive")
+
+ msgCh := globalEventHub.register()
+ defer globalEventHub.unregister(msgCh)
+
+ // Only log client connection in debug scenarios
+ // log.Printf("handleEvents: client connected %s", r.RemoteAddr)
+
+ ctx := r.Context()
+
+ // send a ping every 30s to keep connection alive
+ pingTicker := time.NewTicker(30 * time.Second)
+ defer pingTicker.Stop()
+
+ // initial comment to establish stream
+ w.Write([]byte(": ok\n\n"))
+ flusher.Flush()
+
+ for {
+ select {
+ case <-ctx.Done():
+ // Only log in debug scenarios
+ // log.Printf("handleEvents: client context done %s", r.RemoteAddr)
+ return
+ case msg, ok := <-msgCh:
+ if !ok {
+ return
+ }
+ // write data and check for errors so we can log disconnect causes
+ if _, err := w.Write([]byte("data: ")); err != nil {
+ log.Printf("handleEvents: write error (data prefix) %s: %v", r.RemoteAddr, err)
+ return
+ }
+ if _, err := w.Write(msg); err != nil {
+ log.Printf("handleEvents: write error (msg) %s: %v", r.RemoteAddr, err)
+ return
+ }
+ if _, err := w.Write([]byte("\n\n")); err != nil {
+ log.Printf("handleEvents: write error (terminator) %s: %v", r.RemoteAddr, err)
+ return
+ }
+ // Flush and check; Flush has no error return so rely on ctx.Done if connection closed
+ flusher.Flush()
+ case <-pingTicker.C:
+ // send a keep-alive comment
+ if _, err := w.Write([]byte(": ping\n\n")); err != nil {
+ log.Printf("handleEvents: write error (ping) %s: %v", r.RemoteAddr, err)
+ return
+ }
+ flusher.Flush()
+ }
+ }
+}
+
+// parseWidgetID extracts uint64 widget_id from various numeric types in a map
+func parseWidgetID(m map[string]any) (uint64, bool) {
+ if raw, exists := m["widget_id"]; exists {
+ switch v := raw.(type) {
+ case float64:
+ return uint64(v), true
+ case int:
+ return uint64(v), true
+ case int64:
+ return uint64(v), true
+ case uint64:
+ return v, true
+ case json.Number:
+ if n, err := v.Int64(); err == nil {
+ return uint64(n), true
+ }
+ }
+ }
+ return 0, false
+}
+
+// helper to publish JSON event
+func publishEvent(eventType string, payload any) {
+ if globalEventHub == nil {
+ return
+ }
+
+ // debounce monitor events per widget to avoid flapping
+ if eventType == "monitor:site_changed" {
+ if payloadMap, ok := payload.(map[string]any); ok {
+ if widgetIDUint, ok := parseWidgetID(payloadMap); ok {
+ globalEventHub.mu.Lock()
+ if lastTime, exists := globalEventHub.lastMonitorEventTimes[widgetIDUint]; exists {
+ if time.Since(lastTime) < globalEventHub.monitorEventDebounceTime {
+ globalEventHub.mu.Unlock()
+ return // drop event, too soon
+ }
+ }
+ globalEventHub.lastMonitorEventTimes[widgetIDUint] = time.Now()
+ globalEventHub.mu.Unlock()
+ }
+ }
+ }
+
+ wrapper := map[string]any{
+ "type": eventType,
+ "time": time.Now().Unix(),
+ "data": payload,
+ }
+
+ b, err := json.Marshal(wrapper)
+ if err != nil {
+ log.Printf("failed to marshal event: %v", err)
+ return
+ }
+
+ // cache recent monitor/custom-api/page events so reconnecting clients can catch up
+ if globalEventHub != nil {
+ key := ""
+ if eventType == "monitor:site_changed" || eventType == "custom-api:data_changed" {
+ if payloadMap, ok := payload.(map[string]any); ok {
+ if widgetIDUint, ok := parseWidgetID(payloadMap); ok {
+ key = eventType + "|" + strconv.FormatUint(widgetIDUint, 10)
+ }
+ }
+ } else if eventType == "page:update" {
+ if payloadMap, ok := payload.(map[string]any); ok {
+ if slug, ok := payloadMap["slug"].(string); ok {
+ key = eventType + "|" + slug
+ }
+ }
+ }
+
+ globalEventHub.mu.Lock()
+ clientsCount := len(globalEventHub.clients)
+ if key != "" {
+ // store a copy
+ copyB := make([]byte, len(b))
+ copy(copyB, b)
+ globalEventHub.lastEvents[key] = copyB
+ }
+ globalEventHub.mu.Unlock()
+
+ // Only log if no clients connected (potential issue)
+ if clientsCount == 0 && eventType != "monitor:site_changed" && eventType != "page:update" {
+ log.Printf("publishing event %s (no clients)", eventType)
+ }
+ }
+
+ globalEventHub.broadcast(b)
+}
diff --git a/internal/glance/glance.go b/internal/glance/glance.go
index 28771fa54..94db6f613 100644
--- a/internal/glance/glance.go
+++ b/internal/glance/glance.go
@@ -42,6 +42,25 @@ type application struct {
usernameHashToUsername map[string]string
authAttemptsMu sync.Mutex
failedAuthAttempts map[string]*failedAuthAttempt
+ monitorCtx context.Context
+ monitorCancel context.CancelFunc
+}
+
+// registerWidgetRecursive registers a widget and all its child widgets (if any)
+func (a *application) registerWidgetRecursive(w widget) {
+ a.widgetByID[w.GetID()] = w
+
+ // If this widget is a container (group or split-column), register its children too
+ switch cw := w.(type) {
+ case *groupWidget:
+ for _, childWidget := range cw.Widgets {
+ a.registerWidgetRecursive(childWidget)
+ }
+ case *splitColumnWidget:
+ for _, childWidget := range cw.Widgets {
+ a.registerWidgetRecursive(childWidget)
+ }
+ }
}
func newApplication(c *config) (*application, error) {
@@ -52,6 +71,9 @@ func newApplication(c *config) (*application, error) {
slugToPage: make(map[string]*page),
widgetByID: make(map[uint64]widget),
}
+
+ // initialize global event hub
+ globalEventHub = newEventHub()
config := &app.Config
//
@@ -150,6 +172,9 @@ func newApplication(c *config) (*application, error) {
assetResolver: app.StaticAssetPath,
}
+ // initialize context for background workers
+ app.monitorCtx, app.monitorCancel = context.WithCancel(context.Background())
+
for p := range config.Pages {
page := &config.Pages[p]
page.PrimaryColumnIndex = -1
@@ -174,7 +199,7 @@ func newApplication(c *config) (*application, error) {
for i := range page.HeadWidgets {
widget := page.HeadWidgets[i]
- app.widgetByID[widget.GetID()] = widget
+ app.registerWidgetRecursive(widget)
widget.setProviders(providers)
}
@@ -187,7 +212,7 @@ func newApplication(c *config) (*application, error) {
for w := range column.Widgets {
widget := column.Widgets[w]
- app.widgetByID[widget.GetID()] = widget
+ app.registerWidgetRecursive(widget)
widget.setProviders(providers)
}
}
@@ -227,6 +252,51 @@ func newApplication(c *config) (*application, error) {
}
app.parsedManifest = []byte(manifest)
+ // start background updater to check monitor and custom-api widgets periodically
+ go func() {
+ ticker := time.NewTicker(15 * time.Second)
+ defer ticker.Stop()
+
+ updateWidgetIfNeeded := func(w widget) {
+ if mon, ok := w.(*monitorWidget); ok {
+ mon.update(app.monitorCtx)
+ } else if api, ok := w.(*customAPIWidget); ok {
+ api.update(app.monitorCtx)
+ }
+ }
+
+ var updateWidgetsRecursive func([]widget)
+ updateWidgetsRecursive = func(widgets []widget) {
+ for i := range widgets {
+ w := widgets[i]
+ updateWidgetIfNeeded(w)
+ // recursively update widgets in containers
+ if group, ok := w.(*groupWidget); ok {
+ updateWidgetsRecursive(group.Widgets)
+ } else if split, ok := w.(*splitColumnWidget); ok {
+ updateWidgetsRecursive(split.Widgets)
+ }
+ }
+ }
+
+ for {
+ select {
+ case <-app.monitorCtx.Done():
+ return
+ case <-ticker.C:
+ for _, page := range app.slugToPage {
+ page.mu.Lock()
+ // recursively update head and column widgets
+ updateWidgetsRecursive(page.HeadWidgets)
+ for c := range page.Columns {
+ updateWidgetsRecursive(page.Columns[c].Widgets)
+ }
+ page.mu.Unlock()
+ }
+ }
+ }
+ }()
+
return app, nil
}
@@ -355,6 +425,16 @@ func (a *application) handlePageContentRequest(w http.ResponseWriter, r *http.Re
page.updateOutdatedWidgets()
err = pageContentTemplate.Execute(&responseBytes, pageData)
+
+ // detect content changes and publish event
+ newContent := responseBytes.Bytes()
+ if !bytes.Equal(newContent, page.lastRenderedContent) {
+ // copy content
+ page.lastRenderedContent = make([]byte, len(newContent))
+ copy(page.lastRenderedContent, newContent)
+ // publish an event that this page changed
+ publishEvent("page:update", map[string]string{"slug": page.Slug})
+ }
}()
if err != nil {
@@ -424,6 +504,31 @@ func (a *application) handleWidgetRequest(w http.ResponseWriter, r *http.Request
// widget.handleRequest(w, r)
}
+func (a *application) handleWidgetContentRequest(w http.ResponseWriter, r *http.Request) {
+ widgetValue := r.PathValue("widget")
+
+ widgetID, err := strconv.ParseUint(widgetValue, 10, 64)
+ if err != nil {
+ a.handleNotFound(w, r)
+ return
+ }
+
+ widget, exists := a.widgetByID[widgetID]
+ if !exists {
+ a.handleNotFound(w, r)
+ return
+ }
+
+ if a.handleUnauthorizedResponse(w, r, showUnauthorizedJSON) {
+ return
+ }
+
+ // render widget HTML and return
+ html := widget.Render()
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ w.Write([]byte(html))
+}
+
func (a *application) StaticAssetPath(asset string) string {
return a.Config.Server.BaseURL + "/static/" + staticFSHash + "/" + asset
}
@@ -440,6 +545,10 @@ func (a *application) server() (func() error, func() error) {
mux.HandleFunc("GET /{page}", a.handlePageRequest)
mux.HandleFunc("GET /api/pages/{page}/content/{$}", a.handlePageContentRequest)
+ // widget content endpoint for partial updates
+ mux.HandleFunc("GET /api/widgets/{widget}/content/{$}", a.handleWidgetContentRequest)
+ // SSE endpoint for live events
+ mux.HandleFunc("GET /api/events", a.handleEvents)
if !a.Config.Theme.DisablePicker {
mux.HandleFunc("POST /api/set-theme/{key}", a.handleThemeChangeRequest)
diff --git a/internal/glance/main.go b/internal/glance/main.go
index 6d73a831e..3e49e90a9 100644
--- a/internal/glance/main.go
+++ b/internal/glance/main.go
@@ -97,6 +97,14 @@ func serveApp(configPath string) error {
exitChannel := make(chan struct{})
hadValidConfigOnStartup := false
var stopServer func() error
+ var oldApp *application
+
+ // defer cleanup of background workers
+ defer func() {
+ if oldApp != nil && oldApp.monitorCancel != nil {
+ oldApp.monitorCancel()
+ }
+ }()
onChange := func(newContents []byte) {
if stopServer != nil {
@@ -125,6 +133,13 @@ func serveApp(configPath string) error {
return
}
+ // cancel background workers from old application
+ if oldApp != nil && oldApp.monitorCancel != nil {
+ oldApp.monitorCancel()
+ }
+
+ oldApp = app
+
if !hadValidConfigOnStartup {
hadValidConfigOnStartup = true
}
@@ -170,6 +185,7 @@ func serveApp(configPath string) error {
return fmt.Errorf("creating application: %w", err)
}
+ oldApp = app
startServer, _ := app.server()
if err := startServer(); err != nil {
return fmt.Errorf("starting server: %w", err)
diff --git a/internal/glance/static/js/page.js b/internal/glance/static/js/page.js
index 0212a4fa2..d763befb5 100644
--- a/internal/glance/static/js/page.js
+++ b/internal/glance/static/js/page.js
@@ -407,6 +407,11 @@ function setupCollapsibleLists() {
continue;
}
+ // guard: skip if already initialized (button is added as next sibling, not child)
+ if (list.nextElementSibling && list.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue;
+ }
+
attachExpandToggleButton(list);
for (let c = collapseAfter; c < list.children.length; c++) {
@@ -431,6 +436,11 @@ function setupCollapsibleGrids() {
continue;
}
+ // guard: skip if already initialized (button is added as next sibling, not child)
+ if (gridElement.nextElementSibling && gridElement.nextElementSibling.classList.contains("expand-toggle-button")) {
+ continue;
+ }
+
const collapseAfterRows = parseInt(gridElement.dataset.collapseAfterRows);
if (collapseAfterRows == -1) {
@@ -578,23 +588,37 @@ function setupClocks() {
}
const updateCallbacks = [];
+ const clockStates = [];
for (var i = 0; i < clocks.length; i++) {
const clock = clocks[i];
+
+ // Guard: skip if already initialized
+ if (clock.dataset.clockInitialized === 'true') {
+ continue;
+ }
+ clock.dataset.clockInitialized = 'true';
+
const hourFormat = clock.dataset.hourFormat;
const localTimeContainer = clock.querySelector('[data-local-time]');
const localDateElement = localTimeContainer.querySelector('[data-date]');
const localWeekdayElement = localTimeContainer.querySelector('[data-weekday]');
const localYearElement = localTimeContainer.querySelector('[data-year]');
+ const timeElement = localTimeContainer.querySelector('[data-time]');
const timeZoneContainers = clock.querySelectorAll('[data-time-in-zone]');
- const setLocalTime = makeSettableTimeElement(
- localTimeContainer.querySelector('[data-time]'),
- hourFormat
- );
+ const clockState = { timeDisplay: '', initialized: false };
+ clockStates.push(clockState);
+
+ const setLocalTime = makeSettableTimeElement(timeElement, hourFormat);
updateCallbacks.push((now) => {
- setLocalTime(now);
+ if (!clockState.initialized) {
+ setLocalTime(now);
+ clockState.initialized = true;
+ } else {
+ setLocalTime(now);
+ }
localDateElement.textContent = now.getDate() + ' ' + monthNames[now.getMonth()];
localWeekdayElement.textContent = weekDayNames[now.getDay()];
localYearElement.textContent = now.getFullYear();
@@ -625,10 +649,13 @@ function setupClocks() {
for (var i = 0; i < updateCallbacks.length; i++)
updateCallbacks[i](now);
- setTimeout(updateClocks, (60 - now.getSeconds()) * 1000);
+ setTimeout(updateClocks, 15000);
};
- updateClocks();
+ // Only start the update loop if there are clocks to update
+ if (updateCallbacks.length > 0) {
+ updateClocks();
+ }
}
async function setupCalendars() {
@@ -748,7 +775,7 @@ async function setupPage() {
const pageElement = document.getElementById("page");
const pageContentElement = document.getElementById("page-content");
- const pageContent = await fetchPageContent(pageData);
+ let pageContent = await fetchPageContent(pageData);
pageContentElement.innerHTML = pageContent;
@@ -781,6 +808,201 @@ async function setupPage() {
document.body.classList.add("page-columns-transitioned");
}, 300);
}
+
+ // try to setup SSE stream; fall back to polling if not available
+ const setupSSE = () => {
+ if (typeof EventSource === 'undefined') return false;
+
+ try {
+ const es = new EventSource(`${pageData.baseURL}/api/events`);
+
+ es.onmessage = async (ev) => {
+ try {
+ const msg = JSON.parse(ev.data);
+ if (msg.type === 'page:update' && msg.data && msg.data.slug === pageData.slug) {
+ const newContent = await fetchPageContent(pageData);
+ if (newContent !== pageContent) {
+ pageContent = newContent;
+ pageContentElement.innerHTML = newContent;
+
+ try {
+ setupPopovers();
+ setupClocks();
+ await setupCalendars();
+ await setupTodos();
+ setupCarousels();
+ setupSearchBoxes();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ setupGroups();
+ setupMasonries();
+ setupDynamicRelativeTime();
+ setupLazyImages();
+
+ for (let i = 0; i < contentReadyCallbacks.length; i++) {
+ contentReadyCallbacks[i]();
+ }
+ } catch (e) {
+ console.error('Error applying SSE-updated page content', e);
+ }
+ }
+ } else if (msg.type === 'monitor:site_changed') {
+ // update only the affected widget if possible
+ try {
+ const widgetId = msg.data && msg.data.widget_id;
+ if (widgetId) {
+ const resp = await fetch(`${pageData.baseURL}/api/widgets/${widgetId}/content/`);
+ if (resp.ok) {
+ const html = await resp.text();
+ const widgetElem = document.querySelector(`[data-widget-id="${widgetId}"]`);
+ if (widgetElem) {
+ widgetElem.outerHTML = html;
+
+ // re-run initializers globally
+ // setup functions now have guards to prevent duplicate listeners
+ setupPopovers();
+ setupClocks();
+ setupCarousels();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ setupGroups();
+ setupMasonries();
+ setupDynamicRelativeTime();
+ setupLazyImages();
+
+ for (let i = 0; i < contentReadyCallbacks.length; i++) {
+ contentReadyCallbacks[i]();
+ }
+ }
+ }
+ } else {
+ // fallback to full page fetch if widget_id missing
+ const newContent = await fetchPageContent(pageData);
+ if (newContent !== pageContent) {
+ pageContent = newContent;
+ pageContentElement.innerHTML = newContent;
+ }
+ }
+ } catch (e) {
+ console.error('Failed to fetch widget content after monitor event', e);
+ }
+ } else if (msg.type === 'custom-api:data_changed') {
+ // update custom-api widget when data changes
+ try {
+ const widgetId = msg.data && msg.data.widget_id;
+ if (widgetId) {
+ const resp = await fetch(`${pageData.baseURL}/api/widgets/${widgetId}/content/`);
+ if (resp.ok) {
+ const html = await resp.text();
+ const widgetElem = document.querySelector(`[data-widget-id="${widgetId}"]`);
+ if (widgetElem && html !== widgetElem.outerHTML) {
+ widgetElem.outerHTML = html;
+
+ // re-run initializers globally
+ // setup functions now have guards to prevent duplicate listeners
+ setupPopovers();
+ setupClocks();
+ setupCarousels();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ setupGroups();
+ setupMasonries();
+ setupDynamicRelativeTime();
+ setupLazyImages();
+
+ for (let i = 0; i < contentReadyCallbacks.length; i++) {
+ contentReadyCallbacks[i]();
+ }
+ }
+ }
+ }
+ } catch (e) {
+ console.error('Failed to fetch widget content after custom-api event', e);
+ }
+ }
+ } catch (e) {
+ console.error('Failed to handle SSE message', e);
+ }
+ };
+
+ es.onerror = (e) => {
+ console.error('SSE connection error', e);
+ // Do not forcibly close the EventSource here. Let the browser
+ // manage automatic reconnects. Forcing a close causes the
+ // client to unregister immediately which can cause missed
+ // events when they occur during a reconnect window.
+ };
+
+ return true;
+ } catch (e) {
+ return false;
+ }
+ };
+
+ // start a lightweight poller to refresh page content when it changes
+ const defaultLivePollIntervalMs = 15000;
+
+ const startLivePoller = () => {
+ const intervalMs = parseInt(pageElement.dataset.livePollInterval) || defaultLivePollIntervalMs;
+ let lastContent = pageContent;
+ let timeoutId = null;
+
+ const poll = async () => {
+ if (document.hidden) {
+ timeoutId = setTimeout(poll, intervalMs);
+ return;
+ }
+
+ try {
+ const newContent = await fetchPageContent(pageData);
+ if (newContent !== lastContent) {
+ lastContent = newContent;
+ pageContentElement.innerHTML = newContent;
+
+ try {
+ setupPopovers();
+ setupClocks();
+ await setupCalendars();
+ await setupTodos();
+ setupCarousels();
+ setupSearchBoxes();
+ setupCollapsibleLists();
+ setupCollapsibleGrids();
+ setupGroups();
+ setupMasonries();
+ setupDynamicRelativeTime();
+ setupLazyImages();
+
+ for (let i = 0; i < contentReadyCallbacks.length; i++) {
+ contentReadyCallbacks[i]();
+ }
+ } catch (e) {
+ console.error('Error applying updated page content', e);
+ }
+ }
+ } catch (e) {
+ console.error('Live poll failed:', e);
+ }
+
+ timeoutId = setTimeout(poll, intervalMs);
+ };
+
+ // pause/resume on visibilitychange
+ document.addEventListener('visibilitychange', () => {
+ if (document.hidden) {
+ if (timeoutId) clearTimeout(timeoutId);
+ return;
+ }
+
+ // resume immediately when visible
+ poll();
+ });
+
+ poll();
+ };
+
+ const sseActive = setupSSE();
+ if (!sseActive) startLivePoller();
}
setupPage();
diff --git a/internal/glance/templates/clock.html b/internal/glance/templates/clock.html
index 1bc0bf526..096cfef70 100644
--- a/internal/glance/templates/clock.html
+++ b/internal/glance/templates/clock.html
@@ -8,7 +8,7 @@
diff --git a/internal/glance/templates/widget-base.html b/internal/glance/templates/widget-base.html
index 019ff000e..d092e5037 100644
--- a/internal/glance/templates/widget-base.html
+++ b/internal/glance/templates/widget-base.html
@@ -1,4 +1,4 @@
-