wails/v3/examples/gin-example
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
..
static Update docs + examples. 2025-03-11 08:50:03 +11:00
go.mod feat(v3): add server mode for headless HTTP deployment (#4903) 2026-01-25 14:33:44 +11:00
go.sum feat(v3): add server mode for headless HTTP deployment (#4903) 2026-01-25 14:33:44 +11:00
main.go Refactor Manager API to use singular naming convention (#4367) 2025-06-22 12:19:14 +10:00
README.md Add gin example 2025-03-09 14:38:46 +11:00

Gin Example

This example demonstrates how to use the Gin web framework with Wails.

Overview

This example shows how to:

  • Set up Gin as the asset handler for a Wails application
  • Create a middleware that routes requests between Wails and Gin
  • Define API endpoints with Gin
  • Communicate between the Gin-served frontend and Wails backend
  • Implement custom Gin middleware

Running the Example

cd v3/examples/gin-example
go mod tidy
go run .

How It Works

The example uses Gin's HTTP router to serve the frontend content whilst still allowing Wails to handle its internal routes. This is achieved through:

  1. Creating a Gin router with routes for the frontend
  2. Implementing a middleware function that decides whether to pass requests to Gin or let Wails handle them
  3. Configuring the Wails application to use both the Gin router as the asset handler and the custom middleware

Wails-Gin Integration

The key part of the integration is the middleware function:

func GinMiddleware(ginEngine *gin.Engine) application.Middleware {
    return func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // Let Wails handle its internal routes
            if r.URL.Path == "/wails/runtime.js" || r.URL.Path == "/wails/ipc" {
                next.ServeHTTP(w, r)
                return
            }

            // Let Gin handle everything else
            ginEngine.ServeHTTP(w, r)
        })
    }
}

This allows you to leverage Gin's powerful routing and middleware capabilities whilst still maintaining full access to Wails features.

Custom Gin Middleware

The example also demonstrates how to create custom Gin middleware:

func LoggingMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        // Start timer
        startTime := time.Now()

        // Process request
        c.Next()

        // Calculate latency
        latency := time.Since(startTime)

        // Log request details
        log.Printf("[GIN] %s | %s | %s | %d | %s",
            c.Request.Method,
            c.Request.URL.Path,
            c.ClientIP(),
            c.Writer.Status(),
            latency,
        )
    }
}

This middleware is applied to all Gin routes and logs details about each request.

Application Configuration

The Wails application is configured to use Gin as follows:

app := application.New(application.Options{
    Name:        "Gin Example",
    Description: "A demo of using Gin with Wails",
    Mac: application.MacOptions{
        ApplicationShouldTerminateAfterLastWindowClosed: true,
    },
    Assets: application.AssetOptions{
        Handler:    ginEngine,
        Middleware: GinMiddleware(ginEngine),
    },
})

This configuration tells Wails to:

  1. Use the Gin engine as the primary handler for HTTP requests
  2. Use our custom middleware to route requests between Wails and Gin