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

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

@ -7,19 +7,21 @@ import (
) )
type cache_config struct { type cache_config struct {
Enabled bool Enabled bool
Path string Path string
MaxSize int64 `json:"max-size"` MaxSize int64 `json:"max-size"`
Lifetime int64 Lifetime int64
UpdateInterval int64 `json:"update-interval"`
} }
type config struct { type config struct {
cfg string cfg string
Listen string Listen string
BasePath string `json:"base-path"` BasePath string `json:"base-path"`
Cache cache_config Cache cache_config
Proxy, Nsfw bool Proxy, Nsfw bool
WixmpProxy string `json:"wixmp-proxy"` WixmpProxy string `json:"wixmp-proxy"`
TemplatesDir string `json:"templates-dir"`
} }
var CFG = config{ var CFG = config{
@ -27,11 +29,13 @@ var CFG = config{
Listen: "127.0.0.1:3003", Listen: "127.0.0.1:3003",
BasePath: "/", BasePath: "/",
Cache: cache_config{ Cache: cache_config{
Enabled: true, Enabled: true,
Path: "cache", Path: "cache",
UpdateInterval: 1,
}, },
Proxy: true, TemplatesDir: "html",
Nsfw: true, Proxy: true,
Nsfw: true,
} }
func ExecuteConfig() { func ExecuteConfig() {

View File

@ -19,7 +19,8 @@ import (
// парсинг темплейтов // парсинг темплейтов
func (s skunkyart) ExecuteTemplate(file string, data any) { func (s skunkyart) ExecuteTemplate(file string, data any) {
var buf strings.Builder var buf strings.Builder
tmp, e := template.ParseFiles(file) tmp := template.New(file)
tmp, e := tmp.Parse(Templates[file])
err(e) err(e)
err(tmp.Execute(&buf, &data)) err(tmp.Execute(&buf, &data))
wr(s.Writer, buf.String()) wr(s.Writer, buf.String())
@ -57,7 +58,9 @@ func (s skunkyart) ConvertDeviantArtUrlToSkunkyArt(url string) (output string) {
if len(url) > 32 && url[27:32] != "stash" { if len(url) > 32 && url[27:32] != "stash" {
url = url[27:] url = url[27:]
toart := strings.Index(url, "/art/") toart := strings.Index(url, "/art/")
output = UrlBuilder("post", url[:toart], url[toart+5:]) if toart != -1 {
output = UrlBuilder("post", url[:toart], url[toart+5:])
}
} }
return return
} }
@ -261,7 +264,7 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
if url != "" { if url != "" {
list.WriteString(`<a title="open/download" href="`) list.WriteString(`<a title="open/download" href="`)
list.WriteString(url) list.WriteString(url)
list.WriteString(`"><img src="`) list.WriteString(`"><img loading="lazy" src="`)
list.WriteString(url) list.WriteString(url)
list.WriteString(`" width="15%"></a>`) list.WriteString(`" width="15%"></a>`)
} else { } else {
@ -296,7 +299,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
return "" return ""
} else { } else {
list.WriteString("</div>") list.WriteString("</div>")
list.WriteString(s.NavBase(content[0])) if content != nil {
list.WriteString(s.NavBase(content[0]))
}
} }
return list.String() return list.String()
@ -425,6 +430,7 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
b, _, _ := download() b, _, _ := download()
s.Writer.Write(b) s.Writer.Write(b)
} else { } else {
s.Writer.WriteHeader(403)
s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled.")) s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled."))
} }
} }
@ -441,6 +447,9 @@ func InitCacheSystem() {
err(e) err(e)
for _, a := range dirnames { for _, a := range dirnames {
a = c.Path + "/" + a a = c.Path + "/" + a
rm := func() {
err(os.RemoveAll(a))
}
if c.Lifetime != 0 { if c.Lifetime != 0 {
now := time.Now().UnixMilli() now := time.Now().UnixMilli()
@ -449,15 +458,33 @@ func InitCacheSystem() {
time := time.Unix(stat.Ctim.Unix()).UnixMilli() time := time.Unix(stat.Ctim.Unix()).UnixMilli()
if time+c.Lifetime <= now { if time+c.Lifetime <= now {
os.RemoveAll(a) rm()
} }
} }
if c.MaxSize != 0 && stat.Size() > c.MaxSize { if c.MaxSize != 0 && stat.Size() > c.MaxSize {
os.RemoveAll(a) rm()
} }
} }
dir.Close() 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 wr = io.WriteString
var Templates = make(map[string]string)
type skunkyart struct { type skunkyart struct {
Writer http.ResponseWriter Writer http.ResponseWriter
@ -31,6 +32,7 @@ type skunkyart struct {
SomeList string SomeList string
Deviation struct { Deviation struct {
Post devianter.Post Post devianter.Post
Related string
StringTime string StringTime string
Tags string Tags string
Comments 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() { func (s skunkyart) GRUser() {
if len(s.Query) < 1 { if len(s.Query) < 1 {
s.ReturnHTTPError(400) s.ReturnHTTPError(400)
@ -217,6 +206,12 @@ func (s skunkyart) Deviation(author, postname string) {
post.Post.Description = ParseDescription(post.Post.Deviation.TextContent) post.Post.Description = ParseDescription(post.Post.Deviation.TextContent)
// время публикации // время публикации
post.StringTime = post.Post.Deviation.PublishedTime.UTC().String() 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 { for _, x := range post.Post.Deviation.Extended.Tags {

View File

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

View File

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

View File

@ -18,7 +18,7 @@
</header> </header>
{{if ne .Templates.Search.List ""}} {{if ne .Templates.Search.List ""}}
{{if ne .Templates.Search.Content.Total 0}} {{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}} {{end}}
{{.Templates.Search.List}} {{.Templates.Search.List}}
{{else}} {{else}}

View File

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