diff options
Diffstat (limited to 'processors')
| -rw-r--r-- | processors/processors.go | 2 | ||||
| -rw-r--r-- | processors/request.go | 45 | ||||
| -rw-r--r-- | processors/sidebar.go | 98 |
3 files changed, 145 insertions, 0 deletions
diff --git a/processors/processors.go b/processors/processors.go index 69fbbec..e060abe 100644 --- a/processors/processors.go +++ b/processors/processors.go @@ -3,5 +3,7 @@ package processors import "github.com/gofiber/fiber/v2"
func Initialize(app *fiber.App) {
+ app.Use(RequestContextProcessor)
app.Use(MetaContextProcessor)
+ app.Use(SidebarContextProcessor)
}
diff --git a/processors/request.go b/processors/request.go new file mode 100644 index 0000000..f4fe8d2 --- /dev/null +++ b/processors/request.go @@ -0,0 +1,45 @@ +package processors + +import ( + "github.com/gofiber/fiber/v2" +) + +type QueryParam struct { + Key string + Value string +} + +type Request struct { + Path string + Method string + Query []QueryParam + Params []QueryParam + QueryString string + IP string + URL string +} + +func RequestContextProcessor(ctx *fiber.Ctx) error { + queryParams := []QueryParam{} + for k, v := range ctx.Queries() { + queryParams = append(queryParams, QueryParam{Key: k, Value: v}) + } + + routeParams := []QueryParam{} + for k, v := range ctx.AllParams() { + routeParams = append(routeParams, QueryParam{Key: k, Value: v}) + } + + request := Request{ + Path: ctx.Path(), + Method: ctx.Method(), + Query: queryParams, + Params: routeParams, + QueryString: string(ctx.Request().URI().QueryString()), + IP: ctx.IP(), + URL: ctx.OriginalURL(), + } + + ctx.Locals("Request", request) + return ctx.Next() +} diff --git a/processors/sidebar.go b/processors/sidebar.go new file mode 100644 index 0000000..26138f1 --- /dev/null +++ b/processors/sidebar.go @@ -0,0 +1,98 @@ +package processors + +import ( + "fmt" + "imageboard/database" + "imageboard/models" + + "github.com/gofiber/fiber/v2" +) + +type SiteStats struct { + Posts string + Tags string + Today string + Storage string + Comments string +} + +func SidebarContextProcessor(ctx *fiber.Ctx) error { + popularTags, popularTagsErr := database.GetPopularTags(15) + if popularTagsErr != nil || len(popularTags) == 0 { + mockTags := []models.Tag{ + {Name: "anime", Type: models.TagTypeGeneral, Count: 1523}, + {Name: "manga", Type: models.TagTypeGeneral, Count: 892}, + {Name: "kawaii", Type: models.TagTypeGeneral, Count: 756}, + {Name: "retro", Type: models.TagTypeMeta, Count: 634}, + {Name: "y2k", Type: models.TagTypeMeta, Count: 511}, + {Name: "aesthetic", Type: models.TagTypeGeneral, Count: 445}, + {Name: "sakura", Type: models.TagTypeArtist, Count: 389}, + {Name: "studio_ghibli", Type: models.TagTypeCopyright, Count: 312}, + {Name: "totoro", Type: models.TagTypeCharacter, Count: 298}, + {Name: "sailor_moon", Type: models.TagTypeCharacter, Count: 267}, + {Name: "pokemon", Type: models.TagTypeCopyright, Count: 234}, + {Name: "pixiv", Type: models.TagTypeMeta, Count: 198}, + {Name: "digital_art", Type: models.TagTypeMeta, Count: 176}, + {Name: "watercolor", Type: models.TagTypeGeneral, Count: 145}, + {Name: "minimalist", Type: models.TagTypeGeneral, Count: 123}, + } + ctx.Locals("PopularTags", mockTags) + } else { + ctx.Locals("PopularTags", popularTags) + } + + recentTags, recentTagsErr := database.GetRecentTags(10) + if recentTagsErr != nil || len(recentTags) == 0 { + mockRecentTags := []models.Tag{ + {Name: "cyberpunk", Type: models.TagTypeGeneral, Count: 23}, + {Name: "vaporwave", Type: models.TagTypeMeta, Count: 45}, + {Name: "synthwave", Type: models.TagTypeGeneral, Count: 12}, + {Name: "retrocomputing", Type: models.TagTypeMeta, Count: 8}, + {Name: "neon", Type: models.TagTypeGeneral, Count: 67}, + {Name: "glitch", Type: models.TagTypeMeta, Count: 34}, + {Name: "pixel_art", Type: models.TagTypeGeneral, Count: 89}, + {Name: "lo_fi", Type: models.TagTypeGeneral, Count: 56}, + } + ctx.Locals("RecentTags", mockRecentTags) + } else { + ctx.Locals("RecentTags", recentTags) + } + + postsCount, postsErr := database.GetTotalPostsCount() + tagsCount, tagsCountErr := database.GetTotalTagsCount() + commentsCount, commentsErr := database.GetTotalCommentsCount() + todayCount, todayErr := database.GetTodayPostsCount() + storageSize, storageErr := database.GetTotalStorageSize() + + var stats SiteStats + + if postsErr == nil { + stats.Posts = fmt.Sprintf("%d", postsCount) + } else { + stats.Posts = "0" + } + if tagsCountErr == nil { + stats.Tags = fmt.Sprintf("%d", tagsCount) + } else { + stats.Tags = "0" + } + if commentsErr == nil { + stats.Comments = fmt.Sprintf("%d", commentsCount) + } else { + stats.Comments = "0" + } + if todayErr == nil { + stats.Today = fmt.Sprintf("%d new", todayCount) + } else { + stats.Today = "0 new" + } + if storageErr == nil { + stats.Storage = storageSize + } else { + stats.Storage = "0 B" + } + + ctx.Locals("SiteStats", stats) + + return ctx.Next() +} |
