40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package controllers_pages
|
||
|
||
import (
|
||
"main/mvc/models"
|
||
"main/mvc/models/models_pages"
|
||
"net/http"
|
||
)
|
||
|
||
// Обработчик главной страницы
|
||
func PostPageHandler(a *models.App) http.HandlerFunc {
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
var err error
|
||
|
||
posts := models_pages.GetPosts()
|
||
|
||
// Страничка рендерится только если ее нет в кэше
|
||
pageData, ok := models.PagesCache.Get(models_pages.PostPageTmplName)
|
||
if !ok {
|
||
|
||
post := posts[models_pages.PostLink(r.URL.Path)]
|
||
|
||
pageData, err = models_pages.RenderPostPage(a.Templates, a.Version, post.Data)
|
||
if err != nil {
|
||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
return
|
||
}
|
||
models.PagesCache.Set(models_pages.PostPageTmplName, pageData)
|
||
}
|
||
|
||
sendPostPage(w, pageData.([]byte))
|
||
})
|
||
}
|
||
|
||
// Отправляет страницу
|
||
func sendPostPage(w http.ResponseWriter, data []byte) {
|
||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||
w.WriteHeader(http.StatusOK)
|
||
w.Write(data)
|
||
}
|