wails/v3/examples/gin-example/main.go
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

116 lines
2.7 KiB
Go

package main
import (
"embed"
"log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed static
var staticFiles embed.FS
// GinMiddleware creates a middleware that passes requests to Gin if they're not handled by Wails
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 the `/wails` route
if strings.HasPrefix(r.URL.Path, "/wails") {
next.ServeHTTP(w, r)
return
}
// Let Gin handle everything else
ginEngine.ServeHTTP(w, r)
})
}
}
// LoggingMiddleware is a Gin middleware that logs request details
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,
)
}
}
func main() {
// Create a new Gin router
ginEngine := gin.New() // Using New() instead of Default() to add our own middleware
// Add middlewares
ginEngine.Use(gin.Recovery())
ginEngine.Use(LoggingMiddleware())
// Serve embedded static files
ginEngine.StaticFS("/static", http.FS(staticFiles))
// Define routes
ginEngine.GET("/", func(c *gin.Context) {
file, err := staticFiles.ReadFile("static/index.html")
if err != nil {
c.String(http.StatusInternalServerError, "Error reading index.html")
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", file)
})
ginEngine.GET("/api/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello from Gin API!",
"time": time.Now().Format(time.RFC3339),
})
})
// Create a new Wails application
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),
},
})
// Register event handler and store cleanup function
removeGinHandler := app.Event.On("gin-button-clicked", func(event *application.CustomEvent) {
log.Printf("Received event from frontend: %v", event.Data)
})
// Note: In production, call removeGinHandler() during cleanup
_ = removeGinHandler
// Create window
app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "Wails + Gin Example",
Width: 900,
Height: 700,
URL: "/",
})
// Run the app
err := app.Run()
if err != nil {
log.Fatal(err)
}
}