亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Google App Engine 中托管 Go 項目時應如何處理文件路徑?

在 Google App Engine 中托管 Go 項目時應如何處理文件路徑?

Go
九州編程 2023-07-17 14:28:52
我正在嘗試在 Google 的應用程序引擎上部署簡單的 go 語言代碼。這是我試圖部署的代碼。 https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/go11x/static主程序package mainimport (    "fmt"    "html/template"    "log"    "net/http"    "os"    "path/filepath"    "time")var (    indexTmpl = template.Must(        template.ParseFiles(filepath.Join("templates", "index.html")),    ))func main() {    http.HandleFunc("/", indexHandler)    // Serve static files out of the public directory.    // By configuring a static handler in app.yaml, App Engine serves all the    // static content itself. As a result, the following two lines are in    // effect for development only.    public := http.StripPrefix("/public", http.FileServer(http.Dir("public")))    http.Handle("/public/", public)    port := os.Getenv("PORT")    if port == "" {        port = "8080"        log.Printf("Defaulting to port %s", port)    }    log.Printf("Listening on port %s", port)    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))}// indexHandler uses a template to create an index.html.func indexHandler(w http.ResponseWriter, r *http.Request) {    if r.URL.Path != "/" {        http.NotFound(w, r)        return    }    type indexData struct {        Logo        string        Style       string        RequestTime string    }    data := indexData{        Logo:        "/public/gcp-gopher.svg",        Style:       "/public/style.css",        RequestTime: time.Now().Format(time.RFC822),    }    if err := indexTmpl.Execute(w, data); err != nil {        log.Printf("Error executing template: %v", err)        http.Error(w, "Internal server error", http.StatusInternalServerError)    }}問題:如何處理我希望應用程序讀取的模板和其他小文件?我的應用程序是一個玩具應用程序,因此我不需要云存儲或任何此類解決方案。我只想從(本地)目錄中讀取內容。
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

所以...我用 3 種不同的方式測試了這個部署,我發現:

  1. 使用 , 直接將存儲庫克隆到靜態文件夾,然后從那里進行部署,重現了該問題,但前提是我是從 Google Cloud Shell 執行此操作的git clone。cd

    A。后來我發現我在 Cloud Shell 中使用的 Go 版本是 Go 1.12。

    b.?我創建了一個新的 VM 實例,以在全新的 Go 1.11 環境中對其進行測試,并且相同的過程運行得非常順利。

  2. 與上面的過程相同,但我沒有從靜態部署,而是將其內容移動到另一個目錄,然后從那里部署它。

    A。這在 VM 實例和 Cloud Shell 中有效。

  3. 根據App Engine 標準環境中的 Go 1.11 快速入門中的建議,我使用go get命令將示例代碼下載cd到靜態文件夾并從那里進行部署。

    A。這在兩種環境中都有效。

我的建議是始終嘗試使用該go get命令下載 Google 的 golang 示例,正如指南中所建議的那樣,并且在我所做的測試中它沒有擾亂 App Engine 部署。

還值得一提的是,這兩個環境具有相同的 Cloud SDK 版本,即 259。


查看完整回答
反對 回復 2023-07-17
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號