refactor: reorganize codebase into modular packages

feat(global): add global package for shared variables and types
refactor(handlers): move handlers to dedicated package and update imports
refactor(session): extract session management to separate package
refactor(config): move config handling to dedicated package
refactor(router): update route handlers to use new package structure
refactor(main): simplify main.go by moving logic to packages
This commit is contained in:
2026-04-22 12:57:04 +08:00
parent df8df78bab
commit ddf91299e7
11 changed files with 667 additions and 663 deletions
+33 -69
View File
@@ -6,49 +6,21 @@ import (
"net/http"
"os"
"os/signal"
"super-frpc/config"
"super-frpc/database"
"super-frpc/frpLogger"
"super-frpc/global"
"super-frpc/handlers"
"super-frpc/postLog"
"super-frpc/session"
"super-frpc/service"
"super-frpc/watchdog"
"syscall"
"time"
)
type SoftwareInfo struct {
Name string
Version string
Developer string
BuildVer int16
Description string
BuildType string
}
var softwareInfo SoftwareInfo = SoftwareInfo{
Name: "Super-frpc",
Version: "0.0.1",
Developer: "Madobi Nanami",
BuildVer: 1,
BuildType: "debug",
}
type StatusInfo struct {
ServerStatus string
WatchdogStatus string
}
type Is struct {
debug bool
online bool
watchdogConnected bool
}
var is Is = Is {
debug: false,
online: false,
watchdogConnected: false,
}
func main() {
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", global.Software.Name, global.Software.Version, global.Software.BuildVer, global.Software.BuildType, global.Software.Developer))
configPath := flag.String("config", "./config.json", "path to config file")
dbPath_data := flag.String("db", "./database.db", "path to database file")
dbPath_log := flag.String("log", "./logs.db", "path to logs database file")
@@ -66,32 +38,32 @@ func main() {
postLog.Info(fmt.Sprintf("Created default config file at %s", *configPath))
}
config, err := LoadConfig(*configPath)
cfg, err := config.LoadConfig(*configPath, service.GetInitSystem)
if err != nil {
postLog.Fatal(fmt.Sprintf("Failed to load config: %v", err))
}
postLog.SetDebugMode(config.Debug)
is.debug = config.Debug
postLog.SetDebugMode(cfg.Debug)
global.Is.Debug = cfg.Debug
if err := postLog.InitLogsDatabase(*dbPath_log); err != nil {
postLog.Fatal(fmt.Sprintf("Failed to initialize logs database: %v", err))
}
postLog.Info("Logs database initialized successfully")
if err := InitDatabase(*dbPath_data, *dbPath_log); err != nil {
if err := database.InitDatabase(*dbPath_data, *dbPath_log); err != nil {
postLog.Fatal(fmt.Sprintf("Failed to initialize database: %v", err))
}
postLog.Info("Database initialized successfully")
if err := InitFrpcDatabase(*dbPath_data); err != nil {
if err := database.InitFrpcDatabase(*dbPath_data); err != nil {
postLog.Fatal(fmt.Sprintf("Failed to initialize frpc database: %v", err))
}
frpLogger.SetDatabase(db, frpcDB)
frpLogger.SetDebugMode(config.Debug)
frpLogger.SetDatabase(global.ConfigDB, global.FrpcDB)
frpLogger.SetDebugMode(cfg.Debug)
_, err = GetConfig()
_, err = config.GetConfig()
if err != nil {
postLog.Fatal(fmt.Sprintf("Failed to get config: %v", err))
}
@@ -104,15 +76,15 @@ func main() {
if err != nil {
postLog.Error(fmt.Sprintf("Unable to initialize Watchdog: %s", err))
} else {
if !watchdog.Connect("127.0.0.1", config.Watchdog.Port) {
postLog.Error(fmt.Sprintf("Failed to connect to Watchdog at %s:%d", "127.0.0.1", config.Watchdog.Port))
if !watchdog.Connect("127.0.0.1", cfg.Watchdog.Port) {
postLog.Error(fmt.Sprintf("Failed to connect to Watchdog at %s:%d", "127.0.0.1", cfg.Watchdog.Port))
} else {
postLog.Info(fmt.Sprintf("Connected to Watchdog at %s:%d", "127.0.0.1", config.Watchdog.Port))
is.watchdogConnected = true
postLog.Info(fmt.Sprintf("Connected to Watchdog at %s:%d", "127.0.0.1", cfg.Watchdog.Port))
global.Is.WatchdogConnected = true
}
}
addr := fmt.Sprintf("%s:%s", config.ListenAddr, config.ListenPort)
addr := fmt.Sprintf("%s:%s", cfg.ListenAddr, cfg.ListenPort)
server := &http.Server{
Addr: addr,
ReadTimeout: 15 * time.Second,
@@ -122,7 +94,7 @@ func main() {
go func() {
postLog.Info(fmt.Sprintf("Server starting on %s", addr))
is.online = true
global.Is.Online = true
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
postLog.Fatal(fmt.Sprintf("Failed to start server: %v", err))
}
@@ -132,8 +104,8 @@ func main() {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
for range ticker.C {
CleanupExpiredTokens()
CleanupExpiredSessions()
session.CleanupExpiredTokens()
session.CleanupExpiredSessions()
}
}()
@@ -147,37 +119,29 @@ func main() {
postLog.Error(fmt.Sprintf("Server closed with error: %v", err))
}
if err := CloseDatabase(); err != nil {
if err := database.CloseDatabase(); err != nil {
postLog.Error(fmt.Sprintf("Error closing database: %v", err))
}
if err := CloseFrpcDatabase(); err != nil {
postLog.Error(fmt.Sprintf("Error closing frpc database: %v", err))
}
watchdog.Close()
postLog.Info("Server stopped")
}
func GetStatusHandler(w http.ResponseWriter, r *http.Request) {
statusInfo := StatusInfo{
ServerStatus: "Offline",
WatchdogStatus: "Offline",
statusInfo := map[string]string{
"ServerStatus": "Offline",
"WatchdogStatus": "Offline",
}
if !is.online {
statusInfo.ServerStatus = "Offline"
} else {
statusInfo.ServerStatus = "Online"
if global.Is.Online {
statusInfo["ServerStatus"] = "Online"
}
if !is.watchdogConnected {
statusInfo.WatchdogStatus = "Offline"
} else {
statusInfo.WatchdogStatus = "Online"
if global.Is.WatchdogConnected {
statusInfo["WatchdogStatus"] = "Online"
}
SendSuccessResponse(w, "getStatus", statusInfo)
handlers.SendSuccessResponse(w, "getStatus", statusInfo)
}
func GetSoftwareInfoHandler(w http.ResponseWriter, r *http.Request) {
SendSuccessResponse(w, "getSoftwareInfo", softwareInfo)
handlers.SendSuccessResponse(w, "getSoftwareInfo", global.Software)
}