wails/v3/examples/server/frontend/dist/index.html
Lea Anthony 9a363d7be5
feat(v3): add server mode for headless HTTP deployment (#4903)
* feat(v3): add server mode for headless HTTP deployment

Server mode allows Wails applications to run as pure HTTP servers
without native GUI dependencies. Enable with `-tags server` build tag.

Features:
- HTTP server with configurable host/port via ServerOptions
- WAILS_SERVER_HOST and WAILS_SERVER_PORT env var overrides
- WebSocket event broadcasting to connected browsers
- Browser clients represented as BrowserWindow (Window interface)
- Health check endpoint at /health
- Graceful shutdown with configurable timeout
- Docker support with Dockerfile.server template and tasks

Build and run:
  wails3 task build:server
  wails3 task run:server
  wails3 task build:docker
  wails3 task run:docker

Documentation at docs/guides/server-build.mdx

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat(v3): add server mode for headless HTTP deployment

Server mode allows Wails applications to run as pure HTTP servers
without native GUI dependencies. Enable with `-tags server` build tag.

Features:
- HTTP server with configurable host/port via ServerOptions
- WAILS_SERVER_HOST and WAILS_SERVER_PORT env var overrides
- WebSocket event broadcasting to connected browsers
- Browser clients represented as BrowserWindow (Window interface)
- Health check endpoint at /health
- Graceful shutdown with configurable timeout
- Docker support with Dockerfile.server template and tasks

Build and run:
  wails3 task build:server
  wails3 task run:server
  wails3 task build:docker
  wails3 task run:docker

Documentation at docs/guides/server-build.mdx

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address CodeRabbit review comments

- Fix corrupted test file with embedded terminal output
- Fix module name mismatch in gin-routing (was gin-example)
- Fix replace directive version mismatch in gin-service
- Fix placeholder module name in ios example (was changeme)
- Fix Dockerfile COPY path to work from both build contexts
- Fix bare URL in README (MD034 compliance)
- Fix comment accuracy in getScreens (returns error, not empty slice)
- Remove deprecated docker-compose version field
- Add port documentation in Taskfile template

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address CodeRabbit review comments

- Add note about healthcheck wget not being available in distroless images
- Add !server build constraint to menu_windows.go and menu_darwin.go
- Downgrade window-visibility-test go.mod from 1.25 to 1.24 to match CI

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 14:33:44 +11:00

187 lines
6.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wails Headless Example</title>
<script type="module" src="/wails/runtime.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 40px;
text-align: center;
max-width: 500px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
h1 {
margin-bottom: 10px;
font-size: 2.5rem;
}
.subtitle {
opacity: 0.8;
margin-bottom: 30px;
}
.badge {
display: inline-block;
background: rgba(255, 255, 255, 0.2);
padding: 5px 15px;
border-radius: 20px;
font-size: 0.9rem;
margin-bottom: 30px;
}
.input-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
input {
flex: 1;
padding: 15px 20px;
border: none;
border-radius: 10px;
font-size: 1rem;
background: rgba(255, 255, 255, 0.9);
color: #333;
}
input:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.3);
}
button {
padding: 15px 30px;
border: none;
border-radius: 10px;
font-size: 1rem;
background: white;
color: #667eea;
cursor: pointer;
font-weight: 600;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
}
button:active {
transform: translateY(0);
}
#result {
margin-top: 20px;
padding: 20px;
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
min-height: 60px;
display: flex;
align-items: center;
justify-content: center;
}
#result.success {
background: rgba(46, 213, 115, 0.2);
}
.info {
margin-top: 30px;
font-size: 0.85rem;
opacity: 0.7;
}
</style>
</head>
<body>
<div class="container">
<h1>Wails Headless</h1>
<p class="subtitle">Running without a native window</p>
<span class="badge">Server Mode</span>
<div class="input-group">
<input type="text" id="nameInput" placeholder="Enter your name..." />
<button onclick="greet()">Greet</button>
</div>
<div id="result">Enter a name and click Greet</div>
<div style="margin-top: 20px;">
<button onclick="broadcast()" style="background: #667eea; color: white;">Broadcast Event</button>
</div>
<div id="events" style="margin-top: 20px; padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px; text-align: left; font-size: 0.9rem;">
<strong>Events:</strong>
<div id="event-log" style="margin-top: 10px; max-height: 150px; overflow-y: auto;"></div>
</div>
<p class="info">
This Wails application is running in headless mode.<br>
It serves content via HTTP without requiring a native GUI.
</p>
</div>
<script type="module">
// Import the generated bindings
// Note: In a real app, you would use the generated TypeScript bindings
window.greet = async function() {
const name = document.getElementById('nameInput').value;
const result = document.getElementById('result');
try {
// Call the Go service method via Wails bindings
// The binding ID is generated from the FQN: main.GreetService.Greet
const response = await wails.Call.ByName('main.GreetService.Greet', name);
result.textContent = response;
result.className = 'success';
} catch (err) {
result.textContent = 'Error: ' + err.message;
result.className = '';
}
};
// Allow Enter key to submit
document.getElementById('nameInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
window.greet();
}
});
// Broadcast an event to all connected clients (via the server)
window.broadcast = async function() {
try {
await wails.Events.Emit('broadcast', { message: 'Hello from browser!', time: new Date().toISOString() });
} catch (err) {
console.error('Failed to broadcast:', err);
}
};
// Set up event handling for server-side events via WebSocket
// The custom.js (loaded by runtime.js) sets up the WebSocket connection
// and calls window._wails.dispatchWailsEvent for each event
const origDispatch = window._wails?.dispatchWailsEvent;
window._wails = window._wails || {};
window._wails.dispatchWailsEvent = function(event) {
// Log the event in the UI
const eventLog = document.getElementById('event-log');
if (eventLog) {
const div = document.createElement('div');
const sender = event.sender ? `<span style="color: orange;">[${event.sender}]</span> ` : '';
div.innerHTML = `<span style="opacity: 0.6;">${new Date().toLocaleTimeString()}</span> ${sender}${event.name}: ${JSON.stringify(event.data)}`;
eventLog.insertBefore(div, eventLog.firstChild); // Newest first
}
// Call original handler if it exists
if (origDispatch) origDispatch.call(window._wails, event);
};
</script>
</body>
</html>