如何在openshift中向golang添加外部包

如何在openshift中向golang添加外部包,go,openshift,Go,Openshift,如何在运行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.Respon

如何在运行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)
    }

尽管我没有openshift的经验,但通常您会希望提供依赖项。通过这样做,您可以确保您的应用程序可以使用正确的版本,并且不必担心openshift(或任何其他应用程序平台)自己的构建系统。

上述代码的问题在于您没有使用openshift指定的环境变量

假设您在OpenShift分配的指定端口和主机上启动程序,这些端口和主机在环境中作为OpenShift_GO_IP和OpenShift_GO_端口提供。因此,基本上您必须用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)
请查看此处的文档:


关于mux,如果找不到它,它将尝试自动为您下载软件包。至少mux对我有效。

如果我理解正确,目前支持的方法是销售。但是,可以尝试使用Openshift盒式脚本:不确定这是评论还是真实答案。与OP有相同的问题