mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
25w27a
This commit is contained in:
parent
dcc50401c4
commit
8a50b311fc
7 changed files with 63 additions and 14 deletions
47
main.go
47
main.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"ghproxy/api"
|
||||
|
|
@ -23,6 +24,7 @@ import (
|
|||
"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
"github.com/cloudwego/hertz/pkg/common/adaptor"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
|
||||
"github.com/hertz-contrib/http2/factory"
|
||||
)
|
||||
|
|
@ -31,6 +33,7 @@ var (
|
|||
cfg *config.Config
|
||||
r *server.Hertz
|
||||
configfile = "/data/ghproxy/config/config.toml"
|
||||
hertZfile *os.File
|
||||
cfgfile string
|
||||
version string
|
||||
runMode string
|
||||
|
|
@ -129,7 +132,29 @@ func setupLogger(cfg *config.Config) {
|
|||
fmt.Printf("Log Level: %s\n", cfg.Log.Level)
|
||||
logDebug("Config File Path: ", cfgfile)
|
||||
logDebug("Loaded config: %v\n", cfg)
|
||||
logInfo("Init Completed")
|
||||
logInfo("Logger Initialized Successfully")
|
||||
}
|
||||
|
||||
func setupHertZLogger(cfg *config.Config) {
|
||||
var err error
|
||||
|
||||
if cfg.Log.HertZLogPath != "" {
|
||||
hertZfile, err = os.OpenFile(cfg.Log.HertZLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to open log file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
hlog.SetOutput(hertZfile)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func setMemLimit(cfg *config.Config) {
|
||||
if cfg.Server.MemLimit > 0 {
|
||||
debug.SetMemoryLimit((cfg.Server.MemLimit) * 1024 * 1024)
|
||||
logInfo("Set Memory Limit to %d MB", cfg.Server.MemLimit)
|
||||
}
|
||||
}
|
||||
|
||||
func loadlist(cfg *config.Config) {
|
||||
|
|
@ -315,7 +340,9 @@ func init() {
|
|||
loadConfig()
|
||||
if cfg != nil { // 在setupLogger前添加空值检查
|
||||
setupLogger(cfg)
|
||||
setupHertZLogger(cfg)
|
||||
InitReq(cfg)
|
||||
setMemLimit(cfg)
|
||||
loadlist(cfg)
|
||||
setupRateLimit(cfg)
|
||||
|
||||
|
|
@ -346,12 +373,17 @@ func main() {
|
|||
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||
|
||||
r := server.New(
|
||||
server.WithHostPorts(addr),
|
||||
server.WithH2C(true),
|
||||
)
|
||||
|
||||
r.AddProtocol("h2", factory.NewServerFactory())
|
||||
if cfg.Server.H2C {
|
||||
r = server.New(
|
||||
server.WithHostPorts(addr),
|
||||
server.WithH2C(true),
|
||||
)
|
||||
r.AddProtocol("h2", factory.NewServerFactory())
|
||||
} else {
|
||||
r = server.New(
|
||||
server.WithHostPorts(addr),
|
||||
)
|
||||
}
|
||||
|
||||
// 添加Recovery中间件
|
||||
r.Use(recovery.Recovery())
|
||||
|
|
@ -414,5 +446,6 @@ func main() {
|
|||
|
||||
r.Spin()
|
||||
defer logger.Close()
|
||||
defer hertZfile.Close()
|
||||
fmt.Println("Program Exit")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue