wails/v3/examples/gin-example
Lea Anthony 3087ba0bdc
Refactor Manager API to use singular naming convention (#4367)
* Refactor Manager API to use singular naming convention

This is a RENAME-ONLY exercise that converts the Wails v3 Manager API from plural to singular naming for better consistency and clarity.

## Changes Applied

### API Transformations:
- `app.Windows.*` → `app.Window.*`
- `app.Events.*` → `app.Event.*`
- `app.ContextMenus.*` → `app.ContextMenu.*`
- `app.KeyBindings.*` → `app.KeyBinding.*`
- `app.Dialogs.*` → `app.Dialog.*`
- `app.Menus.*` → `app.Menu.*`
- `app.Screens.*` → `app.Screen.*`

### Files Updated:
- **Core Application**: 22 files in `v3/pkg/application/`
- **Examples**: 43+ files in `v3/examples/`
- **Documentation**: 13 files in `docs/src/content/docs/`
- **CLI Tests**: 1 file in `v3/internal/commands/`

### Critical Constraints Preserved:
-  Event string constants unchanged (e.g., "windows:WindowShow")
-  Platform event names preserved (events.Windows, events.Mac, etc.)
-  TypeScript API remains compatible
-  All functionality intact

### Verification:
-  All examples build successfully (`task test:examples` passes)
-  Application package compiles without errors
-  Documentation reflects new API patterns

## Benefits

- **Improved Clarity**: Singular names are more intuitive (`app.Window` vs `app.Windows`)
- **Better Consistency**: Aligns with Go naming conventions
- **Enhanced Developer Experience**: Clearer autocomplete and API discovery

🤖 Generated with [Claude Code](https://claude.ai/code)

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

* Fix generator testcases and add cross-platform test cleanup

- Update 28 generator testcase files to use singular API (app.Window.New() vs app.Windows.New())
- Add cross-platform cleanup system with Go script to remove test artifacts
- Add test:all task with comprehensive testing and automatic cleanup
- Fix cleanup to target files vs directories correctly (preserves source directories)

🤖 Generated with [Claude Code](https://claude.ai/code)

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

* Fix remaining Windows CI failures by updating all plural API usage to singular

Fixed the last remaining instances of old plural Manager API usage:
- tests/window-visibility-test/main.go: Updated all app.Windows -> app.Window and app.Menus -> app.Menu
- internal/templates/_common/main.go.tmpl: Updated app.Windows -> app.Window and app.Events -> app.Event
- pkg/services/badge/badge_windows.go: Updated app.Windows -> app.Window (Windows-specific fix)

These fixes address the Windows CI failures where platform-specific files still used the old API.
The tests didn't catch this locally because Windows-specific files only compile on Windows.

🤖 Generated with [Claude Code](https://claude.ai/code)

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-06-22 12:19:14 +10:00
..
static Update docs + examples. 2025-03-11 08:50:03 +11:00
go.mod feat: Complete App API restructuring with organized manager pattern (#4359) 2025-06-21 19:51:14 +10:00
go.sum feat: Complete App API restructuring with organized manager pattern (#4359) 2025-06-21 19:51:14 +10: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