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