我在Go中有一个简单的Web服务器,如下所示:
func webServerWay() {
r := mux.NewRouter()
//File Upload Code
r.HandleFunc("/upload", uploadFile).Methods("POST")
//Test Message
r.HandleFunc("/test", test).Methods("GET")
r.PathPrefix("/site/").Handler(http.StripPrefix("/site/", http.FileServer(http.Dir("site"))))
if err := http.ListenAndServe(":8010", r); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}简单测试函数如下所示:这是一个查看端点是否可达的简单函数。
func test(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome it Works!")
}站点结构为:
├── go.mod
├── go.sum
├── main.go
└── site
├── dist
└── index.html该应用程序在我的本地开发环境中完美工作,即Mac OS:
Ajay@apples-MacBook-Air:/$ curl localhost:8010/test
Welcome it Works!
Ajay@apples-MacBook-Air:/$ curl localhost:8010/site/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>gAnalytics</title>
</head>
<body>
<div id="root"></div>
</body>
<script defer src="./dist/appXYZ.min.js"></script></head>
</html>但是当我为Linux(Ubuntu20.04)x64创建一个构建版本时
GOOS=linux GOARCH=386 go build -o gApp24v1.linux main.go并在Linux中运行代码:
sudo ./gApp24v1.linux静态站点服务不起作用,但我可以到达测试端点:
root@gapp24:/home/ajay/app# curl localhost:8010/test
Welcome it Works!
root@gapp24:/home/ajay/app# curl localhost:8010/site/
404 page not found我在静态文件处理程序中尝试了多种编码方式,查看其他答案并阅读文档,但它在我的macOS中运行,而不是在我的Linux Machine中运行。
有什么建议吗!
另外,我的Linux(Ubuntu机器)中的netstat如下:
root@gapp24:/home/ajay/app# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 671/vsftpd
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 547/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 704/sshd: /usr/sbin
tcp6 0 0 :::8010 :::* LISTEN 1167/./gApp24v1.lin
tcp6 0 0 :::22 :::* LISTEN 704/sshd: /usr/sbin 发布于 2021-07-07 15:31:01
在部署构建文件时,还需要相应地移动相应的静态文件:
这里的gApp24v1.linux是为Linux构建的。包含文件夹site的静态文件也需要放在相应的路径中:
.
├── gApp24v1.linux
└── site
├── dist
│ ├── appXYZ.min.js
└── index.html发布于 2021-07-07 20:08:06
您可以使用此repo https://github.com/go-bindata/go-bindata将静态文件压缩到可管理的Go源代码中,而不是移动静态站点目录。
https://stackoverflow.com/questions/68280504
复制相似问题