修改
This commit is contained in:
parent
d2766d5bd3
commit
3d60f7053e
|
@ -14,6 +14,20 @@ import (
|
|||
|
||||
var Backends []*Backend
|
||||
|
||||
func SetCors(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
w.Header().Set("Access-Control-Expose-Headers", "*")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
// 如果请求方法为 OPTIONS,直接返回 200 状态码
|
||||
if r.Method == "OPTIONS" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rootDir := "../server" // Change this to your root directory
|
||||
vueBuild := "/home/eson/workspace/fusenpack-vue-created"
|
||||
|
@ -43,10 +57,8 @@ func main() {
|
|||
|
||||
// Define the static file server that serves the Vue dist folder
|
||||
fs := http.FileServer(http.Dir(vueBuild))
|
||||
|
||||
// Define a handler function for the Vue static assets
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
indexHtmlPath := vueBuild + "/index.html"
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var prefix string
|
||||
idx := strings.Index(r.URL.Path[1:], "/")
|
||||
if idx == -1 {
|
||||
|
@ -54,6 +66,7 @@ func main() {
|
|||
} else {
|
||||
prefix = r.URL.Path[:idx]
|
||||
}
|
||||
|
||||
// log.Println(r.URL.Path, prefix)
|
||||
if _, ok := allRoutes[prefix]; ok {
|
||||
proxy := httputil.NewSingleHostReverseProxy(apiURL)
|
||||
|
@ -61,14 +74,14 @@ func main() {
|
|||
} else {
|
||||
// fs.ServeHTTP(w, r)
|
||||
if strings.HasPrefix(prefix, "/type") {
|
||||
http.ServeFile(w, r, vueBuild+"/index.html")
|
||||
http.ServeFile(w, r, indexHtmlPath)
|
||||
} else if strings.HasPrefix(prefix, "/dt-") {
|
||||
http.ServeFile(w, r, vueBuild+"/index.html")
|
||||
http.ServeFile(w, r, indexHtmlPath)
|
||||
} else {
|
||||
fs.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
ServerAddress := ":9900"
|
||||
log.Println("listen on ", ServerAddress)
|
||||
|
@ -117,6 +130,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
|
|||
return
|
||||
}
|
||||
|
||||
hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge")
|
||||
|
||||
// 解析目标URL时已经包含了查询参数
|
||||
targetURL.RawQuery = r.URL.RawQuery
|
||||
|
||||
|
@ -151,6 +166,10 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
|
|||
}
|
||||
}
|
||||
|
||||
if hasBridge {
|
||||
w.Header().Add("Access-Control-Allow-Headers", "Bridge")
|
||||
}
|
||||
|
||||
// 转发目标服务器的响应状态码和主体
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
_, err = io.Copy(w, resp.Body)
|
||||
|
|
Loading…
Reference in New Issue
Block a user