Commit ff4f1a2422

ff4f1a2422b3e02917af1f1786ff5b3ac09565ef

parent: 7f1ab1b73d

Unsigned

lost+skunk <skunky@ebloid.ru> · 2024-07-06 17:06 UTC

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

Layout: unified · split

app/config.go +18 −14
@@ -7,19 +7,21 @@ import (
77)
88
99type cache_config struct {
10 Enabled bool
11 Path string
12 MaxSize int64 `json:"max-size"`
13 Lifetime int64
10 Enabled bool
11 Path string
12 MaxSize int64 `json:"max-size"`
13 Lifetime int64
14 UpdateInterval int64 `json:"update-interval"`
1415}
1516
1617type config struct {
17 cfg string
18 Listen string
19 BasePath string `json:"base-path"`
20 Cache cache_config
21 Proxy, Nsfw bool
22 WixmpProxy string `json:"wixmp-proxy"`
18 cfg string
19 Listen string
20 BasePath string `json:"base-path"`
21 Cache cache_config
22 Proxy, Nsfw bool
23 WixmpProxy string `json:"wixmp-proxy"`
24 TemplatesDir string `json:"templates-dir"`
2325}
2426
2527var CFG = config{
@@ -27,11 +29,13 @@ var CFG = config{
2729 Listen: "127.0.0.1:3003",
2830 BasePath: "/",
2931 Cache: cache_config{
30 Enabled: true,
31 Path: "cache",
32 Enabled: true,
33 Path: "cache",
34 UpdateInterval: 1,
3235 },
33 Proxy: true,
34 Nsfw: true,
36 TemplatesDir: "html",
37 Proxy: true,
38 Nsfw: true,
3539}
3640
3741func ExecuteConfig() {
app/util.go +34 −7
@@ -19,7 +19,8 @@ import (
1919// парсинг темплейтов
2020func (s skunkyart) ExecuteTemplate(file string, data any) {
2121 var buf strings.Builder
22 tmp, e := template.ParseFiles(file)
22 tmp := template.New(file)
23 tmp, e := tmp.Parse(Templates[file])
2324 err(e)
2425 err(tmp.Execute(&buf, &data))
2526 wr(s.Writer, buf.String())
@@ -57,7 +58,9 @@ func (s skunkyart) ConvertDeviantArtUrlToSkunkyArt(url string) (output string) {
5758 if len(url) > 32 && url[27:32] != "stash" {
5859 url = url[27:]
5960 toart := strings.Index(url, "/art/")
60 output = UrlBuilder("post", url[:toart], url[toart+5:])
61 if toart != -1 {
62 output = UrlBuilder("post", url[:toart], url[toart+5:])
63 }
6164 }
6265 return
6366}
@@ -261,7 +264,7 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
261264 if url != "" {
262265 list.WriteString(`<a title="open/download" href="`)
263266 list.WriteString(url)
264 list.WriteString(`"><img src="`)
267 list.WriteString(`"><img loading="lazy" src="`)
265268 list.WriteString(url)
266269 list.WriteString(`" width="15%"></a>`)
267270 } else {
@@ -296,7 +299,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
296299 return ""
297300 } else {
298301 list.WriteString("</div>")
299 list.WriteString(s.NavBase(content[0]))
302 if content != nil {
303 list.WriteString(s.NavBase(content[0]))
304 }
300305 }
301306
302307 return list.String()
@@ -425,6 +430,7 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
425430 b, _, _ := download()
426431 s.Writer.Write(b)
427432 } else {
433 s.Writer.WriteHeader(403)
428434 s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled."))
429435 }
430436}
@@ -441,6 +447,9 @@ func InitCacheSystem() {
441447 err(e)
442448 for _, a := range dirnames {
443449 a = c.Path + "/" + a
450 rm := func() {
451 err(os.RemoveAll(a))
452 }
444453 if c.Lifetime != 0 {
445454 now := time.Now().UnixMilli()
446455
@@ -449,15 +458,33 @@ func InitCacheSystem() {
449458 time := time.Unix(stat.Ctim.Unix()).UnixMilli()
450459
451460 if time+c.Lifetime <= now {
452 os.RemoveAll(a)
461 rm()
453462 }
454463 }
455464 if c.MaxSize != 0 && stat.Size() > c.MaxSize {
456 os.RemoveAll(a)
465 rm()
457466 }
458467 }
459468
460469 dir.Close()
461 time.Sleep(time.Second)
470 time.Sleep(time.Second * time.Duration(CFG.Cache.UpdateInterval))
471 }
472}
473
474func CopyTemplatesToMemory() {
475 try := func(e error) {
476 if e != nil {
477 panic(e.Error())
478 }
479 }
480
481 dir, e := os.ReadDir(CFG.TemplatesDir)
482 try(e)
483
484 for _, x := range dir {
485 n := CFG.TemplatesDir + "/" + x.Name()
486 file, e := os.ReadFile(n)
487 try(e)
488 Templates[n] = string(file)
462489 }
463490}
app/wraper.go → app/wrapper.go renamed +8 −13
@@ -13,6 +13,7 @@ import (
1313)
1414
1515var wr = io.WriteString
16var Templates = make(map[string]string)
1617
1718type skunkyart struct {
1819 Writer http.ResponseWriter
@@ -31,6 +32,7 @@ type skunkyart struct {
3132 SomeList string
3233 Deviation struct {
3334 Post devianter.Post
35 Related string
3436 StringTime string
3537 Tags string
3638 Comments string
@@ -65,19 +67,6 @@ type skunkyart struct {
6567 }
6668}
6769
68// var Templates struct {
69// Index string
70// About string
71//
72// GRuser string
73// Deviation string
74// List string
75// Search string
76// }
77
78// //go:embed ../html/*
79// var Templates embed.FS
80
8170func (s skunkyart) GRUser() {
8271 if len(s.Query) < 1 {
8372 s.ReturnHTTPError(400)
@@ -217,6 +206,12 @@ func (s skunkyart) Deviation(author, postname string) {
217206 post.Post.Description = ParseDescription(post.Post.Deviation.TextContent)
218207 // время публикации
219208 post.StringTime = post.Post.Deviation.PublishedTime.UTC().String()
209 post.Post.IMG = s.ParseMedia(post.Post.Deviation.Media)
210 for _, x := range post.Post.Deviation.Extended.RelatedContent {
211 if len(x.Deviations) != 0 {
212 post.Related = s.DeviationList(x.Deviations)
213 }
214 }
220215
221216 // хештэги
222217 for _, x := range post.Post.Deviation.Extended.Tags {
config.example.json +5 −3
@@ -1,13 +1,15 @@
11{
22 "listen": "0.0.0.0:3003",
3 "base-path": "/skunky/",
3 "base-path": "/",
44 "cache": {
55 "enabled": true,
66 "path": "cache",
77 "lifetime": null,
8 "max-size": 100000
8 "max-size": 100000,
9 "update-interval": 5
910 },
10 "proxy": true,
11 "templates-dir": "html",
1112 "wixmp-proxy": "http://127.0.0.1:8080",
13 "proxy": true,
1214 "nsfw": false
1315}
\ No newline at end of file
html/deviantion.htm +6
@@ -38,6 +38,12 @@
3838 {{.Templates.Deviation.Post.Description}}
3939 </figcaption>
4040 {{end}}
41 {{if ne .Templates.Deviation.Related ""}}
42 <details>
43 <summary>Related content</summary>
44 {{.Templates.Deviation.Related}}
45 </details>
46 {{end}}
4147 {{if (ne .Templates.Deviation.Comments "")}}
4248 {{.Templates.Deviation.Comments}}
4349 {{end}}
html/search.htm +1 −1
@@ -18,7 +18,7 @@
1818 </header>
1919 {{if ne .Templates.Search.List ""}}
2020 {{if ne .Templates.Search.Content.Total 0}}
21 <h1>Total resuls: {{.Templates.Search.Content.Total}}</h1>
21 <h1>Results by request '{{.QueryRaw}}': {{.Templates.Search.Content.Total}}</h1>
2222 {{end}}
2323 {{.Templates.Search.List}}
2424 {{else}}
main.go +1
@@ -8,6 +8,7 @@ import (
88
99func main() {
1010 app.ExecuteConfig()
11 app.CopyTemplatesToMemory()
1112 err := devianter.UpdateCSRF()
1213 if err != nil {
1314 println(err.Error())