some changes
parent
5a34d18ed6
commit
70d82d69d1
|
@ -21,60 +21,60 @@ func MainPageHandler(a *models.App) http.HandlerFunc {
|
||||||
if count, err = tools.GetJournalctlLogsCount("server", models.Cfg.ServerDomain, 24); err != nil {
|
if count, err = tools.GetJournalctlLogsCount("server", models.Cfg.ServerDomain, 24); err != nil {
|
||||||
log.Printf("%s", err.Error())
|
log.Printf("%s", err.Error())
|
||||||
}
|
}
|
||||||
SendCount(w, count)
|
sendCount(w, count)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Пасхалка
|
// Пасхалка
|
||||||
if r.Method == "LOVE" {
|
if r.Method == "LOVE" {
|
||||||
SendLove(w)
|
sendLove(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Пасхалка 2
|
// Пасхалка 2
|
||||||
if r.Method == "LIMINAL" {
|
if r.Method == "LIMINAL" {
|
||||||
SendLiminal(w)
|
sendLiminal(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Страничка рендерится только если ее нет в кэше
|
// Страничка рендерится только если ее нет в кэше
|
||||||
pageData, ok := a.Cache.Get(models_pages.MainPageTmplName)
|
pageData, ok := models.PagesCache.Get(models_pages.MainPageTmplName)
|
||||||
if !ok {
|
if !ok {
|
||||||
pageData, err = models_pages.RenderMainPage(a.Templates, a.Version)
|
pageData, err = models_pages.RenderMainPage(a.Templates, a.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ответ на метод COUNT
|
// Ответ на метод 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.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ответ на метод LOVE
|
// Ответ на метод LOVE
|
||||||
func SendLove(w http.ResponseWriter) {
|
func sendLove(w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("13.01.2005\n"))
|
w.Write([]byte("13.01.2005\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ответ на метод LIMINAL
|
// Ответ на метод LIMINAL
|
||||||
func SendLiminal(w http.ResponseWriter) {
|
func sendLiminal(w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
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"
|
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"
|
||||||
|
|
|
@ -12,22 +12,22 @@ func MainRuPageHandler(a *models.App) http.HandlerFunc {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Страничка рендерится только если ее нет в кэше
|
// Страничка рендерится только если ее нет в кэше
|
||||||
pageData, ok := a.Cache.Get(models_pages.MainRuPageTmplName)
|
pageData, ok := models.PagesCache.Get(models_pages.MainRuPageTmplName)
|
||||||
if !ok {
|
if !ok {
|
||||||
pageData, err = models_pages.RenderMainRuPage(a.Templates, a.Version)
|
pageData, err = models_pages.RenderMainRuPage(a.Templates, a.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
|
|
@ -14,7 +14,7 @@ func PostPageHandler(a *models.App) http.HandlerFunc {
|
||||||
posts := models_pages.GetPosts()
|
posts := models_pages.GetPosts()
|
||||||
|
|
||||||
// Страничка рендерится только если ее нет в кэше
|
// Страничка рендерится только если ее нет в кэше
|
||||||
pageData, ok := a.Cache.Get(models_pages.PostPageTmplName)
|
pageData, ok := models.PagesCache.Get(models_pages.PostPageTmplName)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
||||||
post := posts[models_pages.PostLink(r.URL.Path)]
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
|
|
@ -12,22 +12,22 @@ func PostsPageHandler(a *models.App) http.HandlerFunc {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Страничка рендерится только если ее нет в кэше
|
// Страничка рендерится только если ее нет в кэше
|
||||||
pageData, ok := a.Cache.Get(models_pages.PostsPageTmplName)
|
pageData, ok := models.PagesCache.Get(models_pages.PostsPageTmplName)
|
||||||
if !ok {
|
if !ok {
|
||||||
pageData, err = models_pages.RenderPostsPage(a.Templates, a.Version)
|
pageData, err = models_pages.RenderPostsPage(a.Templates, a.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(data)
|
w.Write(data)
|
||||||
|
|
|
@ -9,21 +9,18 @@ import (
|
||||||
type Middleware func(http.Handler) http.Handler
|
type Middleware func(http.Handler) http.Handler
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MiddlewaresChain = CreateMiddlewaresChain(
|
MiddlewaresChain = createMiddlewaresChain(
|
||||||
LoggingMiddleware,
|
loggingMiddleware,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Возвращает один middleware, который объединяет все переданные
|
Возвращает один middleware, который объединяет все переданные
|
||||||
|
|
||||||
CreateMiddlewaresChain(m1, m2, m3)
|
createMiddlewaresChain(m1, m2, m3)
|
||||||
= func(next http.Handler) http.Handler { return m1(m2(m3(final))) }
|
= 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 {
|
return func(final http.Handler) http.Handler {
|
||||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||||
final = middlewares[i](final)
|
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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App хранит информацию о приложении
|
// App хранит шаблоны и время запуска
|
||||||
type App struct {
|
type App struct {
|
||||||
Templates *template.Template // Шаблоны страниц
|
Templates *template.Template // Шаблоны страниц
|
||||||
Cache *Cache // Кэш (отрендеренные странички)
|
|
||||||
Version int64 // Время запуска
|
Version int64 // Время запуска
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +20,6 @@ func AppInit() (*App, error) {
|
||||||
|
|
||||||
a := &App{
|
a := &App{
|
||||||
Version: time.Now().Unix(),
|
Version: time.Now().Unix(),
|
||||||
Cache: CacheInit(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Загрузка шаблонов
|
// Загрузка шаблонов
|
||||||
|
|
|
@ -2,23 +2,23 @@ package models
|
||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
type Cache struct {
|
type cache struct {
|
||||||
Data map[string]any
|
Data map[string]any
|
||||||
Mu sync.RWMutex
|
Mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func CacheInit() *Cache {
|
var (
|
||||||
return &Cache{Data: make(map[string]any)}
|
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()
|
c.Mu.RLock()
|
||||||
pageData, ok := c.Data[key]
|
pageData, ok := c.Data[key]
|
||||||
c.Mu.RUnlock()
|
c.Mu.RUnlock()
|
||||||
return pageData, ok
|
return pageData, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Set(key string, data any) {
|
func (c *cache) Set(key string, data any) {
|
||||||
c.Mu.Lock()
|
c.Mu.Lock()
|
||||||
c.Data[key] = data
|
c.Data[key] = data
|
||||||
c.Mu.Unlock()
|
c.Mu.Unlock()
|
||||||
|
|
|
@ -19,10 +19,10 @@ const (
|
||||||
PostsPageTmplName = "posts.gohtml"
|
PostsPageTmplName = "posts.gohtml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Posts map[PostLink]*Post
|
type posts map[PostLink]*Post
|
||||||
|
|
||||||
var (
|
var (
|
||||||
posts = Posts{}
|
allPosts = posts{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -31,8 +31,8 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPosts() Posts {
|
func GetPosts() posts {
|
||||||
return posts
|
return allPosts
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPosts(dir string) error {
|
func loadPosts(dir string) error {
|
||||||
|
@ -54,7 +54,7 @@ func loadPosts(dir string) error {
|
||||||
|
|
||||||
html := tools.MdToHTML(md)
|
html := tools.MdToHTML(md)
|
||||||
link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".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
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -72,7 +72,7 @@ func RenderPostsPage(templates *template.Template, version int64) ([]byte, error
|
||||||
context := map[string]any{
|
context := map[string]any{
|
||||||
"version": version,
|
"version": version,
|
||||||
"renderingTimestamp": time.Now().Unix(),
|
"renderingTimestamp": time.Now().Unix(),
|
||||||
"posts": posts,
|
"posts": allPosts,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil {
|
if err := templates.ExecuteTemplate(&pageData, PostsPageTmplName, context); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue