81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
// Package forward 内网双通道透明转发(监管 JSON + 处方 PDF),支持真实转发与回显模式。
|
||
package forward
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
"net/http"
|
||
|
||
"xk-hy-forward-go/internal/forward/testweb"
|
||
)
|
||
|
||
// Run 启动 forward 全部 HTTP 服务:/health、/province/supervise/data、/mng/file/auth/upload。
|
||
func Run() {
|
||
// 步骤 1:从 CWD / 程序目录加载 .env(不覆盖已有 OS 环境变量)
|
||
LoadEnvFiles()
|
||
// 步骤 2:初始化按日滚动的应用日志
|
||
if err := initAppLog(); err != nil {
|
||
log.Printf("应用日志初始化失败: %v", err)
|
||
}
|
||
if err := initAPILog(); err != nil {
|
||
log.Printf("api-logs 目录初始化失败: %v", err)
|
||
}
|
||
|
||
// 步骤 3:读取监听地址、访问控制、是否真实转发
|
||
listen := env("LISTEN_ADDR", ":16001")
|
||
allowIPs := env("ALLOW_IPS", "")
|
||
forwardSecret := env("FORWARD_SHARED_SECRET", "")
|
||
enableForward := envBool("ENABLE_FORWARD", true)
|
||
|
||
// 步骤 4:解析两条通道的上游 URL
|
||
superviseTarget := env("SUPERVISE_TARGET_URL", "")
|
||
if superviseTarget == "" {
|
||
superviseTarget = env("TARGET_URL", DefaultSuperviseTarget)
|
||
}
|
||
fileTarget := env("FILE_TARGET_URL", DefaultFileTarget)
|
||
|
||
// 步骤 5:为两条通道各创建一个 ReverseProxy(回显模式时不会调用 ServeHTTP,但 URL 校验仍在此完成)
|
||
superviseProxy, err := newReverseProxy(superviseTarget, "supervise")
|
||
if err != nil {
|
||
log.Fatalf("SUPERVISE_TARGET_URL 无效: %v", err)
|
||
}
|
||
fileProxy, err := newReverseProxy(fileTarget, "file")
|
||
if err != nil {
|
||
log.Fatalf("FILE_TARGET_URL 无效: %v", err)
|
||
}
|
||
|
||
mux := http.NewServeMux()
|
||
|
||
// 步骤 6:健康检查
|
||
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
|
||
w.WriteHeader(http.StatusOK)
|
||
_, _ = w.Write([]byte("ok"))
|
||
})
|
||
|
||
// 步骤 7:监管业务 JSON 通道
|
||
mux.HandleFunc("/province/supervise/data", func(w http.ResponseWriter, r *http.Request) {
|
||
handleForward(w, r, "supervise", superviseProxy, superviseTarget, allowIPs, forwardSecret, enableForward)
|
||
})
|
||
|
||
// 步骤 8:处方 PDF 上传通道
|
||
mux.HandleFunc("/mng/file/auth/upload", func(w http.ResponseWriter, r *http.Request) {
|
||
handleForward(w, r, "file", fileProxy, fileTarget, allowIPs, forwardSecret, enableForward)
|
||
})
|
||
|
||
// 步骤 8b:政务云测试页 /t(ApiPost 风格,读 file api-logs、连通测试、导出 Postman)
|
||
testweb.Register(mux, testweb.Deps{
|
||
SuperviseTarget: superviseTarget,
|
||
FileTarget: fileTarget,
|
||
APILogRoot: APILogRoot(),
|
||
})
|
||
|
||
// 步骤 9:打印启动配置并监听
|
||
msg := fmt.Sprintf("xk-hy-forward-go 监听 %s | forward_enabled=%v | supervise=%s | file=%s",
|
||
listen, enableForward, superviseTarget, fileTarget)
|
||
appLogf(msg)
|
||
log.Print(msg)
|
||
if err := http.ListenAndServe(listen, logHTTP(mux)); err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
}
|