Реки и копирование темплейтов в озу

This commit is contained in:
lost+skunk 2024-07-06 20:06:04 +03:00
parent 7f1ab1b73d
commit ff4f1a2422
7 changed files with 73 additions and 38 deletions

View File

@ -11,6 +11,7 @@ type cache_config struct {
Path string
MaxSize int64 `json:"max-size"`
Lifetime int64
UpdateInterval int64 `json:"update-interval"`
}
type config struct {
@ -20,6 +21,7 @@ type config struct {
Cache cache_config
Proxy, Nsfw bool
WixmpProxy string `json:"wixmp-proxy"`
TemplatesDir string `json:"templates-dir"`
}
var CFG = config{
@ -29,7 +31,9 @@ var CFG = config{
Cache: cache_config{
Enabled: true,
Path: "cache",
UpdateInterval: 1,
},
TemplatesDir: "html",
Proxy: true,
Nsfw: true,
}

View File

@ -19,7 +19,8 @@ import (
// парсинг темплейтов
func (s skunkyart) ExecuteTemplate(file string, data any) {
var buf strings.Builder
tmp, e := template.ParseFiles(file)
tmp := template.New(file)
tmp, e := tmp.Parse(Templates[file])
err(e)
err(tmp.Execute(&buf, &data))
wr(s.Writer, buf.String())
@ -57,8 +58,10 @@ func (s skunkyart) ConvertDeviantArtUrlToSkunkyArt(url string) (output string) {
if len(url) > 32 && url[27:32] != "stash" {
url = url[27:]
toart := strings.Index(url, "/art/")
if toart != -1 {
output = UrlBuilder("post", url[:toart], url[toart+5:])
}
}
return
}
@ -261,7 +264,7 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
if url != "" {
list.WriteString(`<a title="open/download" href="`)
list.WriteString(url)
list.WriteString(`"><img src="`)
list.WriteString(`"><img loading="lazy" src="`)
list.WriteString(url)
list.WriteString(`" width="15%"></a>`)
} else {
@ -296,8 +299,10 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
return ""
} else {
list.WriteString("</div>")
if content != nil {
list.WriteString(s.NavBase(content[0]))
}
}
return list.String()
}
@ -425,6 +430,7 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
b, _, _ := download()
s.Writer.Write(b)
} else {
s.Writer.WriteHeader(403)
s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled."))
}
}
@ -441,6 +447,9 @@ func InitCacheSystem() {
err(e)
for _, a := range dirnames {
a = c.Path + "/" + a
rm := func() {
err(os.RemoveAll(a))
}
if c.Lifetime != 0 {
now := time.Now().UnixMilli()
@ -449,15 +458,33 @@ func InitCacheSystem() {
time := time.Unix(stat.Ctim.Unix()).UnixMilli()
if time+c.Lifetime <= now {
os.RemoveAll(a)
rm()
}
}
if c.MaxSize != 0 && stat.Size() > c.MaxSize {
os.RemoveAll(a)
rm()
}
}
dir.Close()
time.Sleep(time.Second)
time.Sleep(time.Second * time.Duration(CFG.Cache.UpdateInterval))
}
}
func CopyTemplatesToMemory() {
try := func(e error) {
if e != nil {
panic(e.Error())
}
}
dir, e := os.ReadDir(CFG.TemplatesDir)
try(e)
for _, x := range dir {
n := CFG.TemplatesDir + "/" + x.Name()
file, e := os.ReadFile(n)
try(e)
Templates[n] = string(file)
}
}

View File

@ -13,6 +13,7 @@ import (
)
var wr = io.WriteString
var Templates = make(map[string]string)
type skunkyart struct {
Writer http.ResponseWriter
@ -31,6 +32,7 @@ type skunkyart struct {
SomeList string
Deviation struct {
Post devianter.Post
Related string
StringTime string
Tags string
Comments string
@ -65,19 +67,6 @@ type skunkyart struct {
}
}
// var Templates struct {
// Index string
// About string
//
// GRuser string
// Deviation string
// List string
// Search string
// }
// //go:embed ../html/*
// var Templates embed.FS
func (s skunkyart) GRUser() {
if len(s.Query) < 1 {
s.ReturnHTTPError(400)
@ -217,6 +206,12 @@ func (s skunkyart) Deviation(author, postname string) {
post.Post.Description = ParseDescription(post.Post.Deviation.TextContent)
// время публикации
post.StringTime = post.Post.Deviation.PublishedTime.UTC().String()
post.Post.IMG = s.ParseMedia(post.Post.Deviation.Media)
for _, x := range post.Post.Deviation.Extended.RelatedContent {
if len(x.Deviations) != 0 {
post.Related = s.DeviationList(x.Deviations)
}
}
// хештэги
for _, x := range post.Post.Deviation.Extended.Tags {

View File

@ -1,13 +1,15 @@
{
"listen": "0.0.0.0:3003",
"base-path": "/skunky/",
"base-path": "/",
"cache": {
"enabled": true,
"path": "cache",
"lifetime": null,
"max-size": 100000
"max-size": 100000,
"update-interval": 5
},
"proxy": true,
"templates-dir": "html",
"wixmp-proxy": "http://127.0.0.1:8080",
"proxy": true,
"nsfw": false
}

View File

@ -38,6 +38,12 @@
{{.Templates.Deviation.Post.Description}}
</figcaption>
{{end}}
{{if ne .Templates.Deviation.Related ""}}
<details>
<summary>Related content</summary>
{{.Templates.Deviation.Related}}
</details>
{{end}}
{{if (ne .Templates.Deviation.Comments "")}}
{{.Templates.Deviation.Comments}}
{{end}}

View File

@ -18,7 +18,7 @@
</header>
{{if ne .Templates.Search.List ""}}
{{if ne .Templates.Search.Content.Total 0}}
<h1>Total resuls: {{.Templates.Search.Content.Total}}</h1>
<h1>Results by request '{{.QueryRaw}}': {{.Templates.Search.Content.Total}}</h1>
{{end}}
{{.Templates.Search.List}}
{{else}}

View File

@ -8,6 +8,7 @@ import (
func main() {
app.ExecuteConfig()
app.CopyTemplatesToMemory()
err := devianter.UpdateCSRF()
if err != nil {
println(err.Error())