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 (
7) 7)
8 8
9type cache_config struct { 9type cache_config struct {
10 Enabled bool 10 Enabled bool
11 Path string 11 Path string
12 MaxSize int64 `json:"max-size"` 12 MaxSize int64 `json:"max-size"`
13 Lifetime int64 13 Lifetime int64
14 UpdateInterval int64 `json:"update-interval"`
14} 15}
15 16
16type config struct { 17type config struct {
17 cfg string 18 cfg string
18 Listen string 19 Listen string
19 BasePath string `json:"base-path"` 20 BasePath string `json:"base-path"`
20 Cache cache_config 21 Cache cache_config
21 Proxy, Nsfw bool 22 Proxy, Nsfw bool
22 WixmpProxy string `json:"wixmp-proxy"` 23 WixmpProxy string `json:"wixmp-proxy"`
24 TemplatesDir string `json:"templates-dir"`
23} 25}
24 26
25var CFG = config{ 27var CFG = config{
@@ -27,11 +29,13 @@ var CFG = config{
27 Listen: "127.0.0.1:3003", 29 Listen: "127.0.0.1:3003",
28 BasePath: "/", 30 BasePath: "/",
29 Cache: cache_config{ 31 Cache: cache_config{
30 Enabled: true, 32 Enabled: true,
31 Path: "cache", 33 Path: "cache",
34 UpdateInterval: 1,
32 }, 35 },
33 Proxy: true, 36 TemplatesDir: "html",
34 Nsfw: true, 37 Proxy: true,
38 Nsfw: true,
35} 39}
36 40
37func ExecuteConfig() { 41func ExecuteConfig() {
app/util.go +34 −7
@@ -19,7 +19,8 @@ import (
19// парсинг темплейтов 19// парсинг темплейтов
20func (s skunkyart) ExecuteTemplate(file string, data any) { 20func (s skunkyart) ExecuteTemplate(file string, data any) {
21 var buf strings.Builder 21 var buf strings.Builder
22 tmp, e := template.ParseFiles(file) 22 tmp := template.New(file)
23 tmp, e := tmp.Parse(Templates[file])
23 err(e) 24 err(e)
24 err(tmp.Execute(&buf, &data)) 25 err(tmp.Execute(&buf, &data))
25 wr(s.Writer, buf.String()) 26 wr(s.Writer, buf.String())
@@ -57,7 +58,9 @@ func (s skunkyart) ConvertDeviantArtUrlToSkunkyArt(url string) (output string) {
57 if len(url) > 32 && url[27:32] != "stash" { 58 if len(url) > 32 && url[27:32] != "stash" {
58 url = url[27:] 59 url = url[27:]
59 toart := strings.Index(url, "/art/") 60 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 }
61 } 64 }
62 return 65 return
63} 66}
@@ -261,7 +264,7 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
261 if url != "" { 264 if url != "" {
262 list.WriteString(`<a title="open/download" href="`) 265 list.WriteString(`<a title="open/download" href="`)
263 list.WriteString(url) 266 list.WriteString(url)
264 list.WriteString(`"><img src="`) 267 list.WriteString(`"><img loading="lazy" src="`)
265 list.WriteString(url) 268 list.WriteString(url)
266 list.WriteString(`" width="15%"></a>`) 269 list.WriteString(`" width="15%"></a>`)
267 } else { 270 } else {
@@ -296,7 +299,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
296 return "" 299 return ""
297 } else { 300 } else {
298 list.WriteString("</div>") 301 list.WriteString("</div>")
299 list.WriteString(s.NavBase(content[0])) 302 if content != nil {
303 list.WriteString(s.NavBase(content[0]))
304 }
300 } 305 }
301 306
302 return list.String() 307 return list.String()
@@ -425,6 +430,7 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) {
425 b, _, _ := download() 430 b, _, _ := download()
426 s.Writer.Write(b) 431 s.Writer.Write(b)
427 } else { 432 } else {
433 s.Writer.WriteHeader(403)
428 s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled.")) 434 s.Writer.Write([]byte("Sorry, butt proxy on this instance disabled."))
429 } 435 }
430} 436}
@@ -441,6 +447,9 @@ func InitCacheSystem() {
441 err(e) 447 err(e)
442 for _, a := range dirnames { 448 for _, a := range dirnames {
443 a = c.Path + "/" + a 449 a = c.Path + "/" + a
450 rm := func() {
451 err(os.RemoveAll(a))
452 }
444 if c.Lifetime != 0 { 453 if c.Lifetime != 0 {
445 now := time.Now().UnixMilli() 454 now := time.Now().UnixMilli()
446 455
@@ -449,15 +458,33 @@ func InitCacheSystem() {
449 time := time.Unix(stat.Ctim.Unix()).UnixMilli() 458 time := time.Unix(stat.Ctim.Unix()).UnixMilli()
450 459
451 if time+c.Lifetime <= now { 460 if time+c.Lifetime <= now {
452 os.RemoveAll(a) 461 rm()
453 } 462 }
454 } 463 }
455 if c.MaxSize != 0 && stat.Size() > c.MaxSize { 464 if c.MaxSize != 0 && stat.Size() > c.MaxSize {
456 os.RemoveAll(a) 465 rm()
457 } 466 }
458 } 467 }
459 468
460 dir.Close() 469 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)
462 } 489 }
463} 490}
app/wraper.go → app/wrapper.go renamed +8 −13
@@ -13,6 +13,7 @@ import (
13) 13)
14 14
15var wr = io.WriteString 15var wr = io.WriteString
16var Templates = make(map[string]string)
16 17
17type skunkyart struct { 18type skunkyart struct {
18 Writer http.ResponseWriter 19 Writer http.ResponseWriter
@@ -31,6 +32,7 @@ type skunkyart struct {
31 SomeList string 32 SomeList string
32 Deviation struct { 33 Deviation struct {
33 Post devianter.Post 34 Post devianter.Post
35 Related string
34 StringTime string 36 StringTime string
35 Tags string 37 Tags string
36 Comments string 38 Comments string
@@ -65,19 +67,6 @@ type skunkyart struct {
65 } 67 }
66} 68}
67 69
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
81func (s skunkyart) GRUser() { 70func (s skunkyart) GRUser() {
82 if len(s.Query) < 1 { 71 if len(s.Query) < 1 {
83 s.ReturnHTTPError(400) 72 s.ReturnHTTPError(400)
@@ -217,6 +206,12 @@ func (s skunkyart) Deviation(author, postname string) {
217 post.Post.Description = ParseDescription(post.Post.Deviation.TextContent) 206 post.Post.Description = ParseDescription(post.Post.Deviation.TextContent)
218 // время публикации 207 // время публикации
219 post.StringTime = post.Post.Deviation.PublishedTime.UTC().String() 208 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 }
220 215
221 // хештэги 216 // хештэги
222 for _, x := range post.Post.Deviation.Extended.Tags { 217 for _, x := range post.Post.Deviation.Extended.Tags {
config.example.json +5 −3
@@ -1,13 +1,15 @@
1{ 1{
2 "listen": "0.0.0.0:3003", 2 "listen": "0.0.0.0:3003",
3 "base-path": "/skunky/", 3 "base-path": "/",
4 "cache": { 4 "cache": {
5 "enabled": true, 5 "enabled": true,
6 "path": "cache", 6 "path": "cache",
7 "lifetime": null, 7 "lifetime": null,
8 "max-size": 100000 8 "max-size": 100000,
9 "update-interval": 5
9 }, 10 },
10 "proxy": true, 11 "templates-dir": "html",
11 "wixmp-proxy": "http://127.0.0.1:8080", 12 "wixmp-proxy": "http://127.0.0.1:8080",
13 "proxy": true,
12 "nsfw": false 14 "nsfw": false
13}\ No newline at end of file 15}\ No newline at end of file
html/deviantion.htm +6
@@ -38,6 +38,12 @@
38 {{.Templates.Deviation.Post.Description}} 38 {{.Templates.Deviation.Post.Description}}
39 </figcaption> 39 </figcaption>
40 {{end}} 40 {{end}}
41 {{if ne .Templates.Deviation.Related ""}}
42 <details>
43 <summary>Related content</summary>
44 {{.Templates.Deviation.Related}}
45 </details>
46 {{end}}
41 {{if (ne .Templates.Deviation.Comments "")}} 47 {{if (ne .Templates.Deviation.Comments "")}}
42 {{.Templates.Deviation.Comments}} 48 {{.Templates.Deviation.Comments}}
43 {{end}} 49 {{end}}
html/search.htm +1 −1
@@ -18,7 +18,7 @@
18 </header> 18 </header>
19 {{if ne .Templates.Search.List ""}} 19 {{if ne .Templates.Search.List ""}}
20 {{if ne .Templates.Search.Content.Total 0}} 20 {{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>
22 {{end}} 22 {{end}}
23 {{.Templates.Search.List}} 23 {{.Templates.Search.List}}
24 {{else}} 24 {{else}}
main.go +1
@@ -8,6 +8,7 @@ import (
8 8
9func main() { 9func main() {
10 app.ExecuteConfig() 10 app.ExecuteConfig()
11 app.CopyTemplatesToMemory()
11 err := devianter.UpdateCSRF() 12 err := devianter.UpdateCSRF()
12 if err != nil { 13 if err != nil {
13 println(err.Error()) 14 println(err.Error())