如何在运行golang的openshift中安装github.com/gorilla/mux。我知道在当地我们会去安装。 openshift的等价物是什么。给出的代码在我的计算机上运行正常。但是在实际站点中提供503服务不可用。
package main
import (
"github.com/gorilla/mux"
"fmt"
"net/http"
"os"
"io/ioutil"
)
func homeHandler(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res,req, "home/index.html")
}
func dataHandler(res http.ResponseWriter, req * http.Request){
params:= mux.Vars(req)
fName,_:=params["fname"]
res.Header().Set("Access-Control-Allow-Origin", "*")
contents,_ := ioutil.ReadFile("home/data/"+fName)
res.Header().Set("Content-Type", "application/json")
res.Write(contents)
}
func main() {
r := mux.NewRouter()
r.PathPrefix("/home/css/").Handler(http.StripPrefix("/home/css/",http.FileServer(http.Dir("home/css/"))))
r.PathPrefix("/home/lib/").Handler(http.StripPrefix("/home/lib/",http.FileServer(http.Dir("home/lib/"))))
r.PathPrefix("/home/views/").Handler(http.StripPrefix("/home/views/",http.FileServer(http.Dir("home/views/"))))
r.PathPrefix("/home/images/").Handler(http.StripPrefix("/home/images/",http.FileServer(http.Dir("home/images/"))))
r.HandleFunc("/home/data/{fname:.+}", dataHandler)
r.HandleFunc(`/home/{name:.*}`,homeHandler)
http.Handle("/", r)
bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
fmt.Printf("listening on %s...", bind)
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
答案 0 :(得分:1)
即使我没有使用openshift的经验,通常你也想要提供你的依赖项。通过这样做,您可以确保您的应用程序可以使用正确的版本,而不必担心openhifts(或任何其他应用程序平台)自己的构建系统。
答案 1 :(得分:0)
上述代码的问题在于您不使用openshift指定的env变量。
您可以在OpenShift分配的指定端口和主机上启动您的程序 - 这些在环境中可用作OPENSHIFT_GO_IP和OPENSHIFT_GO_PORT。所以基本上你必须用os.Getenv(“OPENSHIFT_GO_IP”)和os.Getenv(“OPENSHIFT_GO_PORT”)取代你的,以获得特定的主机和端口。
func main() {
bind := fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
http.ListenAndServe(bind, r)
请查看此处的文档:https://github.com/smarterclayton/openshift-go-cart
关于多路复用器,如果无法找到它,它将尝试自动为您下载该软件包。至少mux对我有用。