我正在尝试在 Google 的应用程序引擎上部署简单的 go 语言代码。这是我试图部署的代码。 https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/go11x/static
主程序
package main
import (
"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)
}
}
问题:如何处理我希望应用程序读取的模板和其他小文件?我的应用程序是一个玩具应用程序,因此我不需要云存储或任何此类解决方案。我只想从(本地)目录中读取内容。
holdtom
相关分类