我将如何在运行 golang 的 openshift 中安装 github.com/gorilla/mux。我知道在本地我们确实去获取并安装。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)
}
呼如林
相关分类