diff --git a/mvc/controllers/controllers_pages/main.go b/mvc/controllers/controllers_pages/main.go index a482693..346b09e 100644 --- a/mvc/controllers/controllers_pages/main.go +++ b/mvc/controllers/controllers_pages/main.go @@ -21,60 +21,60 @@ func MainPageHandler(a *models.App) http.HandlerFunc { if count, err = tools.GetJournalctlLogsCount("server", models.Cfg.ServerDomain, 24); err != nil { log.Printf("%s", err.Error()) } - SendCount(w, count) + sendCount(w, count) return } // Пасхалка if r.Method == "LOVE" { - SendLove(w) + sendLove(w) return } // Пасхалка 2 if r.Method == "LIMINAL" { - SendLiminal(w) + sendLiminal(w) return } // Страничка рендерится только если ее нет в кэше - pageData, ok := a.Cache.Get(models_pages.MainPageTmplName) + pageData, ok := models.PagesCache.Get(models_pages.MainPageTmplName) if !ok { pageData, err = models_pages.RenderMainPage(a.Templates, a.Version) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - a.Cache.Set(models_pages.MainPageTmplName, pageData) + models.PagesCache.Set(models_pages.MainPageTmplName, pageData) } - SendMainPage(w, pageData.([]byte)) + sendMainPage(w, pageData.([]byte)) }) } // Отправляет страницу -func SendMainPage(w http.ResponseWriter, data []byte) { +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) { +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) { +func sendLove(w http.ResponseWriter) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write([]byte("13.01.2005\n")) } // Ответ на метод LIMINAL -func SendLiminal(w http.ResponseWriter) { +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" diff --git a/mvc/controllers/controllers_pages/main_ru.go b/mvc/controllers/controllers_pages/main_ru.go index 5b74e41..ded761f 100644 --- a/mvc/controllers/controllers_pages/main_ru.go +++ b/mvc/controllers/controllers_pages/main_ru.go @@ -12,22 +12,22 @@ func MainRuPageHandler(a *models.App) http.HandlerFunc { var err error // Страничка рендерится только если ее нет в кэше - pageData, ok := a.Cache.Get(models_pages.MainRuPageTmplName) + pageData, ok := models.PagesCache.Get(models_pages.MainRuPageTmplName) if !ok { pageData, err = models_pages.RenderMainRuPage(a.Templates, a.Version) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - a.Cache.Set(models_pages.MainRuPageTmplName, pageData) + models.PagesCache.Set(models_pages.MainRuPageTmplName, pageData) } - SendMainPage(w, pageData.([]byte)) + sendMainRuPage(w, pageData.([]byte)) }) } // Отправляет страницу -func SendMainRuPage(w http.ResponseWriter, data []byte) { +func sendMainRuPage(w http.ResponseWriter, data []byte) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write(data) diff --git a/mvc/controllers/controllers_pages/post.go b/mvc/controllers/controllers_pages/post.go index c99af0b..da2f534 100644 --- a/mvc/controllers/controllers_pages/post.go +++ b/mvc/controllers/controllers_pages/post.go @@ -14,7 +14,7 @@ func PostPageHandler(a *models.App) http.HandlerFunc { posts := models_pages.GetPosts() // Страничка рендерится только если ее нет в кэше - pageData, ok := a.Cache.Get(models_pages.PostPageTmplName) + pageData, ok := models.PagesCache.Get(models_pages.PostPageTmplName) if !ok { post := posts[models_pages.PostLink(r.URL.Path)] @@ -24,15 +24,15 @@ func PostPageHandler(a *models.App) http.HandlerFunc { http.Error(w, err.Error(), http.StatusInternalServerError) return } - a.Cache.Set(models_pages.PostPageTmplName, pageData) + models.PagesCache.Set(models_pages.PostPageTmplName, pageData) } - SendPostPage(w, pageData.([]byte)) + sendPostPage(w, pageData.([]byte)) }) } // Отправляет страницу -func SendPostPage(w http.ResponseWriter, data []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) diff --git a/mvc/controllers/controllers_pages/posts.go b/mvc/controllers/controllers_pages/posts.go index ea06d78..58522aa 100644 --- a/mvc/controllers/controllers_pages/posts.go +++ b/mvc/controllers/controllers_pages/posts.go @@ -12,22 +12,22 @@ func PostsPageHandler(a *models.App) http.HandlerFunc { var err error // Страничка рендерится только если ее нет в кэше - pageData, ok := a.Cache.Get(models_pages.PostsPageTmplName) + pageData, ok := models.PagesCache.Get(models_pages.PostsPageTmplName) if !ok { pageData, err = models_pages.RenderPostsPage(a.Templates, a.Version) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - a.Cache.Set(models_pages.PostsPageTmplName, pageData) + models.PagesCache.Set(models_pages.PostsPageTmplName, pageData) } - SendPostsPage(w, pageData.([]byte)) + sendPostsPage(w, pageData.([]byte)) }) } // Отправляет страницу -func SendPostsPage(w http.ResponseWriter, data []byte) { +func sendPostsPage(w http.ResponseWriter, data []byte) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write(data) diff --git a/mvc/controllers/middlewares.go b/mvc/controllers/middlewares.go index 64c79d3..d57c130 100644 --- a/mvc/controllers/middlewares.go +++ b/mvc/controllers/middlewares.go @@ -9,21 +9,18 @@ import ( type Middleware func(http.Handler) http.Handler var ( - MiddlewaresChain = CreateMiddlewaresChain( - LoggingMiddleware, + MiddlewaresChain = createMiddlewaresChain( + loggingMiddleware, ) ) /* Возвращает один middleware, который объединяет все переданные -CreateMiddlewaresChain(m1, m2, m3) +createMiddlewaresChain(m1, m2, m3) = func(next http.Handler) http.Handler { return m1(m2(m3(final))) } - -CreateMiddlewaresChain(LoggingMiddleware) -= func(next http.Handler) http.Handler { return LoggingMiddleware(final) } */ -func CreateMiddlewaresChain(middlewares ...Middleware) Middleware { +func createMiddlewaresChain(middlewares ...Middleware) Middleware { return func(final http.Handler) http.Handler { for i := len(middlewares) - 1; i >= 0; i-- { final = middlewares[i](final) @@ -32,7 +29,7 @@ func CreateMiddlewaresChain(middlewares ...Middleware) Middleware { } } -func LoggingMiddleware(next http.Handler) http.Handler { +func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() diff --git a/mvc/models/app.go b/mvc/models/app.go index f388960..36caa8b 100644 --- a/mvc/models/app.go +++ b/mvc/models/app.go @@ -9,10 +9,9 @@ import ( "time" ) -// App хранит информацию о приложении +// App хранит шаблоны и время запуска type App struct { Templates *template.Template // Шаблоны страниц - Cache *Cache // Кэш (отрендеренные странички) Version int64 // Время запуска } @@ -21,7 +20,6 @@ func AppInit() (*App, error) { a := &App{ Version: time.Now().Unix(), - Cache: CacheInit(), } // Загрузка шаблонов diff --git a/mvc/models/cache.go b/mvc/models/cache.go index 585711b..847d5ef 100644 --- a/mvc/models/cache.go +++ b/mvc/models/cache.go @@ -2,23 +2,23 @@ package models import "sync" -type Cache struct { +type cache struct { Data map[string]any Mu sync.RWMutex } -func CacheInit() *Cache { - return &Cache{Data: make(map[string]any)} -} +var ( + PagesCache = cache{Data: make(map[string]any)} +) -func (c *Cache) Get(key string) (any, bool) { +func (c *cache) Get(key string) (any, bool) { c.Mu.RLock() pageData, ok := c.Data[key] c.Mu.RUnlock() return pageData, ok } -func (c *Cache) Set(key string, data any) { +func (c *cache) Set(key string, data any) { c.Mu.Lock() c.Data[key] = data c.Mu.Unlock() diff --git a/mvc/models/models_pages/posts.go b/mvc/models/models_pages/posts.go index d56650a..4686e21 100644 --- a/mvc/models/models_pages/posts.go +++ b/mvc/models/models_pages/posts.go @@ -19,10 +19,10 @@ const ( PostsPageTmplName = "posts.gohtml" ) -type Posts map[PostLink]*Post +type posts map[PostLink]*Post var ( - posts = Posts{} + allPosts = posts{} ) func init() { @@ -31,8 +31,8 @@ func init() { } } -func GetPosts() Posts { - return posts +func GetPosts() posts { + return allPosts } func loadPosts(dir string) error { @@ -54,7 +54,7 @@ func loadPosts(dir string) error { html := tools.MdToHTML(md) link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".md")) - posts[PostLink(link)] = newPost(link, html) + allPosts[PostLink(link)] = newPost(link, html) } return nil }) @@ -72,7 +72,7 @@ func RenderPostsPage(templates *template.Template, version int64) ([]byte, error context := map[string]any{ "version": version, "renderingTimestamp": time.Now().Unix(), - "posts": posts, + "posts": allPosts, } if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil {