修改
This commit is contained in:
parent
d2766d5bd3
commit
3d60f7053e
@ -14,6 +14,20 @@ import (
|
|||||||
|
|
||||||
var Backends []*Backend
|
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() {
|
func main() {
|
||||||
rootDir := "../server" // Change this to your root directory
|
rootDir := "../server" // Change this to your root directory
|
||||||
vueBuild := "/home/eson/workspace/fusenpack-vue-created"
|
vueBuild := "/home/eson/workspace/fusenpack-vue-created"
|
||||||
@ -43,10 +57,8 @@ func main() {
|
|||||||
|
|
||||||
// Define the static file server that serves the Vue dist folder
|
// Define the static file server that serves the Vue dist folder
|
||||||
fs := http.FileServer(http.Dir(vueBuild))
|
fs := http.FileServer(http.Dir(vueBuild))
|
||||||
|
indexHtmlPath := vueBuild + "/index.html"
|
||||||
// Define a handler function for the Vue static assets
|
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
var prefix string
|
var prefix string
|
||||||
idx := strings.Index(r.URL.Path[1:], "/")
|
idx := strings.Index(r.URL.Path[1:], "/")
|
||||||
if idx == -1 {
|
if idx == -1 {
|
||||||
@ -54,6 +66,7 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
prefix = r.URL.Path[:idx]
|
prefix = r.URL.Path[:idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Println(r.URL.Path, prefix)
|
// log.Println(r.URL.Path, prefix)
|
||||||
if _, ok := allRoutes[prefix]; ok {
|
if _, ok := allRoutes[prefix]; ok {
|
||||||
proxy := httputil.NewSingleHostReverseProxy(apiURL)
|
proxy := httputil.NewSingleHostReverseProxy(apiURL)
|
||||||
@ -61,14 +74,14 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
// fs.ServeHTTP(w, r)
|
// fs.ServeHTTP(w, r)
|
||||||
if strings.HasPrefix(prefix, "/type") {
|
if strings.HasPrefix(prefix, "/type") {
|
||||||
http.ServeFile(w, r, vueBuild+"/index.html")
|
http.ServeFile(w, r, indexHtmlPath)
|
||||||
} else if strings.HasPrefix(prefix, "/dt-") {
|
} else if strings.HasPrefix(prefix, "/dt-") {
|
||||||
http.ServeFile(w, r, vueBuild+"/index.html")
|
http.ServeFile(w, r, indexHtmlPath)
|
||||||
} else {
|
} else {
|
||||||
fs.ServeHTTP(w, r)
|
fs.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
|
||||||
ServerAddress := ":9900"
|
ServerAddress := ":9900"
|
||||||
log.Println("listen on ", ServerAddress)
|
log.Println("listen on ", ServerAddress)
|
||||||
@ -117,6 +130,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasBridge := strings.Contains(r.Header.Get("Access-Control-Request-Headers"), "bridge")
|
||||||
|
|
||||||
// 解析目标URL时已经包含了查询参数
|
// 解析目标URL时已经包含了查询参数
|
||||||
targetURL.RawQuery = r.URL.RawQuery
|
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)
|
w.WriteHeader(resp.StatusCode)
|
||||||
_, err = io.Copy(w, resp.Body)
|
_, err = io.Copy(w, resp.Body)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user