问题描述
因此,我已经使用Gorilla / Mux在Go中设置了SPA,但是我想在不同的路线上运行它,以便可以将我的两个Kubernetes服务连接在一起(2个不同的SPA)。 可能是我必须将kubernetes服务更改为/ stock吗?
我的一个服务在常规路径“ /”上运行,我想添加第二个服务,以便当用户键入“ https:// URL / stock”时,我的第二个SPA得以提供(希望第二个SPA在其上运行) / stock)
我知道一种方法就是将我的代码和其他内容合并到我的第一个服务中,但是我想知道我是否可以这样做?
main.go
type spaHandler struct {
staticPath string
indexPath string
}
func (h spaHandler) ServeHTTP(w http.ResponseWriter,r *http.Request) {
path,err := filepath.Abs(r.URL.Path)
if err != nil {
http.Error(w,err.Error(),http.StatusBadRequest)
return
}
path = filepath.Join(h.staticPath,r.URL.Path)
_,err = os.Stat(path)
if os.IsNotExist(err) {
http.ServeFile(w,r,filepath.Join(h.staticPath,h.indexPath))
return
} else if err != nil {
http.Error(w,http.StatusInternalServerError)
return
}
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w,r)
}
func main() {
router := mux.NewRouter().StrictSlash(true)
//Serve Other Routes Above PathPrefix
router.HandleFunc("/api/findStock",findStock)
spa := spaHandler{staticPath: "./stock/build",indexPath: "index.html"}
router.PathPrefix("/").Handler(spa) <------ any way to serve on /stock???
svr := &http.Server{
Handler: router,Addr: ":8080",WriteTimeout: 15 * time.Second,ReadTimeout: 15 * time.Second,}
log.Fatal(svr.ListenAndServe())
}
App.js
class App extends Component {
render(){
return (
<Router>
<div>
<section>
<NavBar/>
<Switch>
<Route exact path='/' component={Home}/>
</Switch>
</section>
</div>
</Router>
);
}
}
export default App;
我知道我可以在App.js中更改/ stock的路径,但是当我到达localhost:8080 /时,它仍然显示我的导航栏
已更新
ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
nginx.ingress.kubernetes.io/rewrite-target: /
name: ingress
spec:
rules:
- host: www.url.com
http:
paths:
- backend:
serviceName: service1
servicePort: 80
path: /
- backend:
serviceName: stockService
servicePort: 80
path: /stock
因此,当我转到/ stock端点时,我会得到
由于MIME类型(“文本/纯文本”)不匹配(X-Content-Type-Options:nosniff),来自“ www.url.com//static/css/2.d9ad5f5c.chunk.css”的资源被阻止)
我认为这是因为我的Stock Service默认路径在main.go中设置为/,因此它试图从那里提供文件,但是在Web上我试图从/ stock访问它。
新的更新/修复
main.go
func main() {
router := mux.NewRouter()
//Serve Other Routes Above PathPrefix
router.HandleFunc("/api/findStock",indexPath: "index.html"}
router.PathPrefix("/stock").Handler(http.StripPrefix("/stock",spa)) <-- UPDATED
svr := &http.Server{
Handler: router,Addr: ":8080",}
log.Fatal(svr.ListenAndServe())
}
App.js
class App extends Component {
render(){
return (
<Router>
<div>
<section>
<NavBar/>
<Switch>
<Route exact path='/stock' component={Home}/>
</Switch>
</section>
</div>
</Router>
);
}
}
export default App;
package.json
{
"homepage": "stock",<---- This was newly added to the top of my file
"name": "stock","version": "0.1.0",}
我仍在使用入口访问第二项服务
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)