Compare commits

..

1 Commits

Author SHA1 Message Date
serr 975e7e8a4c add client cache, add page struct 2025-04-23 12:17:20 +03:00
9 changed files with 103 additions and 107 deletions

View File

@ -6,7 +6,8 @@ body {
margin: 0;
align-items: flex-start;
padding: 10px;
background-color: #E6E6FA;
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPSc0MCcgaGVpZ2h0PSc0MCc+PHJlY3QgZmlsbD0nIzkxZWFlNCcgeD0nMCcgeT0nMCcgd2lkdGg9JzIwcHgnIGhlaWdodD0nMjBweCcvPjxyZWN0IGZpbGw9JyM4NmE4ZTcnIHg9JzAnIHk9JzIwJyB3aWR0aD0nMjBweCcgaGVpZ2h0PScyMHB4Jy8+PHJlY3QgZmlsbD0nIzkxZWFlNCcgeD0nMjAnIHk9JzIwJyB3aWR0aD0nMjBweCcgaGVpZ2h0PScyMHB4Jy8+PHJlY3QgZmlsbD0nIzg2YThlNycgeD0nMjAnIHk9JzAnIHdpZHRoPScyMHB4JyBoZWlnaHQ9JzIwcHgnLz48L3N2Zz4=");
background-repeat: repeat;
}
header, main, footer {
@ -32,7 +33,6 @@ div {
border: 1px solid;
width: 100%;
padding: 0 20px;
overflow: hidden;
}
h1 {
@ -42,17 +42,6 @@ h1 {
border-bottom: 1px solid;
}
pre {
padding: 10px;
background: #E6E6FA;
border: 1px solid #e8e8e8;
overflow-x: auto;
white-space: pre;
word-wrap: normal;
width: 0;
min-width: calc(100% - 20px);
}
.count {
display: flex;
justify-content: center;

View File

@ -38,25 +38,35 @@ func MainPageHandler(app *models.App) http.HandlerFunc {
}
// Страничка рендерится только если ее нет в кэше
pageData, ok := app.PagesCache.Get(models_pages.MainPageTmplName)
page, ok := app.PagesCache.Get(models_pages.MainPageTmplName)
if !ok {
pageData, err = models_pages.RenderMainPage(app.Templates, app.Version)
pageData, err := models_pages.RenderMainPage(app.Templates, app.Version)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
app.PagesCache.Set(models_pages.MainPageTmplName, pageData)
ETag := models.GenerateETag(pageData)
page = &models.Page{Data: pageData, ETag: ETag}
app.PagesCache.Set(models_pages.MainPageTmplName, page)
}
sendMainPage(w, pageData)
sendMainPage(r, w, page)
})
}
// Отправляет страницу
func sendMainPage(w http.ResponseWriter, data []byte) {
func sendMainPage(r *http.Request, w http.ResponseWriter, page *models.Page) {
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("ETag", page.ETag)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
if match := r.Header.Get("If-None-Match"); match == page.ETag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Write(page.Data)
}
// Ответ на метод COUNT

View File

@ -10,8 +10,6 @@ import (
// Обработчик главной страницы
func PostPageHandler(app *models.App) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
postLink := r.URL.Path
// Ссылки на посты имеют вид postLink = /link/, а если прилетело что то типо /link/123123,
@ -22,26 +20,33 @@ func PostPageHandler(app *models.App) http.HandlerFunc {
}
// Страничка рендерится только если ее нет в кэше
pageData, ok := app.PagesCache.Get(postLink)
page, ok := app.PagesCache.Get(postLink)
if !ok {
post := app.Posts[models_pages.PostLink(postLink)]
pageData, err = models_pages.RenderPostPage(app.Templates, app.Version, post.Data)
pageData, err := models_pages.RenderPostPage(app.Templates, app.Version, post.Data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
app.PagesCache.Set(postLink, pageData)
ETag := models.GenerateETag(pageData)
page = &models.Page{Data: pageData, ETag: ETag}
app.PagesCache.Set(postLink, page)
}
sendPostPage(w, pageData)
sendPostPage(r, w, page)
})
}
// Отправляет страницу
func sendPostPage(w http.ResponseWriter, data []byte) {
func sendPostPage(r *http.Request, w http.ResponseWriter, page *models.Page) {
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("ETag", page.ETag)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
if match := r.Header.Get("If-None-Match"); match == page.ETag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Write(page.Data)
}

View File

@ -9,26 +9,33 @@ import (
// Обработчик главной страницы
func PostsPageHandler(app *models.App) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
// Страничка рендерится только если ее нет в кэше
pageData, ok := app.PagesCache.Get(models_pages.PostsPageTmplName)
page, ok := app.PagesCache.Get(models_pages.PostsPageTmplName)
if !ok {
pageData, err = app.Posts.RenderPostsPage(app.Templates, app.Version)
pageData, err := app.Posts.RenderPostsPage(app.Templates, app.Version)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
app.PagesCache.Set(models_pages.PostsPageTmplName, pageData)
ETag := models.GenerateETag(pageData)
page = &models.Page{Data: pageData, ETag: ETag}
app.PagesCache.Set(models_pages.PostsPageTmplName, page)
}
sendPostsPage(w, pageData)
sendPostsPage(r, w, page)
})
}
// Отправляет страницу
func sendPostsPage(w http.ResponseWriter, data []byte) {
func sendPostsPage(r *http.Request, w http.ResponseWriter, page *models.Page) {
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("ETag", page.ETag)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
if match := r.Header.Get("If-None-Match"); match == page.ETag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Write(page.Data)
}

View File

@ -1,25 +1,40 @@
package models
import "sync"
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sync"
)
type Page struct {
Data []byte
ETag string
}
type Cache struct {
Data map[string][]byte
Data map[string]*Page
Mu sync.RWMutex
}
func initCache() *Cache {
return &Cache{Data: make(map[string][]byte)}
return &Cache{Data: make(map[string]*Page)}
}
func (c *Cache) Get(key string) ([]byte, bool) {
func (c *Cache) Get(key string) (*Page, bool) {
c.Mu.RLock()
pageData, ok := c.Data[key]
page, ok := c.Data[key]
c.Mu.RUnlock()
return pageData, ok
return page, ok
}
func (c *Cache) Set(key string, data []byte) {
func (c *Cache) Set(key string, page *Page) {
c.Mu.Lock()
c.Data[key] = data
c.Data[key] = page
c.Mu.Unlock()
}
func GenerateETag(data []byte) string {
hash := md5.Sum(data)
return fmt.Sprintf(`"%s"`, hex.EncodeToString(hash[:]))
}

View File

@ -12,18 +12,12 @@
<p>
<strong>some system information</strong>:
</p>
<p>
> unix timestamp of page rendering: <code>{{ .renderingTimestamp }}</code><br/>
> <code>curl -X LIMINAL https://hikan.ru</code> - what do you know about liminal spaces<br/>
> this site code repository - <a href="https://git.hikan.ru/serr/hikan.ru" target="_blank">git.hikan.ru/serr/hikan.ru</a><br/>
> 24-hour server request count (<code>curl -X COUNT https://hikan.ru</code>):
</p>
<p class="count">
<img src="/assets/pic/0.gif?v={{ .version }}">
<img src="/assets/pic/0.gif?v={{ .version }}">
<img src="/assets/pic/0.gif?v={{ .version }}">
<img src="/assets/pic/0.gif?v={{ .version }}">
</p>
<ul>
<li>unix timestamp of page rendering: <code>{{ .renderingTimestamp }}</code></li>
<li><code>curl -X LIMINAL https://hikan.ru</code> - what do you know about liminal spaces?</li>
<li>this site code repository - <a href="https://git.hikan.ru/serr/hikan.ru" target="_blank">git.hikan.ru/serr/hikan.ru</a></li>
<li><code>curl -X COUNT https://hikan.ru</code> - 24-hour server request count</li>
</ul>
</div>
<div>
<p>
@ -34,16 +28,4 @@
</p>
</div>
</footer>
<script>
fetch('/', {method: 'COUNT'})
.then(r => r.text())
.then(num => num.trim())
.then(number => {
if (number.length > 0) {
document.querySelector('p.count').innerHTML =
[...number].map(d => `<img src="/assets/pic/${d}.gif?v={{ .version }}">`).join('');
}
})
.catch(console.error);
</script>
{{ end }}

View File

@ -12,10 +12,10 @@
</p>
</div>
<div>
<p>
> <a href="/">main page</a><br/>
> <a href="/posts/">posts section</a>
</p>
<ul>
<li><a href="/">main page</a></li>
<li><a href="/posts/">posts section</a></li>
</ul>
</div>
</header>
{{ end }}

View File

@ -36,34 +36,34 @@
<h1>
things i love
</h1>
<p>
<strong>> coffee</strong>. i REALLY love coffee. almost any. and a lot of<br/>
<strong>> movies and TV series</strong> (especially TV series). i watch something almost every day<br/>
<strong>> true crime</strong>. i'm obsessed with serial killer cases, mysterious disappearances, unsolved murders - all that dark stuff<br/>
<strong>> russian underground rap</strong> like Slava KPSS, Zamay, MB Packet, Ovsyankin etc.<br/>
<strong>> simple and extensible code</strong>. i think if your code is overly complex, it means you are doing something wrong. most things are simpler than they seem
</p>
<ul>
<li><strong>coffee</strong>. i REALLY love coffee. almost any. and a lot of</li>
<li><strong>movies and TV series</strong> (especially TV series). i watch something almost every day</li>
<li><strong>true crime</strong>. i'm obsessed with serial killer cases, mysterious disappearances, unsolved murders - all that dark stuff</li>
<li><strong>russian underground rap</strong> like Slava KPSS, Zamay, MB Packet, Ovsyankin etc.</li>
<li><strong>simple and extensible code</strong>. i think if your code is overly complex, it means you are doing something wrong. most things are simpler than they seem</li>
</ul>
</div>
<div>
<h1>
projects
</h1>
<p>
> <a href="https://git.hikan.ru/serr" target="_blank">git.hikan.ru/serr</a> - check my repos<br/>
> <del>telegram bot with schedule for SPBPU - <a href="https://t.me/polysched_bot" target="_blank">polysched_bot</a></del> (transferred to a more proactive owner)<br/>
> <del>telegram bot with schedule for SPMI - <a href="https://t.me/gornischedule_bot" target="_blank">gornischedule_bot</a></del> (closed)<br/>
</p>
<ul>
<li><a href="https://git.hikan.ru/serr" target="_blank">git.hikan.ru/serr</a> - check my repos</li>
<li><del>telegram bot with schedule for SPBPU - <a href="https://t.me/polysched_bot" target="_blank">polysched_bot</a></del> (transferred to a more proactive owner)</li>
<li><del>telegram bot with schedule for SPMI - <a href="https://t.me/gornischedule_bot" target="_blank">gornischedule_bot</a></del> (closed)</li>
</ul>
</div>
<div>
<h1>
nice links
</h1>
<p>
> <a href="https://mo.rijndael.cc/" target="_blank">Mo</a>, thx for design idea!<br/>
> huge collection of Xakep issues - <a href="https://図書館.きく.コム/" target="_blank">図書館.きく.コム</a><br/>
> i love this website highlighting the Small Web - <a href="https://smallweb.cc/" target="_blank">smallweb</a><br/>
> very atmospheric forum about black metal - <a href="https://www.lycanthropia.net/" target="_blank">lycanthropia</a><br/>
</p>
<ul>
<li><a href="https://mo.rijndael.cc/" target="_blank">Mo</a>, thx for design idea!</li>
<li>huge collection of Xakep issues - <a href="https://図書館.きく.コム/" target="_blank">図書館.きく.コム</a></li>
<li>i love this website highlighting the Small Web - <a href="https://smallweb.cc/" target="_blank">smallweb</a></li>
<li>very atmospheric forum about black metal - <a href="https://www.lycanthropia.net/" target="_blank">lycanthropia</a></li>
</ul>
</div>
</main>
{{ template "footer" . }}

View File

@ -1,12 +0,0 @@
# Тест блоков с кодом!
Тестовый блок с кодом:
```
// Ответ на метод COUNT
#include <stdio.h>
int main() {
printf("very long striiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing!");
return 0;
}
```