From aabf8faca7986f50b6a2951c115e0096a33bb300 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 11 Jul 2023 16:50:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=BA=E5=88=B6=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E8=BD=AC=E5=8F=91=20=E9=85=8D=E5=90=88vue=E7=9A=84?= =?UTF-8?q?=E9=9D=99=E6=80=81=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxyserver/main.go | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/proxyserver/main.go b/proxyserver/main.go index dcca254d..ae406159 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -6,7 +6,9 @@ import ( "log" "net" "net/http" + "net/http/httputil" "net/url" + "strings" "time" ) @@ -15,21 +17,22 @@ var Backends []*Backend func main() { rootDir := "../server" // Change this to your root directory vueBuild := "/home/eson/workspace/fusenpack-vue-created" + apiURL, err := url.Parse("http://localhost:9900") + if err != nil { + panic(err) + } mux := http.NewServeMux() - // 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.Handle("/", http.StripPrefix("/", fs)) - results := GetZeroInfo(rootDir) + + var allRoutes map[string]bool = make(map[string]bool) for _, result := range results { fmt.Printf("FolderName: %s, Host: %s, Port: %d, PrefixRoute: %v\n", result.FolderName, result.Host, result.Port, result.PrefixRoute) var routes []string for k := range result.PrefixRoute { routes = append(routes, k) + allRoutes[k] = true } Backends = append(Backends, @@ -38,6 +41,35 @@ func main() { routes...)) } + // 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) { + + var prefix string + idx := strings.Index(r.URL.Path[1:], "/") + if idx == -1 { + prefix = r.URL.Path + } else { + prefix = r.URL.Path[:idx] + } + // log.Println(r.URL.Path, prefix) + if _, ok := allRoutes[prefix]; ok { + proxy := httputil.NewSingleHostReverseProxy(apiURL) + proxy.ServeHTTP(w, r) + } else { + // fs.ServeHTTP(w, r) + if strings.HasPrefix(prefix, "/type") { + http.ServeFile(w, r, vueBuild+"/index.html") + } else if strings.HasPrefix(prefix, "/dt-") { + http.ServeFile(w, r, vueBuild+"/index.html") + } else { + fs.ServeHTTP(w, r) + } + } + }) + ServerAddress := ":9900" log.Println("listen on ", ServerAddress) log.Fatal(http.ListenAndServe(ServerAddress, mux))