From ff4f1a2422b3e02917af1f1786ff5b3ac09565ef Mon Sep 17 00:00:00 2001 From: lost+skunk Date: Sat, 6 Jul 2024 20:06:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=BA=D0=B8=20=D0=B8=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BC=D0=BF=D0=BB=D0=B5=D0=B9=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D0=B7=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.go | 32 +++++++++++++++------------ app/util.go | 41 +++++++++++++++++++++++++++++------ app/{wraper.go => wrapper.go} | 21 +++++++----------- config.example.json | 8 ++++--- html/deviantion.htm | 6 +++++ html/search.htm | 2 +- main.go | 1 + 7 files changed, 73 insertions(+), 38 deletions(-) rename app/{wraper.go => wrapper.go} (96%) diff --git a/app/config.go b/app/config.go index 1df6aaf..18cea29 100644 --- a/app/config.go +++ b/app/config.go @@ -7,19 +7,21 @@ import ( ) type cache_config struct { - Enabled bool - Path string - MaxSize int64 `json:"max-size"` - Lifetime int64 + Enabled bool + Path string + MaxSize int64 `json:"max-size"` + Lifetime int64 + UpdateInterval int64 `json:"update-interval"` } type config struct { - cfg string - Listen string - BasePath string `json:"base-path"` - Cache cache_config - Proxy, Nsfw bool - WixmpProxy string `json:"wixmp-proxy"` + cfg string + Listen string + BasePath string `json:"base-path"` + Cache cache_config + Proxy, Nsfw bool + WixmpProxy string `json:"wixmp-proxy"` + TemplatesDir string `json:"templates-dir"` } var CFG = config{ @@ -27,11 +29,13 @@ var CFG = config{ Listen: "127.0.0.1:3003", BasePath: "/", Cache: cache_config{ - Enabled: true, - Path: "cache", + Enabled: true, + Path: "cache", + UpdateInterval: 1, }, - Proxy: true, - Nsfw: true, + TemplatesDir: "html", + Proxy: true, + Nsfw: true, } func ExecuteConfig() { diff --git a/app/util.go b/app/util.go index 9e1784d..e22e83b 100644 --- a/app/util.go +++ b/app/util.go @@ -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,7 +58,9 @@ func (s skunkyart) ConvertDeviantArtUrlToSkunkyArt(url string) (output string) { if len(url) > 32 && url[27:32] != "stash" { url = url[27:] 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 } @@ -261,7 +264,7 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s if url != "" { list.WriteString(``) } else { @@ -296,7 +299,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s return "" } else { list.WriteString("") - list.WriteString(s.NavBase(content[0])) + 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) } } diff --git a/app/wraper.go b/app/wrapper.go similarity index 96% rename from app/wraper.go rename to app/wrapper.go index f42a9af..006a02a 100644 --- a/app/wraper.go +++ b/app/wrapper.go @@ -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 { diff --git a/config.example.json b/config.example.json index 56b5218..2c3205d 100644 --- a/config.example.json +++ b/config.example.json @@ -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 } \ No newline at end of file diff --git a/html/deviantion.htm b/html/deviantion.htm index 7a7f282..97850d3 100644 --- a/html/deviantion.htm +++ b/html/deviantion.htm @@ -38,6 +38,12 @@ {{.Templates.Deviation.Post.Description}} {{end}} + {{if ne .Templates.Deviation.Related ""}} +
+ Related content + {{.Templates.Deviation.Related}} +
+ {{end}} {{if (ne .Templates.Deviation.Comments "")}} {{.Templates.Deviation.Comments}} {{end}} diff --git a/html/search.htm b/html/search.htm index 8cdcbc1..6644bfc 100644 --- a/html/search.htm +++ b/html/search.htm @@ -18,7 +18,7 @@ {{if ne .Templates.Search.List ""}} {{if ne .Templates.Search.Content.Total 0}} -

Total resuls: {{.Templates.Search.Content.Total}}

+

Results by request '{{.QueryRaw}}': {{.Templates.Search.Content.Total}}

{{end}} {{.Templates.Search.List}} {{else}} diff --git a/main.go b/main.go index 636c358..43b95f0 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( func main() { app.ExecuteConfig() + app.CopyTemplatesToMemory() err := devianter.UpdateCSRF() if err != nil { println(err.Error())