Go Backend SDK Architecture and Setup
Go Backend SDK Architecture and Setup
Architecture
The new GM Desktop fully recommends the Go template (for example c8rv-master) as the backend for micro-apps.
The backend runs as an independent long-lived process, connects to the GM Desktop main process over a Unix Domain Socket, and exposes services using the JSON-RPC 2.0 protocol.
1. How It Works
+-----------------------------+
| GM Desktop frontend (Vue3 / $gm) |
+-----------------------------+
| (internal bridge)
v
+-----------------------------+
| GM Desktop core main process |
+-----------------------------+
| (Unix Domain Socket)
v
+-----------------------------+
| Go backend process (simplejrpc-go) |
| c8rv-master architecture |
+-----------------------------+- Startup and handshake: GM Desktop launches the micro-app backend binary and passes it the socket path or listen parameters.
- Ping keepalive: the main process calls
pingat a fixed interval; the backend returns"pong"so the connection stays healthy. - Method dispatch: the frontend calls
$gm.request; the main process sends the JSON-RPC request over the Unix socket; the Go backend router dispatches it to the matchingHandlerand returns the response.
2. Directory Layout (c8rv-master Template)
backend/
├── cmd/
│ └── main.go # entry point, Unix socket listener, route wiring
├── internal/
│ ├── config/ # config structs and parsing (YAML / ENV)
│ ├── i18n/ # internationalization (zh_CN, en_US)
│ ├── server/ # socket listener and SimpleJRPC bootstrap
│ ├── model/ # data models
│ ├── service/ # business interfaces and implementations
│ └── transport/
│ ├── rpc/ # JSON-RPC request/response DTOs and handlers
│ │ ├── dto/ # validation rules and input definitions
│ │ └── handler/ # JSON-RPC route and method bindings
│ └── adapter/ # protocol adaptation and error mapping
├── conf/
│ └── config.yaml # default runtime configuration
├── go.mod # dependency management
└── Makefile # multi-platform static and cross-compilation scripts3. main.go Entry Point
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/dushixiang/simplejrpc-go"
"app/internal/config"
"app/internal/service"
"app/internal/transport/rpc/handler"
)
var (
socketPath = flag.String("socket", "/tmp/gmssh-app.sock", "Unix domain socket path")
confPath = flag.String("conf", "./conf/config.yaml", "Path to config file")
)
func main() {
flag.Parse()
// 1. Load application configuration
cfg, err := config.Load(*confPath)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
// 2. Initialize core business services
demoSvc := service.NewDemoService(cfg)
// 3. Register JSON-RPC routes and handlers
s := simplejrpc.NewServer()
// Required: GM Desktop heartbeat
s.RegisterFunc("ping", func(ctx context.Context) (string, error) {
return "pong", nil
})
// Inject business handlers
handler.RegisterAppHandlers(s, demoSvc)
// 4. Listen on the Unix Domain Socket and handle graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
log.Println("shutting down backend server...")
_ = os.Remove(*socketPath)
os.Exit(0)
}()
log.Printf("backend listening on unix://%s\n", *socketPath)
if err := s.ListenUnix(*socketPath); err != nil {
log.Fatalf("server exited: %v", err)
}
}4. Development and Debugging
# 1. Enter the backend project
cd backend
# 2. Check dependencies
go mod tidy
# 3. Build locally
make build
# Output binary: build/backend
# 4. Run manually for testing (with a debug socket)
./build/backend -socket /tmp/test-app.sockNext steps:
