添加强制路由转发 配合vue的静态文件
This commit is contained in:
parent
43e2e835eb
commit
aabf8faca7
|
@ -6,7 +6,9 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,21 +17,22 @@ var Backends []*Backend
|
||||||
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"
|
||||||
|
apiURL, err := url.Parse("http://localhost:9900")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
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)
|
results := GetZeroInfo(rootDir)
|
||||||
|
|
||||||
|
var allRoutes map[string]bool = make(map[string]bool)
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
fmt.Printf("FolderName: %s, Host: %s, Port: %d, PrefixRoute: %v\n", result.FolderName, result.Host, result.Port, result.PrefixRoute)
|
fmt.Printf("FolderName: %s, Host: %s, Port: %d, PrefixRoute: %v\n", result.FolderName, result.Host, result.Port, result.PrefixRoute)
|
||||||
var routes []string
|
var routes []string
|
||||||
for k := range result.PrefixRoute {
|
for k := range result.PrefixRoute {
|
||||||
routes = append(routes, k)
|
routes = append(routes, k)
|
||||||
|
allRoutes[k] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
Backends = append(Backends,
|
Backends = append(Backends,
|
||||||
|
@ -38,6 +41,35 @@ func main() {
|
||||||
routes...))
|
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"
|
ServerAddress := ":9900"
|
||||||
log.Println("listen on ", ServerAddress)
|
log.Println("listen on ", ServerAddress)
|
||||||
log.Fatal(http.ListenAndServe(ServerAddress, mux))
|
log.Fatal(http.ListenAndServe(ServerAddress, mux))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user