我正在嘗試在 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 種不同的方式測試了這個部署,我發現:
使用 , 直接將存儲庫克隆到靜態文件夾,然后從那里進行部署,重現了該問題,但前提是我是從 Google Cloud Shell 執行此操作的
git clone
。cd
A。后來我發現我在 Cloud Shell 中使用的 Go 版本是 Go 1.12。
b.?我創建了一個新的 VM 實例,以在全新的 Go 1.11 環境中對其進行測試,并且相同的過程運行得非常順利。
與上面的過程相同,但我沒有從靜態部署,而是將其內容移動到另一個目錄,然后從那里部署它。
A。這在 VM 實例和 Cloud Shell 中有效。
根據App Engine 標準環境中的 Go 1.11 快速入門中的建議,我使用
go get
命令將示例代碼下載cd
到靜態文件夾并從那里進行部署。A。這在兩種環境中都有效。
我的建議是始終嘗試使用該go get
命令下載 Google 的 golang 示例,正如指南中所建議的那樣,并且在我所做的測試中它沒有擾亂 App Engine 部署。
還值得一提的是,這兩個環境具有相同的 Cloud SDK 版本,即 259。
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報
0/150
提交
取消