Inspired by HNKServer/honoka-chan-apk-server, but implemented as a JNI-based embedded runtime instead of the original approach. - Add an Android Compose controller app that starts and stops honoka-chan through JNI, shows runtime and health status, manages data directory mounting, and supports backup import/export for data.db - Add an embeddable Go server entrypoint plus JNI-exported status, health, and reload hooks so desktop and Android builds share the same startup and shutdown path - Add Android build scripts, runtime packaging, and project documentation, including generated default config.json content for honoka_runtime.zip - Add runtime bundle hash tracking so updated honoka_runtime.zip assets are automatically redeployed while preserving config.json and user data files - Add in-app service settings for unlock_all_special_rotation with immediate config persistence and automatic reload when the service is running - Split SQLite driver selection by platform, using go-sqlite3 on Android and modernc.org/sqlite elsewhere to avoid Android x86_64 seccomp crashes - Update startup, database initialization, and system health/reload handlers to support the embedded runtime and Android control flow Signed-off-by: Sean Du <do4suki@gmail.com>
101 lines
1.7 KiB
Go
101 lines
1.7 KiB
Go
package db
|
|
|
|
import (
|
|
"sync"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
var (
|
|
MainDb = "assets/main.db"
|
|
UserDb = "assets/data.db"
|
|
MainEng *xorm.Engine
|
|
UserEng *xorm.Engine
|
|
|
|
mu sync.Mutex
|
|
)
|
|
|
|
func Init(mainDbPath, userDbPath string) error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
if mainDbPath == "" {
|
|
mainDbPath = MainDb
|
|
}
|
|
if userDbPath == "" {
|
|
userDbPath = UserDb
|
|
}
|
|
|
|
if MainEng != nil || UserEng != nil {
|
|
if MainDb == mainDbPath && UserDb == userDbPath {
|
|
return nil
|
|
}
|
|
if err := closeLocked(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
mainEng, err := xorm.NewEngine(sqliteDriverName, mainSQLiteDSN(mainDbPath))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := prepareMainSQLiteEngine(mainEng); err != nil {
|
|
mainEng.Close()
|
|
return err
|
|
}
|
|
if err := mainEng.Ping(); err != nil {
|
|
mainEng.Close()
|
|
return err
|
|
}
|
|
mainEng.SetMaxOpenConns(10)
|
|
mainEng.SetMaxIdleConns(5)
|
|
|
|
userEng, err := xorm.NewEngine(sqliteDriverName, userSQLiteDSN(userDbPath))
|
|
if err != nil {
|
|
mainEng.Close()
|
|
return err
|
|
}
|
|
if err := prepareUserSQLiteEngine(userEng); err != nil {
|
|
mainEng.Close()
|
|
userEng.Close()
|
|
return err
|
|
}
|
|
if err := userEng.Ping(); err != nil {
|
|
mainEng.Close()
|
|
userEng.Close()
|
|
return err
|
|
}
|
|
userEng.SetMaxOpenConns(10)
|
|
userEng.SetMaxIdleConns(5)
|
|
|
|
MainDb = mainDbPath
|
|
UserDb = userDbPath
|
|
MainEng = mainEng
|
|
UserEng = userEng
|
|
return nil
|
|
}
|
|
|
|
func Close() error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return closeLocked()
|
|
}
|
|
|
|
func closeLocked() error {
|
|
var firstErr error
|
|
|
|
if MainEng != nil {
|
|
if err := MainEng.Close(); err != nil && firstErr == nil {
|
|
firstErr = err
|
|
}
|
|
MainEng = nil
|
|
}
|
|
if UserEng != nil {
|
|
if err := UserEng.Close(); err != nil && firstErr == nil {
|
|
firstErr = err
|
|
}
|
|
UserEng = nil
|
|
}
|
|
|
|
return firstErr
|
|
}
|