Files
xk-hy-forward-go/internal/forward/run.go
2026-05-22 08:42:27 +08:00

58 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package forward 内网双通道透明转发(监管 JSON + 处方 PDF)。
package forward
import (
"fmt"
"log"
"net/http"
)
// Run 启动 forward 全部 HTTP 服务:/health、/province/supervise/data、/mng/file/auth/upload。
func Run() {
LoadEnvFiles()
if err := initAppLog(); err != nil {
log.Printf("应用日志初始化失败: %v", err)
}
listen := env("LISTEN_ADDR", ":16001")
allowIPs := env("ALLOW_IPS", "")
forwardSecret := env("FORWARD_SHARED_SECRET", "")
superviseTarget := env("SUPERVISE_TARGET_URL", "")
if superviseTarget == "" {
superviseTarget = env("TARGET_URL", DefaultSuperviseTarget)
}
fileTarget := env("FILE_TARGET_URL", DefaultFileTarget)
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()
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.HandleFunc("/province/supervise/data", func(w http.ResponseWriter, r *http.Request) {
handleForward(w, r, "supervise", superviseProxy, superviseTarget, allowIPs, forwardSecret)
})
mux.HandleFunc("/mng/file/auth/upload", func(w http.ResponseWriter, r *http.Request) {
handleForward(w, r, "file", fileProxy, fileTarget, allowIPs, forwardSecret)
})
msg := fmt.Sprintf("xk-hy-forward-go 监听 %s | supervise=%s | file=%s", listen, superviseTarget, fileTarget)
appLogf(msg)
log.Print(msg)
if err := http.ListenAndServe(listen, mux); err != nil {
log.Fatal(err)
}
}