После добавления файла go.mod я не могу получить доступ к файлам шаблонов HTML из golang в App Engine. Все работает локально.
Я проверил, что HTML-файлы существуют в App Engine, используя Stackdriver Debug, но среда выполнения их не видит. Вот код, который у меня есть.
var templates map[string]*htmltpl.Template
func init() {
if templates == nil {
templates = make(map[string]*htmltpl.Template)
}
templatesDir := getTemplatesDir()
layouts, err := filepath.Glob(templatesDir + "/layouts/*.html")
if err != nil {
panic(err)
}
includes, err := filepath.Glob(templatesDir + "/includes/*.html")
if err != nil {
panic(err)
}
// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append(includes, layout)
templates[filepath.Base(layout)] = htmltpl.Must(htmltpl.ParseFiles(files...))
}
}
func getTemplatesDir() string {
_, filename, _, ok := runtime.Caller(1)
if !ok {
panic("Could not get filename")
}
dirpath := path.Join(path.Dir(filename), "../../templates")
return dirpath
}
Вот мой app.yaml
:
runtime: go111
main: ./main
handlers:
- url: .*
script: auto
secure: always
А вот моя структура каталогов:
.
├── app.yaml
├── db/
├── go.mod
├── go.sum
├── handlers/
├── main
│ └── main.go
├── middleware/
├── models/
├── static/
├── templates/
│ ├── includes
│ │ ├── base.html
│ │ ├── button.html
│ │ ├── message.html
│ │ └── profile.html
│ └── layouts
│ └── thread.html
└── utils
└── template
└── template.go
Я не понимаю, почему в App Engine вызов filepath.Glob(templatesDir + "/layouts/*.html")
возвращает пустой фрагмент, тогда как при локальном запуске он возвращает фрагмент, содержащий путь к thread.html
.