48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package controllers_pages
|
|
|
|
import (
|
|
"main/mvc/models"
|
|
"main/mvc/models/models_pages"
|
|
"net/http"
|
|
)
|
|
|
|
// Обработчик страницы поста
|
|
func PostPageHandler(app *models.App) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
link := r.URL.Path
|
|
cacheKey := link
|
|
ajax := r.URL.Query().Get("ajax") == "true"
|
|
if ajax {
|
|
cacheKey += "?ajax=true"
|
|
}
|
|
|
|
if pageData, ok := app.PagesCache.Get(cacheKey); ok {
|
|
sendPostPage(w, pageData)
|
|
return
|
|
}
|
|
|
|
post := app.PostsMap[models_pages.PostLink(link)]
|
|
|
|
var (
|
|
pageData []byte
|
|
err error
|
|
)
|
|
|
|
pageData, err = post.RenderPostPage(app.Templates, app.Version, ajax)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
app.PagesCache.Set(cacheKey, pageData)
|
|
|
|
sendPostPage(w, pageData)
|
|
})
|
|
}
|
|
|
|
// Отправляет страницу
|
|
func sendPostPage(w http.ResponseWriter, data []byte) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(data)
|
|
}
|