I am trying to deploy a simple go language code on Google's app engine. This is the code I am trying to deploy. https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/go11x/static
main.go
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)
}
}
When I deploy this code using gcloud app deploy
and use the browser to load the webpage, I see error
2019-08-24 06:32:19 default[] "GET / HTTP/1.1" 500
2019-08-24 06:32:20 default[] panic: open templates/index.html: no such file or directory goroutine 1 [running]: html/template.Must(0x0, 0x800800, 0xc000078f90, 0
x0) /usr/local/go/src/html/template/template.go:372 +0x54
My app.yaml file looks like this. It has static mentioned but nothing about templates.
runtime: go111
handlers:
# Configure App Engine to serve any static assets.
- url: /public
static_dir: public
# Use HTTPS for all requests.
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
Question: How do I handle templates and other small files that I want the application to read? Mine is a toy application so I do not need cloud storage or any such solution. I just want to read from a (local) directory.