wails/v3/pkg/application/single_instance_linux.go
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

101 lines
2.3 KiB
Go

//go:build linux && !android && !server
package application
import (
"errors"
"os"
"strings"
"sync"
"syscall"
"github.com/godbus/dbus/v5"
)
type dbusHandler func(string)
var setup sync.Once
func (f dbusHandler) SendMessage(message string) *dbus.Error {
f(message)
return nil
}
type linuxLock struct {
file *os.File
uniqueID string
dbusPath string
dbusName string
manager *singleInstanceManager
}
func newPlatformLock(manager *singleInstanceManager) (platformLock, error) {
return &linuxLock{
manager: manager,
}, nil
}
func (l *linuxLock) acquire(uniqueID string) error {
if uniqueID == "" {
return errors.New("UniqueID is required for single instance lock")
}
id := "wails_app_" + strings.ReplaceAll(strings.ReplaceAll(uniqueID, "-", "_"), ".", "_")
l.dbusName = "org." + id + ".SingleInstance"
l.dbusPath = "/org/" + id + "/SingleInstance"
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
setup.Do(func() {
f := dbusHandler(func(message string) {
secondInstanceBuffer <- message
})
err = conn.Export(f, dbus.ObjectPath(l.dbusPath), l.dbusName)
})
if err != nil {
return err
}
reply, err := conn.RequestName(l.dbusName, dbus.NameFlagDoNotQueue)
if err != nil {
return err
}
// if name already taken, try to send args to existing instance, if no success just launch new instance
if reply == dbus.RequestNameReplyExists {
return alreadyRunningError
}
return nil
}
func (l *linuxLock) release() {
if l.file != nil {
syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN)
l.file.Close()
os.Remove(l.file.Name())
l.file = nil
}
}
func (l *linuxLock) notify(data string) error {
conn, err := dbus.ConnectSessionBus()
// if we will reach any error during establishing connection or sending message we will just continue.
// It should not be the case that such thing will happen actually, but just in case.
if err != nil {
return err
}
err = conn.Object(l.dbusName, dbus.ObjectPath(l.dbusPath)).Call(l.dbusName+".SendMessage", 0, data).Store()
if err != nil {
return err
}
os.Exit(l.manager.options.ExitCode)
return nil
}