Introduction
eapp is the application lifecycle management module of ee-go, providing process signal listening, graceful exit, event registration, and package.json reading capabilities. It listens for system signals (SIGINT, SIGTERM, SIGQUIT), triggers the exit flow, and supports executing user-registered hook functions before exit (such as closing database connections).
Import
import "github.com/wallace5303/ee-go/eapp"API
Run()
Description: Start the program and listen for system signals. When a SIGINT, SIGTERM, or SIGQUIT signal is received, it automatically calls Close() to execute the graceful exit flow.
Parameters: None
Returns: None
Example:
package main
import (
"github.com/wallace5303/ee-go/eboot"
)
func main() {
ego := eboot.New(staticFS)
ego.Run() // Internally calls eapp.Run(), blocks waiting for signals
}See also: Close()
Close()
Description: Execute graceful process exit. Sets the exit flag, triggers registered beforeClose event hooks, waits 500ms, then restores the flag and calls os.Exit(0) to exit the process. This method uses a mutex lock to prevent concurrent calls.
Parameters: None
Returns: None
Example:
eapp.Register("beforeClose", func() {
// Clean up resources before exit
sqlitelib.CloseDatabase()
})
// Manually trigger exit (normally called automatically by signal listener)
eapp.Close()See also: Register()
ReadPackage()
Description: Read the contents of Electron's package.json file. Selects a different reading strategy based on the runtime environment: in development mode, it prioritizes reading from the project directory; in production mode, it decides whether to read from the embedded filesystem or an external file based on static resource configuration. If the file does not exist, it throws an exception and exits.
Parameters: None
Returns: map[string]any — the full contents of package.json, including name, version, and other fields
Example:
pkg := eapp.ReadPackage()
appName := pkg["name"].(string)
appVersion := pkg["version"].(string)
fmt.Printf("App: %s v%s\n", appName, appVersion)See also: econfig.GetStatic(), estatic.ReadJsonStrict()
Register(eventName, handler, args...)
Description: Register a lifecycle event hook function. Currently only supports the beforeClose event. Registered functions are called when the process exits and can be used to release resources (close databases, stop background tasks, etc.). If an unsupported event name is registered, a warning log is output and the registration is ignored.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| eventName | string | Yes | Event name, currently only supports "beforeClose" |
| handler | interface{} | Yes | Hook function, parameter types must match args |
| args | ...interface{} | No | Arguments passed to handler, can be empty |
Returns: None
Example:
// Register without arguments
eapp.Register("beforeClose", func() {
sqlitelib.CloseDatabase()
fmt.Println("Database closed")
})
// Register with arguments
eapp.Register("beforeClose", func(msg string) {
fmt.Println("Exiting:", msg)
}, "goodbye")See also: Close(), Staff
Staff
Description: Internal data structure for event hooks, storing the registered event name, handler function in reflection value form, and its arguments.
| Field | Type | Description |
|---|---|---|
| Name | string | Event name |
| Handler | reflect.Value | Reflection value of the handler function |
| Args | []interface{} | Argument list passed to the handler function |
Events
Description: List of supported event names, currently contains "beforeClose".
Type: []string
BeforeCloseFlag
Description: Constant identifier for the beforeClose event, value is "beforeClose".
Type: string
