How to add external package to golang in openshift

how would i install github.com/gorilla/mux in openshift running golang. I know we are running locally and going to install. Which is equivalent for openshift. The above code works fine on my computer. But it gives 503 Service Unavailable on a real 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)
    }

      

+3


source to share


2 answers


Even though I have no experience with openshift, usually you will need to sell your dependencies. By doing so, you can be sure that the right version is available for your application, and you don’t have to worry about your own open source build system (or any other application frameworks).



+1


source


The problem with the above code is that you are not using the env variables set with openshift.

Suppose you run your program on the specified port and host that OpenShift allocates - they are available as OPENSHIFT_GO_IP and OPENSHIFT_GO_PORT in the environment. So basically you have to replace your os.Getenv ("OPENSHIFT_GO_IP") and os.Getenv ("OPENSHIFT_GO_PORT") to get a 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)

      



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

As for the multiplexer, it will try to automatically download the package for you if it cannot find it. At least the multiplexer works for me.

0


source







All Articles