Skip to content

Commit c1202e5

Browse files
author
w7years
committed
🚀 feat(config): update server port handling and entrypoint to accept dynamic port configuration
🛠️ fix(main): change server address to use dynamic port from command line argument 🔧 chore(config): remove deprecated server_port configuration from struct and set default values
1 parent f54cbbc commit c1202e5

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

Diff for: cmd/proxy/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
)
2424

2525
func main() {
26-
configPath := flag.String("config", "config.yaml", "配置文件路径")
26+
configPath := flag.String("config", "config.yaml", "config file path")
27+
serverPort := flag.Int("port", 3002, "server port")
28+
2729
flag.Parse()
2830

2931
// Load config
@@ -54,7 +56,7 @@ func main() {
5456
router := gin.New()
5557
// Add health check route
5658
router.GET("/generate_204", func(c *gin.Context) {
57-
logger.Logger.Debugln("generate_204 success.")
59+
logger.Logger.Infoln("generate_204 success.")
5860
c.Status(http.StatusNoContent)
5961
})
6062

@@ -84,7 +86,7 @@ func main() {
8486

8587
// Set up server
8688
srv := &http.Server{
87-
Addr: ":" + cfg.ServerPort,
89+
Addr: fmt.Sprintf(":%d", *serverPort),
8890
Handler: router,
8991
ReadTimeout: 90 * time.Second,
9092
WriteTimeout: 90 * time.Second,
@@ -103,7 +105,7 @@ func main() {
103105
logger.Logger.Fatalf("Failed to start server: %v", err)
104106
errChan <- err
105107
} else {
106-
logger.Logger.Infof("Server started successfully: %s", cfg.ServerPort)
108+
logger.Logger.Infof("Server started successfully: %d", *serverPort)
107109
}
108110
}()
109111

Diff for: config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
server_port: "3002"
21
rate_limit: "100-M"
32
fixed_request_ip: ""
43
max_request_body_size_mb: 100

Diff for: entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/sh
2-
exec ./proxy -config "$CONFIG_PATH"
2+
exec ./proxy -config "${CONFIG_PATH}" -port "${PORT}"

Diff for: internal/config/config.go

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
// Config config struct
1010
type Config struct {
11-
ServerPort string `mapstructure:"server_port"`
1211
RateLimit string `mapstructure:"rate_limit"`
1312
MaxRequestBodySizeMB int `mapstructure:"max_request_body_size_mb"`
1413
FixedRequestIP string `mapstructure:"fixed_request_ip"`
@@ -24,7 +23,6 @@ func LoadConfig(configPath string) (*Config, error) {
2423
viper.SetConfigFile(configPath)
2524

2625
// Set default values
27-
viper.SetDefault("server_port", "3001")
2826
viper.SetDefault("rate_limit", "100-M")
2927
viper.SetDefault("max_request_body_size_mb", "100")
3028
viper.SetDefault("log_dir", "logs")

Diff for: internal/middleware/middleware.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func APIKeyAuthMiddleware() gin.HandlerFunc {
1616
apiKey = c.GetHeader("x-api-key")
1717
}
1818
if apiKey == "" {
19-
logger.Logger.Warn("未授权的访问请求")
20-
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未授权"})
19+
logger.Logger.Error("Unauthorized access request")
20+
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
2121
return
2222
}
2323
c.Next()
@@ -48,8 +48,8 @@ func LimitRequestBody(maxBytes int64) gin.HandlerFunc {
4848
return func(c *gin.Context) {
4949
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
5050
if err := c.Request.ParseForm(); err != nil {
51-
logger.Logger.Warn("请求体过大")
52-
c.AbortWithStatusJSON(http.StatusRequestEntityTooLarge, gin.H{"error": "请求体过大"})
51+
logger.Logger.Error("Request body too large")
52+
c.AbortWithStatusJSON(http.StatusRequestEntityTooLarge, gin.H{"error": "Request body too large"})
5353
return
5454
}
5555
c.Next()

0 commit comments

Comments
 (0)