Debugging the Backend
Debugging the Backend
This page explains how to start a Go backend locally, configure the Unix Socket, and run frontend/backend integration tests inside GM Desktop.
Debugging goal
The backend process listens for JSON-RPC 2.0 requests on a Unix Socket. GM Desktop discovers the Socket, runs health checks and forwards the frontend's unified requests to the backend. The backend does not listen on an HTTP port itself.
Before you start
Check the following before you begin:
- Your Go version meets the project requirements (the current project requires Go 1.24+);
- The backend directory contains
main.go,config.jsonandi18n/; - The debug app's service type is Socket (Unix Domain Socket);
- The debug app points at the absolute path of the actual Socket file, not at the backend directory.
Start the backend service
1. Go to the backend directory
You must start the service from the backend directory, because the configuration file and the language files are loaded through relative paths:
cd /path/to/your-app/backend2. Install dependencies (first run only)
make depsThis command tidies and downloads the Go module dependencies. If they are already in place, skip to the next step.
3. Start the service
make runmake run creates the tmp/ and logs/ directories and then runs go run ..
When it runs locally, the service listens on:
/path/to/your-app/backend/tmp/app.sockThe startup log should contain something like:
JSON-RPC server listening on Unix socket: /path/to/your-app/backend/tmp/app.sockWorking directory
Do not run go run backend from the project root. Go into the backend directory first, otherwise config.json, i18n/ and the log directory may not load as expected.
Configure the debug app
In the debug app settings in DevTools, set the Socket file path to the absolute path the backend actually creates:
/path/to/your-app/backend/tmp/app.sockKeep the following in mind:
- The path must point at the
app.sockfile; - That file only exists after the backend process has started;
- The organization name and app name in the debug app must match
OrgName,AppandAppNamein the backendinternal/common/constant.go; - The frontend page address is a separate setting. Do not put the frontend address in the Socket path.
Health checks
You must register ping
The platform calls the ping method every 5 seconds. The backend must register that method and return only pong:
func (s *Server) RegisterHandles(ds interface {
RegisterHandle(name string, handler func(*gsock.Request) (any, error), middlewares ...gsock.RPCMiddleware)
}) {
// Platform health check; cannot be deleted or changed
ds.RegisterHandle("ping", s.Ping)
// Business endpoints
ds.RegisterHandle("hello", s.Hello)
}
func (s *Server) Ping(req *gsock.Request) (any, error) {
return "pong", nil
}Here pong is the business result of the RPC method. You do not assemble {code, data, msg} by hand in the handler; the RPC service takes care of the response wrapper.
Do not add business logic to ping
Health checks have to return quickly. Do not run database queries, file scans or other slow work in ping, and do not change the method name or the return value.
When health checks fail
Work through these checks in order:
- Is the backend process still running;
- Does the Socket path in the debug app match the path in the log exactly;
- Is
pingregistered, and is the return value exactly"pong"; - Is the working directory
backend, and areconfig.jsonandi18n/readable.
Debugging business RPC
Register methods
Every business method has to be registered as a route first. Route names use camelCase and become the last segment of the unified access path:
ds.RegisterHandle("hello", s.Hello)
ds.RegisterHandle("getItemList", s.GetItemList)For example, the unified entry point for the hello method is:
/api/call/{org}/{app}/helloParse the parameters
The handler parses the parameters and calls the Service; it does not hold the core business logic:
func (s *Server) Hello(req *gsock.Request) (any, error) {
var args dto.HelloReq
if err := rpcutil.ParseParams(req, &args); err != nil {
return nil, err
}
if args.Name == "" {
return nil, fmt.Errorf("%s", gi18n.Instance().T("NameRequired"))
}
return exampleService().Hello(args)
}rpcutil.ParseParams reads params from the request, parses the JSON fields into the DTO, and sets the i18n language from params.lang. DTO fields must carry the correct json tags.
Handle the request
The frontend calls the backend through the platform's unified entry point:
window.$gm.request('/api/call/official/example/hello', {
method: 'post',
data: {
version: window.$gm.version,
transport: window.$gm.communicationType,
params: {
lang: 'zh-CN',
name: 'world',
},
},
}).then((res) => {
console.log(res.data.data) // hello world
})An app with a backend service should complete $gm.init() before calling business endpoints.
Inspect the response
The RPC service wraps the handler return value the same way every time:
{
"code": 200,
"data": "hello world",
"msg": "OK",
"meta": {
"endpoint": "hello",
"close": 0
}
}That is the inner RPC response returned by the app backend over the Unix Socket. When you go through /api/call/{org}/{app}/{method}, the platform also returns an outer status code 200000 and puts the inner response in the outer data:
{
"code": 200000,
"data": {
"code": 200,
"data": "hello world",
"msg": "OK",
"meta": {
"endpoint": "hello",
"close": 0
}
},
"meta": {
"endpoint": "/api/call/*",
"close": 0
},
"msg": "Successful operation"
}The platform SDK usually exposes the unified entry point result as res.data of $gm.request(), so the business result is normally in res.data.data:
// res.data is the inner RPC response shown above
if (res.data.code === 200) {
console.log(res.data.data)
}If you check the raw response of the unified entry point instead, check the outer code === 200000 first, then the inner data.code === 200. Do not mix the status codes of the two layers.
When a handler returns an error, the code of the inner RPC response becomes something other than 200 and the error message is in msg. The outer code only says whether the platform handled the unified entry point request successfully, so judge it separately from the inner business status. Unknown methods are reported by the RPC service as a missing route.
Logs and breakpoints
Console logs
The debug environment uses the test configuration in config.json, so logs go to the console and are also written to backend/logs/:
tail -f logs/$(date +%F).logLogs, the configuration file and the language files are all resolved relative to the working directory, so always run make run in the backend directory.
Breakpoint debugging
Set breakpoints in internal/handler/rpc/ or internal/service/, then start main.go as a Go program from your IDE and keep these conditions:
- The working directory is
backend; - The Socket path the program creates matches the DevTools configuration;
config.jsonandi18n/are in the working directory.
FAQ
| Symptom | Likely cause | Fix |
|---|---|---|
| The app shows the backend as offline | The process is not running, or the Socket path is wrong. | Run make run in the backend directory and set the DevTools path to the absolute path shown in the log. |
| Health checks fail | ping is not registered, its return value was changed, or it takes too long. | Confirm that ping is registered and let the handler return "pong" directly. |
| The endpoint reports invalid parameters | The request has no params, or field names do not match the DTO json tags. | Check the request body layers and confirm the business fields are inside the params object. |
| The configuration or language files fail to load | The working directory is not backend. | Stop the current process, go into backend and run make run again. |
| The frontend page opens but endpoint calls fail | $gm.init() did not complete, or the app identifier in the URL does not match the backend constants. | Wait for initialization to finish and check /api/call/{org}/{app}/{method} against AppName. |
| The method is reported as missing | The method name in the URL was not registered through RegisterHandle. | Check the case and camelCase spelling of the method name, then restart the backend. |
Stopping the service
Press Ctrl+C in the terminal that runs make run. The service receives the exit signal and cleans up the Socket connection; the next start uses tmp/app.sock again.
