Go JSON-RPC and Route Registration
Go JSON-RPC and Route Registration
In the GM Desktop architecture, frontend and backend communicate using the JSON-RPC 2.0 specification.
1. Basic Registration
simplejrpc-go provides a lightweight registration mechanism with no reflection overhead:
package handler
import (
"context"
"github.com/dushixiang/simplejrpc-go"
"app/internal/service"
"app/internal/transport/rpc/dto"
)
func RegisterAppHandlers(s *simplejrpc.Server, svc service.DemoService) {
// Get system information
s.RegisterFunc("app.getSystemInfo", func(ctx context.Context, req dto.EmptyReq) (*dto.SystemInfoResp, error) {
return svc.GetSystemInfo(ctx)
})
// Execute a task
s.RegisterFunc("app.executeTask", func(ctx context.Context, req dto.TaskReq) (*dto.TaskResp, error) {
return svc.ExecuteTask(ctx, req)
})
}2. Calling from the Frontend
The Vue 3 page calls the registered methods through the injected $gm client:
// Vue3 Script Setup
import { ref } from 'vue'
const info = ref(null)
const fetchInfo = async () => {
try {
const res = await window.$gm.request({
method: 'app.getSystemInfo',
params: {}
})
info.value = res.data
} catch (err) {
console.error('RPC request failed:', err)
}
}3. Standard Response Format
All business interfaces should return a consistent structure:
{
"code": 0,
"msg": "success",
"data": {
"version": "1.0.0",
"uptime": 3600
}
}