gpt4 book ai didi

go - 如何在openshift中将外部包添加到golang

转载 作者:IT王子 更新时间:2023-10-29 01:13:45 24 4
gpt4 key购买 nike

我将如何在运行 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 的经验,但通常您会希望提供您的依赖项。通过这样做,您可以确保您的应用程序可以使用正确的版本,而不必担心 openshifts(或任何其他应用程序平台)自己的构建系统。

关于go - 如何在openshift中将外部包添加到golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27186519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com