87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"git.pyer.club/kingecg/goaidb/config"
|
|
"git.pyer.club/kingecg/goaidb/log"
|
|
"git.pyer.club/kingecg/goaidb/network"
|
|
"git.pyer.club/kingecg/goaidb/storage"
|
|
)
|
|
|
|
func getExeDir() string {
|
|
exePath, err := os.Executable()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
exeDir := filepath.Dir(exePath)
|
|
return exeDir
|
|
}
|
|
|
|
// 主程序入口
|
|
func main() {
|
|
// 加载配置文件(优先级:命令行参数 > 环境变量 > 默认配置)
|
|
configPath := getExeDir() + "/config.yaml"
|
|
if len(os.Args) > 1 {
|
|
configPath = os.Args[1]
|
|
} else if envPath := os.Getenv("GOAIDB_CONFIG"); envPath != "" {
|
|
configPath = envPath
|
|
}
|
|
|
|
// 解析配置文件
|
|
cfg, err := config.ParseConfig(configPath)
|
|
if err != nil {
|
|
// fmt.Fprintf(os.Stderr, "配置加载失败: %v\n", err)
|
|
log.Error("配置加载失败", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 设置全局配置
|
|
config.SetGlobalConfig(cfg)
|
|
|
|
// 启动配置文件监视
|
|
err = config.WatchConfig(configPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "配置监控启动失败: %v\n", err)
|
|
log.Error("配置监控启动失败", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 设置信号处理(优雅关闭)
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-sigChan
|
|
log.Info("正在关闭...")
|
|
config.CloseWatcher()
|
|
os.Exit(0)
|
|
}()
|
|
|
|
// 初始化存储引擎(默认使用内存引擎)
|
|
storageEngine, err := storage.NewMemoryEngine()
|
|
if err != nil {
|
|
log.Error("Failed to initialize storage engine: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// 创建网络服务器
|
|
server := network.NewServer(storageEngine)
|
|
conf := config.GetConfig()
|
|
// 启动服务
|
|
listener, err := net.Listen("tcp", conf.Server.Host+":"+fmt.Sprintf("%d", conf.Server.Port))
|
|
if err != nil {
|
|
log.Error("Failed to start server: %v\n", err)
|
|
return
|
|
}
|
|
|
|
log.Info("GoAIDB started on port", conf.Server.Port)
|
|
server.Serve(listener)
|
|
}
|