最新的api路径

This commit is contained in:
eson
2023-07-12 14:11:04 +08:00
parent 3c9fb417aa
commit 4dbb77b306
29 changed files with 221 additions and 162 deletions

View File

@@ -113,8 +113,9 @@ func extractPrefixRouteValues(content string) map[string]bool {
for _, line := range lines {
if strings.Contains(line, "Path:") {
path := strings.TrimSpace(strings.TrimPrefix(line, "Path:"))
path1 := strings.Split(strings.Trim(path, `"`), "/")[1]
path1 = "/" + path1
paths := strings.Split(strings.Trim(path, `"`), "/")
path1 := "/" + paths[1] + "/" + paths[2]
if _, ok := prefixPath[path1]; !ok {
prefixPath[path1] = true
}

View File

@@ -9,6 +9,7 @@ import (
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"
)
@@ -28,7 +29,16 @@ func SetCors(w http.ResponseWriter, r *http.Request) {
}
}
var pathdict sync.Map = sync.Map{}
func main() {
pathdict.Store("/css", true)
pathdict.Store("/fonts", true)
pathdict.Store("/img", true)
pathdict.Store("/js", true)
pathdict.Store("/svg", true)
pathdict.Store("/favicon.ico", true)
rootDir := "../server" // Change this to your root directory
vueBuild := "/home/eson/workspace/fusenpack-vue-created"
apiURL, err := url.Parse("http://localhost:9900")
@@ -59,27 +69,24 @@ func main() {
fs := http.FileServer(http.Dir(vueBuild))
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 {
prefix = r.URL.Path
} else {
prefix = r.URL.Path[:idx]
}
// log.Println(r.URL.Path, prefix)
if _, ok := allRoutes[prefix]; ok {
if strings.HasPrefix(r.URL.Path, "/api") {
proxy := httputil.NewSingleHostReverseProxy(apiURL)
proxy.ServeHTTP(w, r)
} else {
// fs.ServeHTTP(w, r)
if strings.HasPrefix(prefix, "/type") {
http.ServeFile(w, r, indexHtmlPath)
} else if strings.HasPrefix(prefix, "/dt-") {
http.ServeFile(w, r, indexHtmlPath)
idx := strings.Index(r.URL.Path[1:], "/")
var prefix string
if idx != -1 {
prefix = r.URL.Path[:idx+1]
} else {
fs.ServeHTTP(w, r)
prefix = r.URL.Path
}
if _, ok := pathdict.Load(prefix); ok {
fs.ServeHTTP(w, r)
} else {
http.ServeFile(w, r, indexHtmlPath)
}
}
}))