- Introduced main.go for the server with CORS middleware and static asset handling. - Added desktop/app.go for local API server with logging and file handling capabilities. - Implemented desktop/main.go to initialize the application with Wails framework and asset management.
160 lines
3.8 KiB
Go
160 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// App struct
|
|
type App struct {
|
|
ctx context.Context
|
|
port int
|
|
debug bool
|
|
}
|
|
|
|
// NewApp creates a new App struct
|
|
func NewApp(debug bool) *App {
|
|
return &App{
|
|
debug: debug,
|
|
}
|
|
}
|
|
|
|
func (a *App) logDebug(format string, args ...any) {
|
|
if a != nil && a.debug {
|
|
fmt.Printf("[debug] "+format+"\n", args...)
|
|
}
|
|
}
|
|
|
|
// logHandler wraps HTTP handlers to log requests when debug is enabled.
|
|
func (a *App) logHandler(next http.Handler) http.Handler {
|
|
if !a.debug {
|
|
return next
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
next.ServeHTTP(w, r)
|
|
fmt.Printf("[debug] http %s %s %dms\n", r.Method, r.URL.Path, time.Since(start).Milliseconds())
|
|
})
|
|
}
|
|
|
|
// startup is called when the app starts. The context is saved
|
|
// so we can call the runtime methods
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
a.logDebug("startup begin")
|
|
|
|
// Start local API server on a random port
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
fmt.Printf("Error starting local server: %v\n", err)
|
|
return
|
|
}
|
|
|
|
a.port = listener.Addr().(*net.TCPAddr).Port
|
|
a.logDebug("local API listener bound on %s", listener.Addr().String())
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
// CORS middleware for local desktop API
|
|
cors := func(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
mux.HandleFunc("/api/ping", cors(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprintf(w, `{"status":"ok"}`)
|
|
}))
|
|
|
|
server := &http.Server{
|
|
Addr: listener.Addr().String(),
|
|
Handler: a.logHandler(mux),
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 15 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
go func() {
|
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
fmt.Printf("Error serving desktop API: %v\n", err)
|
|
}
|
|
}()
|
|
fmt.Printf("Desktop API server started on port %d\n", a.port)
|
|
a.logDebug("startup complete")
|
|
}
|
|
|
|
// GetAPIPort returns the port the local server is running on
|
|
func (a *App) GetAPIPort() int {
|
|
a.logDebug("GetAPIPort -> %d", a.port)
|
|
return a.port
|
|
}
|
|
|
|
// LogFrontend allows the frontend to log to the terminal
|
|
func (a *App) LogFrontend(message string) {
|
|
fmt.Printf("[frontend] %s\n", message)
|
|
}
|
|
|
|
// SaveFile shows a save dialog and writes the content to the selected file
|
|
func (a *App) SaveFile(filename string, content string) error {
|
|
a.logDebug("SaveFile filename=%s", filename)
|
|
filePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
|
DefaultFilename: filename,
|
|
Title: "Save Graph",
|
|
Filters: []runtime.FileFilter{
|
|
{
|
|
DisplayName: "JSON Files (*.json)",
|
|
Pattern: "*.json",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if filePath == "" {
|
|
return nil // Cancelled
|
|
}
|
|
|
|
return os.WriteFile(filePath, []byte(content), 0644)
|
|
}
|
|
|
|
// LoadFile shows an open dialog and returns the content of the selected file
|
|
func (a *App) LoadFile() (string, error) {
|
|
a.logDebug("LoadFile")
|
|
filePath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
|
Title: "Open Graph",
|
|
Filters: []runtime.FileFilter{
|
|
{
|
|
DisplayName: "JSON Files (*.json)",
|
|
Pattern: "*.json",
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if filePath == "" {
|
|
return "", nil // Cancelled
|
|
}
|
|
|
|
content, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(content), nil
|
|
}
|
|
|