hikan.ru/mvc/controllers/controllers_pages/main.go

94 lines
2.5 KiB
Go

package controllers_pages
import (
"fmt"
"log"
"main/mvc/models"
"main/mvc/models/models_pages"
"main/tools"
"net/http"
)
// Обработчик главной страницы
func MainPageHandler(app *models.App) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
pageData []byte
err error
)
// Количество запросов, обработанных сервером за 24ч
if r.Method == "COUNT" {
var count []byte
if count, err = tools.GetJournalctlLogsCount("server", app.Cfg.ServerDomain, 24); err != nil {
log.Printf("%s", err.Error())
}
sendCount(w, count)
return
}
// Пасхалка
if r.Method == "LOVE" {
sendLove(w)
return
}
// Пасхалка 2
if r.Method == "LIMINAL" {
sendLiminal(w)
return
}
cacheKey := models_pages.MainPageTmplName
ajax := r.URL.Query().Get("ajax") == "true"
if ajax {
cacheKey += "?ajax=true"
}
if pageData, ok := app.PagesCache.Get(cacheKey); ok {
sendMainPage(w, pageData)
return
}
pageData, err = models_pages.RenderMainPage(app.Templates, app.Version, ajax)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
app.PagesCache.Set(cacheKey, pageData)
sendMainPage(w, pageData)
})
}
// Отправляет страницу
func sendMainPage(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// Ответ на метод COUNT
func sendCount(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// Ответ на метод LOVE
func sendLove(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "13.01.2005\n")
}
// Ответ на метод LIMINAL
func sendLiminal(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
text := "If you're not careful and you slip out of reality in the wrong place, you'll end up in the Backstage, where there's nothing but the stench of old damp carpet, yellow-colored madness, the endless unbearable hum of fluorescent lights, and roughly six hundred million square miles of randomly arranged empty rooms.\n"
fmt.Fprint(w, text)
}