Optimization: Pass in queries in an organized way #9

This commit is contained in:
VnPower 2023-07-05 21:29:55 +07:00
parent da26255411
commit 7765343913
Signed by: vnpower
GPG key ID: 881DE3DEB966106C
7 changed files with 56 additions and 83 deletions

4
go.mod
View file

@ -4,7 +4,7 @@ go 1.20
require (
github.com/goccy/go-json v0.10.2
github.com/gofiber/fiber/v2 v2.46.0
github.com/gofiber/fiber/v2 v2.47.0
github.com/gofiber/template/jet/v2 v2.1.3
)
@ -27,5 +27,5 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.47.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.9.0 // indirect
)

4
go.sum
View file

@ -8,6 +8,8 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofiber/fiber/v2 v2.46.0 h1:wkkWotblsGVlLjXj2dpgKQAYHtXumsK/HyFugQM68Ns=
github.com/gofiber/fiber/v2 v2.46.0/go.mod h1:DNl0/c37WLe0g92U6lx1VMQuxGUQY5V7EIaVoEsUffc=
github.com/gofiber/fiber/v2 v2.47.0 h1:EN5lHVCc+Pyqh5OEsk8fzRiifgwpbrP0rulQ4iNf3fs=
github.com/gofiber/fiber/v2 v2.47.0/go.mod h1:mbFMVN1lQuzziTkkakgtKKdjfsXSw9BKR5lmcNksUoU=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
github.com/gofiber/template/jet/v2 v2.1.3 h1:l/mDuBrJAG1z2sPNQ8/Fn8PRX+6ywhhNCtEqUHEPpAE=
@ -75,6 +77,8 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=

View file

@ -40,18 +40,10 @@ func GetRandomColor() string {
func GetTemplateFunctions() template.FuncMap {
return template.FuncMap{
"inc": func(n int) int {
// For rankings to increment a number by 1
return n + 1
},
"add": func(a int, b int) int {
return a + b
},
"dec": func(n int) int {
return n - 1
},
"toInt": func(s string) int {
n, _ := strconv.Atoi(s)
return n

26
main.go
View file

@ -96,32 +96,6 @@ func setup_router() *fiber.App {
return c.Next()
})
// server.Use(func(c *fiber.Ctx) error {
// sess, err := configs.Store.Get(c)
// if err != nil {
// return err
// }
// var token_string, image_string string
// token := sess.Get("token")
// if token != nil {
// token_string = token.(string)
// } else {
// token_string = configs.ProxyServer
// }
// image := sess.Get("image-proxy")
// if image != nil {
// image_string = image.(string)
// }
// c.Bind(fiber.Map{
// "Token": token_string,
// "ImageProxy": image_string,
// })
// return c.Next()
// })
// Static files
server.Static("/favicon.ico", "./template/favicon.ico")

View file

@ -2,7 +2,7 @@
<h2>{{ Title }}</h2>
<div class="switcher">
{{ url := "/ranking?mode=" + Mode + "&page=1&content=" }}
{{ url := "/ranking?mode=" + Queries.Mode + "&page=1&content=" }}
<span class="switch-title">Content</span>
<a href="{{ url }}all" class="switch-button">Overall</a>
<a href="{{ url }}illust" class="switch-button">Illustrations</a>
@ -11,7 +11,7 @@
<br />
<div class="switcher">
{{ url := "/ranking?content=" + Content + "&page=1&mode=" }}
{{ url := "/ranking?content=" + Queries.Content + "&page=1&mode=" }}
<span class="switch-title">Modes</span>
<a href="{{ url }}daily" class="switch-button">Daily</a>
<a href="{{ url }}weekly" class="switch-button">Weekly</a>
@ -36,29 +36,35 @@
<a class="artwork-thumbnail-title" href="/artworks/{{ .ID }}">
<h3 class="no-margin">{{ .Title }}</h3>
</a>
<a href="/users/{{ .ArtistID }}" class="artwork-thumbnail-artist flex"><img src="{{ .ArtistAvatar }}"
alt="{{ .ArtistName }}" class="artwork-thumbnail-artist-avatar border-rounded" />
{{ .ArtistName }}</a>
<a href="/users/{{ .ArtistID }}" class="artwork-thumbnail-artist flex"
><img
src="{{ .ArtistAvatar }}"
alt="{{ .ArtistName }}"
class="artwork-thumbnail-artist-avatar border-rounded"
/>
{{ .ArtistName }}</a
>
</div>
{{ end }}
<div class="pagination">
{{ url := "/ranking?content=" + Content + "&mode=" + Mode + "&page=" }} {{
if Page <= 1 }} <a href="#" class="pagination-button disabled">First</a>
<a href="#" class="pagination-button disabled">Previous</a>
{{ else }}
<a href="{{ url }}1" class="pagination-button">First</a>
<a href="{{ url }}{{ Page - 1 }}" class="pagination-button">Previous</a>
{{ end }}
{{ url := "/ranking?content=" + Queries.Content + "&mode=" + Queries.Mode +
"&page=" }} {{ if Page <= 1 }}
<a href="#" class="pagination-button disabled">First</a>
<a href="#" class="pagination-button disabled">Previous</a>
{{ else }}
<a href="{{ url }}1" class="pagination-button">First</a>
<a href="{{ url }}{{ Page - 1 }}" class="pagination-button">Previous</a>
{{ end }}
<a href="#" class="pagination-button disabled">{{ Page }}</a>
<a href="#" class="pagination-button disabled">{{ Page }}</a>
{{ if Page == 10 }}
<a href="#" class="pagination-button disabled">Next</a>
<a href="#" class="pagination-button disabled">Last</a>
{{ else }}
<a href="{{ url }}{{ Page + 1 }}" class="pagination-button">Next</a>
<a href="{{ url }}10" class="pagination-button">Last</a>
{{ end }}
{{ if Page == 10 }}
<a href="#" class="pagination-button disabled">Next</a>
<a href="#" class="pagination-button disabled">Last</a>
{{ else }}
<a href="{{ url }}{{ Page + 1 }}" class="pagination-button">Next</a>
<a href="{{ url }}10" class="pagination-button">Last</a>
{{ end }}
</div>
</div>

View file

@ -2,7 +2,11 @@
<div class="tag-header">
{{ if Tag.Metadata.id }}
<a href="/artworks/{{ Tag.Metadata.id }}">
<img class="tag-thumbnail" src="{{ Tag.Metadata.image }}" alt="{{ Tag.Name }}" />
<img
class="tag-thumbnail"
src="{{ Tag.Metadata.image }}"
alt="{{ Tag.Name }}"
/>
</a>
{{ end }}
@ -60,17 +64,16 @@
<div class="pagination">
{{ url := "/tags/" + Tag.Name + "?category=" + Queries.Category + "&order="
+ Queries.Order + "&mode=" + Queries.Mode + "&page=" }} {{ if toInt
(Queries.Page) == 1 }}
+ Queries.Order + "&mode=" + Queries.Mode + "&page=" }} {{ if Page == 1 }}
<a href="#" class="pagination-button disabled">First</a>
<a href="#" class="pagination-button disabled">Previous</a>
{{ else }}
<a href="{{url}}1" class="pagination-button">First</a>
<a href="{{url}}{{ toInt(Queries.Page) - 1 }}" class="pagination-button">Previous</a>
<a href="{{url}}{{ Page - 1 }}" class="pagination-button">Previous</a>
{{ end }}
<a href="#" class="pagination-button disabled">{{ Queries.Page }}</a>
<a href="#" class="pagination-button disabled">{{ Page }}</a>
<a href="{{url}}{{ toInt(Queries.Page) + 1 }}" class="pagination-button">Next</a>
<a href="{{url}}{{ Page + 1 }}" class="pagination-button">Next</a>
</div>
</div>

View file

@ -131,15 +131,15 @@ func ranking_page(c *fiber.Ctx) error {
if image_proxy == nil {
image_proxy = &configs.ProxyServer
}
mode := c.Query("mode", "daily")
content := c.Query("content", "all")
queries := make(map[string]string, 3)
queries["Mode"] = c.Query("mode", "daily")
queries["Content"] = c.Query("content", "all")
page := c.Query("page", "1")
pageInt, _ := strconv.Atoi(page)
response, err := PC.GetRanking(mode, content, page)
response, err := PC.GetRanking(queries["Mode"], queries["Content"], page)
if err != nil {
return err
}
@ -154,9 +154,9 @@ func ranking_page(c *fiber.Ctx) error {
return c.Render("rank", fiber.Map{
"Title": "Ranking",
"Items": artworks,
"Mode": mode,
"Content": content,
"Page": pageInt})
"Queries": queries,
"Page": pageInt,
})
}
func newest_artworks_page(c *fiber.Ctx) error {
@ -188,15 +188,15 @@ func search_page(c *fiber.Ctx) error {
image_proxy = &configs.ProxyServer
}
queries := make(map[string]string, 3)
queries["Mode"] = c.Query("mode", "safe")
queries["Category"] = c.Query("category", "artworks")
queries["Order"] = c.Query("order", "date_d")
name := c.Params("name")
page := c.Query("page", "1")
order := c.Query("order", "date_d")
mode := c.Query("mode", "safe")
category := c.Query("category", "artworks")
pageInt, _ := strconv.Atoi(page)
tag, err := PC.GetTagData(name)
if err != nil {
@ -205,20 +205,14 @@ func search_page(c *fiber.Ctx) error {
if len(tag.Metadata) > 0 {
tag.Metadata["image"] = models.ProxyImage(tag.Metadata["image"], *image_proxy)
}
result, err := PC.GetSearch(category, name, order, mode, page)
result, err := PC.GetSearch(queries["Category"], name, queries["Order"], queries["Mode"], page)
if err != nil {
return err
}
result.ProxyImages(*image_proxy)
queries := map[string]string{
"Page": page,
"Order": order,
"Mode": mode,
"Category": category,
}
return c.Render("tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries})
return c.Render("tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries, "Page": pageInt})
}
func search(c *fiber.Ctx) error {