How to add external package to golang in openshift

huangapple go评论78阅读模式
英文:

How to add external package to golang in openshift

问题

在OpenShift上运行Golang,如何安装github.com/gorilla/mux?我知道在本地我们可以使用go get和go install。在OpenShift上有什么等效的方法?给出的代码在我的电脑上运行良好,但在实际网站上给出503 Service Unavailable错误。

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上安装github.com/gorilla/mux?在本地我们使用go get和go install。在OpenShift上的等效方法是什么?给出的代码在我的电脑上运行良好,但在实际网站上给出503 Service Unavailable错误。

英文:

how would I install github.com/gorilla/mux in openshift running golang. I know locally we do go get and go install. What is the equivalent for openshift. The code given works fine on my computer. But gives 503 Service Unavailable in the live site.

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)
}

答案1

得分: 1

尽管我对OpenShift没有经验,但通常你会希望将依赖项进行供应商管理。通过这样做,你可以确保应用程序可用正确的版本,并且不必担心OpenShift(或任何其他应用程序平台)的构建系统。

英文:

Even though I have no experience with openshift, generally you will want to vendor your dependencies. By doing so, you can be sure the right version is available to your application, and don't have to worry about openshifts (or any other application platforms) own build system.

答案2

得分: 0

上述代码的问题在于您没有使用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,如果找不到该包,它将尝试自动下载。至少对我来说,mux是有效的。

英文:

The problem with the above code is that you do not use the env variables specified by openshift.

You are suppose to start your program on specified port and host that OpenShift allocates - those are available as OPENSHIFT_GO_IP and OPENSHIFT_GO_PORT in the environment. So basically you have to replace yours with os.Getenv("OPENSHIFT_GO_IP") and os.Getenv("OPENSHIFT_GO_PORT") to get the specific host and port.

func main() {
bind := fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
http.ListenAndServe(bind, r)

Have a look at the documentation here: https://github.com/smarterclayton/openshift-go-cart

Regarding the mux it will attempt to automagically download the package for you if it cannot find it. At least mux works for me.

huangapple
  • 本文由 发表于 2014年11月28日 18:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/27186519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定