-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (117 loc) · 3.79 KB
/
main.go
File metadata and controls
145 lines (117 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"EZ-Encrypt-Middleware/config"
"EZ-Encrypt-Middleware/proxy"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
// Load configuration
config.LoadConfig()
// Set Gin mode
if config.AppConfig.DebugMode != "true" {
gin.SetMode(gin.ReleaseMode)
}
// Create Gin engine
r := gin.Default()
// Check if path prefix is configured
pathPrefix := config.AppConfig.PathPrefix
if pathPrefix != "" {
// If path prefix is configured, only handle paths that match the prefix
r.NoRoute(func(c *gin.Context) {
requestPath := c.Request.URL.Path
// Check if request path starts with the configured prefix
if len(requestPath) >= len(pathPrefix) && requestPath[:len(pathPrefix)] == pathPrefix {
// Remove prefix from path for further processing
c.Request.URL.Path = requestPath[len(pathPrefix):]
if config.AppConfig.IsPaymentNotifyPath(c.Request.URL.Path) {
handlePaymentNotify(c)
return
}
proxy.ProxyHandler(c)
return
}
// If path doesn't match prefix, return 404
c.JSON(http.StatusNotFound, gin.H{"error": "路径未找到"})
})
} else {
// If no prefix is configured, keep the original behavior
r.NoRoute(func(c *gin.Context) {
if config.AppConfig.IsPaymentNotifyPath(c.Request.URL.Path) {
handlePaymentNotify(c)
return
}
proxy.ProxyHandler(c)
})
}
corsConfig := cors.Config{}
if config.AppConfig.CORSOrigin == "*" {
corsConfig.AllowAllOrigins = true
} else {
corsConfig.AllowOrigins = config.AppConfig.GetAllowedOrigins()
}
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Accept", "Authorization", "x-iv"}
corsConfig.AllowCredentials = true
r.Use(cors.New(corsConfig))
timeout := 30 * time.Second
if config.AppConfig.RequestTimeout != "" {
if t, err := time.ParseDuration(config.AppConfig.RequestTimeout + "ms"); err == nil {
timeout = t
}
}
if config.AppConfig.EnableLogging == "true" {
r.Use(func(c *gin.Context) {
log.Printf("请求: %s %s", c.Request.Method, c.Request.URL.Path)
c.Next()
status := c.Writer.Status()
if status >= 400 {
log.Printf("错误请求: %s %s Status: %d", c.Request.Method, c.Request.URL.Path, status)
} else {
log.Printf("响应: %s %s Status: %d", c.Request.Method, c.Request.URL.Path, status)
}
})
}
port := config.AppConfig.Port
if port == "" {
port = "3000"
}
log.Printf("服务器运行在端口 %s", port)
log.Printf("请求超时设置: %v", timeout)
log.Printf("CORS配置 - 允许所有来源: %t", corsConfig.AllowAllOrigins)
if !corsConfig.AllowAllOrigins && len(corsConfig.AllowOrigins) > 0 {
log.Printf("允许的来源: %v", corsConfig.AllowOrigins)
}
log.Printf("调试模式: %s", config.AppConfig.DebugMode)
log.Printf("日志记录: %s", config.AppConfig.EnableLogging)
if len(config.AppConfig.GetAllowedPaymentNotifyPaths()) > 0 {
log.Printf("支付回调路径: %v", config.AppConfig.GetAllowedPaymentNotifyPaths())
}
if config.AppConfig.PathPrefix != "" {
log.Printf("路径前缀: %s", config.AppConfig.PathPrefix)
}
r.Run(":" + port)
}
func handlePaymentNotify(c *gin.Context) {
backendURL := config.AppConfig.BackendAPIURL
targetURL := backendURL + c.Request.URL.Path
target, err := url.Parse(targetURL)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "无效的目标URL"})
return
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Director = func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = target.Path
req.URL.RawQuery = target.RawQuery
req.Host = target.Host
}
proxy.ServeHTTP(c.Writer, c.Request)
}