refactor(global): move config to global package and consolidate handlers
- Move Config struct and related functions to global package - Consolidate handler utilities into utils package - Remove deprecated handlers and instance packages - Add new handlers package with settings and proxy functionality - Update config.json to include watchdog enabled flag
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -10,10 +11,10 @@ import (
|
||||
"super-frpc/database"
|
||||
"super-frpc/frpLogger"
|
||||
"super-frpc/global"
|
||||
"super-frpc/handlers"
|
||||
"super-frpc/postLog"
|
||||
"super-frpc/session"
|
||||
"super-frpc/service"
|
||||
"super-frpc/session"
|
||||
"super-frpc/utils"
|
||||
"super-frpc/watchdog"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -27,24 +28,23 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
|
||||
defaultConfig := `{
|
||||
"listenAddr": "0.0.0.0",
|
||||
"listenPort": "8080",
|
||||
"configDir": "./configs"
|
||||
}`
|
||||
if err := os.WriteFile(*configPath, []byte(defaultConfig), 0644); err != nil {
|
||||
// Load the default config
|
||||
configData, err := json.MarshalIndent(global.CurrentConfig, "", " ")
|
||||
if err != nil {
|
||||
postLog.Warning(fmt.Sprintf("Failed to marshal default config: %v", err))
|
||||
} else if err := os.WriteFile(*configPath, configData, 0644); err != nil {
|
||||
postLog.Warning(fmt.Sprintf("Failed to create default config file: %v", err))
|
||||
}
|
||||
postLog.Info(fmt.Sprintf("Created default config file at %s", *configPath))
|
||||
}
|
||||
|
||||
cfg, err := config.LoadConfig(*configPath, service.GetInitSystem)
|
||||
err := config.LoadConfig(*configPath, service.GetInitSystem)
|
||||
if err != nil {
|
||||
postLog.Fatal(fmt.Sprintf("Failed to load config: %v", err))
|
||||
}
|
||||
|
||||
postLog.SetDebugMode(cfg.Debug)
|
||||
global.Is.Debug = cfg.Debug
|
||||
postLog.SetDebugMode(global.CurrentConfig.Debug)
|
||||
global.Is.Debug = global.CurrentConfig.Debug
|
||||
|
||||
if err := postLog.InitLogsDatabase(*dbPath_log); err != nil {
|
||||
postLog.Fatal(fmt.Sprintf("Failed to initialize logs database: %v", err))
|
||||
@@ -61,12 +61,9 @@ func main() {
|
||||
}
|
||||
|
||||
frpLogger.SetDatabase(global.ConfigDB, global.FrpcDB)
|
||||
frpLogger.SetDebugMode(cfg.Debug)
|
||||
frpLogger.SetDebugMode(global.CurrentConfig.Debug)
|
||||
|
||||
_, err = config.GetConfig()
|
||||
if err != nil {
|
||||
postLog.Fatal(fmt.Sprintf("Failed to get config: %v", err))
|
||||
}
|
||||
config.GetConfig()
|
||||
|
||||
postLog.InitLogBroadcaster()
|
||||
|
||||
@@ -76,15 +73,15 @@ func main() {
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("Unable to initialize Watchdog: %s", err))
|
||||
} else {
|
||||
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))
|
||||
if !watchdog.Connect("127.0.0.1", global.CurrentConfig.Watchdog.Port) {
|
||||
postLog.Error(fmt.Sprintf("Failed to connect to Watchdog at %s:%d", "127.0.0.1", global.CurrentConfig.Watchdog.Port))
|
||||
} else {
|
||||
postLog.Info(fmt.Sprintf("Connected to Watchdog at %s:%d", "127.0.0.1", cfg.Watchdog.Port))
|
||||
postLog.Info(fmt.Sprintf("Connected to Watchdog at %s:%d", "127.0.0.1", global.CurrentConfig.Watchdog.Port))
|
||||
global.Is.WatchdogConnected = true
|
||||
}
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", cfg.ListenAddr, cfg.ListenPort)
|
||||
addr := fmt.Sprintf("%s:%s", global.CurrentConfig.ListenAddr, global.CurrentConfig.ListenPort)
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
@@ -139,9 +136,9 @@ func GetStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if global.Is.WatchdogConnected {
|
||||
statusInfo["WatchdogStatus"] = "Online"
|
||||
}
|
||||
handlers.SendSuccessResponse(w, "getStatus", statusInfo)
|
||||
utils.SendSuccessResponse(w, "getStatus", statusInfo)
|
||||
}
|
||||
|
||||
func GetSoftwareInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
handlers.SendSuccessResponse(w, "getSoftwareInfo", global.Software)
|
||||
utils.SendSuccessResponse(w, "getSoftwareInfo", global.Software)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user