From fbe39f45a34c4d9ebb85453351d9b5f643672a16 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Mon, 31 Jan 2022 18:56:21 +0200 Subject: [PATCH] Feature - Components refactor (#10) * feat: header component * feat: captcha component * feat: rename all html files * feat: update github link * feat: all templates in subfolders * feat: separate thread card * feat: separate pagination * feat: update pagination * feat: separate post * lint --- controllers/captcha_controller.go | 2 +- controllers/threads_controller.go | 8 ++-- main.go | 2 +- repositories/structs.go | 16 ++++++- templates/components/captcha.html | 7 +++ templates/components/footer.html | 9 ++++ templates/components/header.html | 17 +++++++ templates/components/pagiantion.html | 12 +++++ templates/components/post-form.html | 46 +++++++++++++++++++ templates/components/post.html | 20 +++++++++ templates/components/thread-card.html | 26 +++++++++++ templates/footer.html | 9 ---- templates/head/meta-tags-dynamic.html | 31 +++++++++++++ templates/head/meta-tags-static.html | 22 +++++++++ templates/head/static.html | 16 +++++++ templates/index.html | 64 --------------------------- templates/meta-tags-dynamic.html | 31 ------------- templates/meta-tags-static.html | 22 --------- templates/{ => pages}/400.html | 0 templates/{ => pages}/404.html | 0 templates/{ => pages}/500.html | 0 templates/pages/index.html | 28 ++++++++++++ templates/pages/thread.html | 29 ++++++++++++ templates/post-form.html | 50 --------------------- templates/static.html | 16 ------- templates/thread.html | 56 ----------------------- 26 files changed, 284 insertions(+), 255 deletions(-) create mode 100644 templates/components/captcha.html create mode 100644 templates/components/footer.html create mode 100644 templates/components/header.html create mode 100644 templates/components/pagiantion.html create mode 100644 templates/components/post-form.html create mode 100644 templates/components/post.html create mode 100644 templates/components/thread-card.html delete mode 100644 templates/footer.html create mode 100644 templates/head/meta-tags-dynamic.html create mode 100644 templates/head/meta-tags-static.html create mode 100644 templates/head/static.html delete mode 100644 templates/index.html delete mode 100644 templates/meta-tags-dynamic.html delete mode 100644 templates/meta-tags-static.html rename templates/{ => pages}/400.html (100%) rename templates/{ => pages}/404.html (100%) rename templates/{ => pages}/500.html (100%) create mode 100644 templates/pages/index.html create mode 100644 templates/pages/thread.html delete mode 100644 templates/post-form.html delete mode 100644 templates/static.html delete mode 100644 templates/thread.html diff --git a/controllers/captcha_controller.go b/controllers/captcha_controller.go index fbc27ad..8ffd763 100644 --- a/controllers/captcha_controller.go +++ b/controllers/captcha_controller.go @@ -15,7 +15,7 @@ func GetCaptcha(c *gin.Context) { err := captcha.WriteImage(&content, ID, captcha.StdWidth, captcha.StdHeight) if err != nil { log.Println("error:", err) - c.HTML(http.StatusInternalServerError, "500.html", nil) + c.HTML(http.StatusInternalServerError, "pages/500.html", nil) return } diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index a357da3..c622b07 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -54,9 +54,11 @@ func GetThreads(c *gin.Context) { captchaID := captcha.New() htmlData := Repositories.GetThreadsHtmlData{ - Threads: threads, - PagesCount: pagesCount, - Page: page, + Threads: threads, + Pagination: Repositories.HtmlPaginationData{ + PagesCount: pagesCount, + Page: page, + }, FormData: Repositories.HtmlFormData{ CaptchaID: captchaID, IsCaptchaActive: Config.App.IsCaptchaActive, diff --git a/main.go b/main.go index 4e67f2f..fe46587 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ func main() { return Items }, }) - router.LoadHTMLGlob("templates/*.html") + router.LoadHTMLGlob("templates/**/*") router.ForwardedByClientIP = true if Config.App.IsRateLimiterEnabled { router.Use(middleware) diff --git a/repositories/structs.go b/repositories/structs.go index 38cda47..164d677 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -19,6 +19,13 @@ type Post struct { UpdatedAt time.Time `json:"-"` } +func (p *Post) getThreadID() int { + if p.IsParent { + return p.ID + } + return p.ParentID +} + type File struct { ID int `json:"-"` PostID int `json:"-"` @@ -39,6 +46,12 @@ type HtmlFormData struct { IsCaptchaActive bool } +// index.html +type HtmlPaginationData struct { + PagesCount int + Page int +} + // thread.html type GetThreadHtmlData struct { Thread []Post @@ -48,8 +61,7 @@ type GetThreadHtmlData struct { // index.html type GetThreadsHtmlData struct { Threads []Post `json:"threads"` - PagesCount int `json:"pagesCount"` - Page int `json:"page"` + Pagination HtmlPaginationData FormData HtmlFormData } diff --git a/templates/components/captcha.html b/templates/components/captcha.html new file mode 100644 index 0000000..f820dc6 --- /dev/null +++ b/templates/components/captcha.html @@ -0,0 +1,7 @@ +{{ define "captcha" }} +
+ Captcha +
+ + +{{ end }} diff --git a/templates/components/footer.html b/templates/components/footer.html new file mode 100644 index 0000000..b8613a3 --- /dev/null +++ b/templates/components/footer.html @@ -0,0 +1,9 @@ +{{ define "footer" }} + +{{ end }} diff --git a/templates/components/header.html b/templates/components/header.html new file mode 100644 index 0000000..c8d78db --- /dev/null +++ b/templates/components/header.html @@ -0,0 +1,17 @@ +{{ define "header" }} + +{{ if . }} + +

+ {{ if ne . "" }} + {{ . }} + {{ else }} + Welcome to Micrach + {{ end }} +

+
+{{ else }} +

Welcome to Micrach

+{{ end }} + +{{ end }} diff --git a/templates/components/pagiantion.html b/templates/components/pagiantion.html new file mode 100644 index 0000000..813f5f9 --- /dev/null +++ b/templates/components/pagiantion.html @@ -0,0 +1,12 @@ +{{ define "pagiantion" }} + +{{ end }} diff --git a/templates/components/post-form.html b/templates/components/post-form.html new file mode 100644 index 0000000..0bde441 --- /dev/null +++ b/templates/components/post-form.html @@ -0,0 +1,46 @@ +{{ define "post-form" }} +
+ +
+ {{ if eq .FirstPostID 0 }} +
+ {{ else }} + + {{ end }} + + {{ if eq .FirstPostID 0 }} + + {{ end }} + + + + + {{ if .IsCaptchaActive }} + {{ template "captcha" .CaptchaID }} + {{ end }} + +
+ {{ if ne .FirstPostID 0 }} +
+ + +
+ {{ end }} + +
+ +
+
+
+
+ +
+{{ end }} diff --git a/templates/components/post.html b/templates/components/post.html new file mode 100644 index 0000000..41b8781 --- /dev/null +++ b/templates/components/post.html @@ -0,0 +1,20 @@ +{{ define "post" }} +
+ {{ $filesLength := len .Files }} + {{ if gt $filesLength 0 }} +
+ {{ range $File := .Files }} +
+ + Uploaded picture + +
+ {{ end }} +
+ {{ end }} + +
+

{{.Text}}

+
+
+{{ end }} \ No newline at end of file diff --git a/templates/components/thread-card.html b/templates/components/thread-card.html new file mode 100644 index 0000000..1a86f1c --- /dev/null +++ b/templates/components/thread-card.html @@ -0,0 +1,26 @@ +{{ define "thread-card" }} +
+
+ {{ $length := len .Files }} + {{ if gt $length 0 }} + {{ $FirstFile := index .Files 0 }} + Uploaded picture + {{ end }} + +
+ {{ if ne .Title "" }} +
{{.Title}}
+ {{ end }} + + {{ $textLength := len .Text }} + {{ if gt $textLength 300 }} +

{{ slice .Text 0 300 }} ...

+ {{ else }} +

{{.Text}}

+ {{ end }} + + Open +
+
+
+{{ end }} diff --git a/templates/footer.html b/templates/footer.html deleted file mode 100644 index fe5f996..0000000 --- a/templates/footer.html +++ /dev/null @@ -1,9 +0,0 @@ -{{ define "footer" }} - -{{ end }} diff --git a/templates/head/meta-tags-dynamic.html b/templates/head/meta-tags-dynamic.html new file mode 100644 index 0000000..91707d4 --- /dev/null +++ b/templates/head/meta-tags-dynamic.html @@ -0,0 +1,31 @@ +{{ define "meta-tags-dynamic" }} + + {{ $SeoImageUrl := "" }} + {{ if .Files }} + {{ $FirstFile := index .Files 0 }} + {{ $SeoImageUrl = printf "https://micrach.igonin.dev/uploads/%d/t/%d.%s" .ID $FirstFile.ID $FirstFile.Ext }} + {{ else }} + {{ $SeoImageUrl = "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png" }} + {{ end }} + + {{ .Title }} + + + + + + + + + + + + + + + + + + + +{{ end }} \ No newline at end of file diff --git a/templates/head/meta-tags-static.html b/templates/head/meta-tags-static.html new file mode 100644 index 0000000..28be81e --- /dev/null +++ b/templates/head/meta-tags-static.html @@ -0,0 +1,22 @@ +{{ define "meta-tags-static" }} + Micrach + + + + + + + + + + + + + + + + + + + +{{ end }} \ No newline at end of file diff --git a/templates/head/static.html b/templates/head/static.html new file mode 100644 index 0000000..a7cf3d6 --- /dev/null +++ b/templates/head/static.html @@ -0,0 +1,16 @@ +{{ define "static" }} + + + + + +{{ end }} diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index 11eb087..0000000 --- a/templates/index.html +++ /dev/null @@ -1,64 +0,0 @@ - - - - - {{ template "static" }} - - {{ template "meta-tags-static" }} - - - -

Welcome to Micrach

- -
- {{ template "post-form" .FormData }} - -
- {{ range $Post := .Threads }} -
-
- - {{ $length := len $Post.Files }} - {{ if gt $length 0 }} - {{ $FirstFile := index $Post.Files 0 }} - Uploaded picture - {{ end }} - -
- {{ if ne $Post.Title "" }} -
{{$Post.Title}}
- {{ end }} - - {{ $textLength := len $Post.Text }} - {{ if gt $textLength 300 }} -

{{ slice $Post.Text 0 300 }} ...

- {{ else }} -

{{$Post.Text}}

- {{ end }} - - Open -
-
-
- {{ end }} -
- -
- -
-
- - {{ template "footer" }} - - - \ No newline at end of file diff --git a/templates/meta-tags-dynamic.html b/templates/meta-tags-dynamic.html deleted file mode 100644 index 732525c..0000000 --- a/templates/meta-tags-dynamic.html +++ /dev/null @@ -1,31 +0,0 @@ -{{ define "meta-tags-dynamic" }} - -{{ $SeoImageUrl := "" }} -{{ if .Files }} - {{ $FirstFile := index .Files 0 }} - {{ $SeoImageUrl = printf "https://micrach.igonin.dev/uploads/%d/t/%d.%s" .ID $FirstFile.ID $FirstFile.Ext }} -{{ else }} - {{ $SeoImageUrl = "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png" }} -{{ end }} - -{{ .Title }} - - - - - - - - - - - - - - - - - - - -{{ end }} \ No newline at end of file diff --git a/templates/meta-tags-static.html b/templates/meta-tags-static.html deleted file mode 100644 index 25d2fed..0000000 --- a/templates/meta-tags-static.html +++ /dev/null @@ -1,22 +0,0 @@ -{{ define "meta-tags-static" }} -Micrach - - - - - - - - - - - - - - - - - - - -{{ end }} \ No newline at end of file diff --git a/templates/400.html b/templates/pages/400.html similarity index 100% rename from templates/400.html rename to templates/pages/400.html diff --git a/templates/404.html b/templates/pages/404.html similarity index 100% rename from templates/404.html rename to templates/pages/404.html diff --git a/templates/500.html b/templates/pages/500.html similarity index 100% rename from templates/500.html rename to templates/pages/500.html diff --git a/templates/pages/index.html b/templates/pages/index.html new file mode 100644 index 0000000..32a1204 --- /dev/null +++ b/templates/pages/index.html @@ -0,0 +1,28 @@ + + + + + {{ template "static" }} + + {{ template "meta-tags-static" }} + + + + {{ template "header" }} + +
+ {{ template "post-form" .FormData }} + +
+ {{ range $Post := .Threads }} + {{ template "thread-card" $Post }} + {{ end }} +
+ + {{ template "pagiantion" .Pagination }} +
+ + {{ template "footer" }} + + + \ No newline at end of file diff --git a/templates/pages/thread.html b/templates/pages/thread.html new file mode 100644 index 0000000..4671a2f --- /dev/null +++ b/templates/pages/thread.html @@ -0,0 +1,29 @@ +{{ $FirstPost:= index .Thread 0 }} + + + + + + {{ template "static" }} + {{ template "meta-tags-dynamic" $FirstPost }} + + + + + + {{ template "header" $FirstPost.Title }} + +
+ {{ template "post-form" .FormData }} + +
+ {{ range $Post := .Thread }} + {{ template "post" $Post }} + {{ end }} +
+
+ + {{ template "footer" }} + + + \ No newline at end of file diff --git a/templates/post-form.html b/templates/post-form.html deleted file mode 100644 index 8c7bea9..0000000 --- a/templates/post-form.html +++ /dev/null @@ -1,50 +0,0 @@ -{{ define "post-form" }} -
- -
- {{ if eq .FirstPostID 0 }} -
- {{ else }} - - {{ end }} - - {{ if eq .FirstPostID 0 }} - - {{ end }} - - - - - {{ if .IsCaptchaActive }} -
- Captcha -
- - - {{ end }} - -
- {{ if ne .FirstPostID 0 }} -
- - -
- {{ end }} - -
- -
-
-
-
- -
-{{ end }} diff --git a/templates/static.html b/templates/static.html deleted file mode 100644 index 0f48603..0000000 --- a/templates/static.html +++ /dev/null @@ -1,16 +0,0 @@ -{{ define "static" }} - - - - - -{{ end }} diff --git a/templates/thread.html b/templates/thread.html deleted file mode 100644 index 153e65e..0000000 --- a/templates/thread.html +++ /dev/null @@ -1,56 +0,0 @@ -{{ $FirstPost:= index .Thread 0 }} - - - - - - {{ template "static" }} - {{ template "meta-tags-dynamic" $FirstPost }} - - - - - - -

- {{ if ne $FirstPost.Title "" }} - {{$FirstPost.Title}} - {{ else }} - Welcome to Micrach - {{ end }} -

-
- -
- {{ template "post-form" .FormData }} - -
- {{ range $Post := .Thread }} -
- - {{ $filesLength := len $Post.Files }} - {{ if gt $filesLength 0 }} -
- {{ range $File := $Post.Files }} -
- - Uploaded picture - -
- {{ end }} -
- {{ end }} - -
-

{{$Post.Text}}

-
- -
- {{end}} -
-
- - {{ template "footer" }} - - - \ No newline at end of file