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