summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorlost+skunk <[email protected]>2024-07-06 20:06:04 +0300
committerlost+skunk <[email protected]>2024-07-06 20:06:04 +0300
commitff4f1a2422b3e02917af1f1786ff5b3ac09565ef (patch)
tree7da4260b98b23be4852d6196b07d1cfbdc115034 /app
parent7f1ab1b73d1e7a0d8a730354bbc3136c5584fea3 (diff)
downloadskunky-art-ff4f1a2422b3e02917af1f1786ff5b3ac09565ef.tar.gz
skunky-art-ff4f1a2422b3e02917af1f1786ff5b3ac09565ef.tar.bz2
skunky-art-ff4f1a2422b3e02917af1f1786ff5b3ac09565ef.zip
Реки и копирование темплейтов в озу
Diffstat (limited to 'app')
-rw-r--r--app/config.go32
-rw-r--r--app/util.go41
-rw-r--r--app/wrapper.go (renamed from app/wraper.go)21
3 files changed, 60 insertions, 34 deletions
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(`<a title="open/download" href="`)
list.WriteString(url)
- list.WriteString(`"><img src="`)
+ list.WriteString(`"><img loading="lazy" src="`)
list.WriteString(url)
list.WriteString(`" width="15%"></a>`)
} else {
@@ -296,7 +299,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, content ...dlist) s
return ""
} else {
list.WriteString("</div>")
- 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
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 {