diff --git a/api/album.go b/api/album.go index ef20c3b..420d0a3 100644 --- a/api/album.go +++ b/api/album.go @@ -6,7 +6,6 @@ import ( "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/microcosm-cc/bluemonday" - "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) @@ -35,69 +34,67 @@ type Media struct { MimeType string } -var albumCache = cache.New(1*time.Hour, 15*time.Minute) - -func FetchAlbum(albumID string) (Album, error) { - cacheData, found := albumCache.Get(albumID + "-album") +func (client *Client) FetchAlbum(albumID string) (Album, error) { + cacheData, found := client.Cache.Get(albumID + "-album") if found { return cacheData.(Album), nil } - data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config.ImgurId + "&include=media%2Caccount") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount") if err != nil { return Album{}, err } - album, err := ParseAlbum(data) + album, err := parseAlbum(data) if err != nil { return Album{}, err } - albumCache.Set(albumID+"-album", album, cache.DefaultExpiration) + client.Cache.Set(albumID+"-album", album, 1*time.Hour) return album, err } -func FetchPosts(albumID string) (Album, error) { - cacheData, found := albumCache.Get(albumID + "-posts") +func (client *Client) FetchPosts(albumID string) (Album, error) { + cacheData, found := client.Cache.Get(albumID + "-posts") if found { return cacheData.(Album), nil } - data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config.ImgurId + "&include=media%2Caccount%2Ctags") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount%2Ctags") if err != nil { return Album{}, err } - album, err := ParseAlbum(data) + album, err := parseAlbum(data) if err != nil { return Album{}, err } - albumCache.Set(albumID+"-posts", album, cache.DefaultExpiration) + client.Cache.Set(albumID+"-posts", album, 1*time.Hour) return album, nil } -func FetchMedia(mediaID string) (Album, error) { - cacheData, found := albumCache.Get(mediaID + "-media") +func (client *Client) FetchMedia(mediaID string) (Album, error) { + cacheData, found := client.Cache.Get(mediaID + "-media") if found { return cacheData.(Album), nil } - data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config.ImgurId + "&include=media%2Caccount") + data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + client.ClientID + "&include=media%2Caccount") if err != nil { return Album{}, err } - album, err := ParseAlbum(data) + album, err := parseAlbum(data) if err != nil { return Album{}, err } - albumCache.Set(mediaID+"-media", album, cache.DefaultExpiration) + client.Cache.Set(mediaID+"-media", album, 1*time.Hour) return album, nil } -func ParseAlbum(data gjson.Result) (Album, error) { +func parseAlbum(data gjson.Result) (Album, error) { media := make([]Media, 0) data.Get("media").ForEach( func(key gjson.Result, value gjson.Result) bool { diff --git a/api/client.go b/api/client.go new file mode 100644 index 0000000..6b9de21 --- /dev/null +++ b/api/client.go @@ -0,0 +1,21 @@ +package api + +import ( + "time" + + "github.com/patrickmn/go-cache" +) + +type Client struct { + ClientID string + Cache *cache.Cache +} + +func NewClient(clientId string) (*Client) { + client := Client{ + ClientID: clientId, + Cache: cache.New(15*time.Minute, 15*time.Minute), + } + + return &client +} \ No newline at end of file diff --git a/api/comments.go b/api/comments.go index 7ee36c8..b6be83d 100644 --- a/api/comments.go +++ b/api/comments.go @@ -28,15 +28,13 @@ type Comment struct { DeletedAt string } -var commentCache = cache.New(15*time.Minute, 15*time.Minute) - -func FetchComments(galleryID string) ([]Comment, error) { - cacheData, found := commentCache.Get(galleryID) +func (client *Client) FetchComments(galleryID string) ([]Comment, error) { + cacheData, found := client.Cache.Get(galleryID + "-comments") if found { return cacheData.([]Comment), nil } - data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + utils.Config.ImgurId + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best") + data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + client.ClientID + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best") if err != nil { return []Comment{}, nil } @@ -49,7 +47,7 @@ func FetchComments(galleryID string) ([]Comment, error) { go func() { defer wg.Done() - comments = append(comments, ParseComment(value)) + comments = append(comments, parseComment(value)) }() return true @@ -57,7 +55,7 @@ func FetchComments(galleryID string) ([]Comment, error) { ) wg.Wait() - commentCache.Set(galleryID, comments, cache.DefaultExpiration) + client.Cache.Set(galleryID + "-comments", comments, cache.DefaultExpiration) return comments, nil } @@ -66,7 +64,7 @@ var vidRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(mp4|webm)`) var vidFormatRe = regexp.MustCompile(`\.(mp4|webm)`) var iImgurRe = regexp.MustCompile(`https?://i\.imgur\.com`) -func ParseComment(data gjson.Result) Comment { +func parseComment(data gjson.Result) Comment { createdTime, _ := time.Parse("2006-01-02T15:04:05Z", data.Get("created_at").String()) createdAt := createdTime.Format("January 2, 2006 3:04 PM") updatedAt, _ := utils.FormatDate(data.Get("updated_at").String()) @@ -82,7 +80,7 @@ func ParseComment(data gjson.Result) Comment { go func() { defer wg.Done() - comments = append(comments, ParseComment(value)) + comments = append(comments, parseComment(value)) }() return true diff --git a/api/tag.go b/api/tag.go index b9b8f6f..968ef57 100644 --- a/api/tag.go +++ b/api/tag.go @@ -5,9 +5,7 @@ import ( "net/http" "strings" "sync" - "time" - - "codeberg.org/video-prize-ranch/rimgo/utils" + "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) @@ -22,10 +20,8 @@ type Tag struct { BackgroundId string } -var tagCache = cache.New(15*time.Minute, 15*time.Minute) - -func FetchTag(tag string, sort string, page string) (Tag, error) { - cacheData, found := tagCache.Get(tag + sort + page) +func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error) { + cacheData, found := client.Cache.Get(tag + sort + page + "-tag") if found { return cacheData.(Tag), nil } @@ -36,7 +32,7 @@ func FetchTag(tag string, sort string, page string) (Tag, error) { } q := req.URL.Query() - q.Add("client_id", utils.Config.ImgurId) + q.Add("client_id", client.ClientID) q.Add("include", "cover") q.Add("page", page) @@ -109,6 +105,6 @@ func FetchTag(tag string, sort string, page string) (Tag, error) { Background: "/" + data.Get("background_id").String() + ".webp", } - tagCache.Set(tag, tagData, cache.DefaultExpiration) + client.Cache.Set(tag + sort + page + "-tag", tagData, cache.DefaultExpiration) return tagData, nil } diff --git a/api/user.go b/api/user.go index 979e314..404a3ef 100644 --- a/api/user.go +++ b/api/user.go @@ -8,7 +8,6 @@ import ( "time" "codeberg.org/video-prize-ranch/rimgo/utils" - "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) @@ -35,10 +34,8 @@ type Submission struct { IsAlbum bool } -var userCache = cache.New(30*time.Minute, 15*time.Minute) - -func FetchUser(username string) (User, error) { - cacheData, found := userCache.Get(username) +func (client *Client) FetchUser(username string) (User, error) { + cacheData, found := client.Cache.Get(username + "-user") if found { return cacheData.(User), nil } @@ -67,12 +64,12 @@ func FetchUser(username string) (User, error) { CreatedAt: createdTime.Format("January 2, 2006"), } - userCache.Set(username, user, 1*time.Hour) + client.Cache.Set(username + "-user", user, 1*time.Hour) return user, nil } -func FetchSubmissions(username string, sort string, page string) ([]Submission, error) { - cacheData, found := userCache.Get(username + "-submissions") +func (client *Client) FetchSubmissions(username string, sort string, page string) ([]Submission, error) { + cacheData, found := client.Cache.Get(username + "-submissions") if found { return cacheData.([]Submission), nil } @@ -131,6 +128,6 @@ func FetchSubmissions(username string, sort string, page string) ([]Submission, ) wg.Wait() - userCache.Set(username + "-submissions", submissions, 15*time.Minute) + client.Cache.Set(username + "-submissions", submissions, 15*time.Minute) return submissions, nil } diff --git a/main.go b/main.go index d88070f..d33d63c 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,8 @@ func main() { fmt.Println(err) } utils.LoadConfig() + + pages.InitializeApiClient() engine := handlebars.NewFileSystem(http.FS(views.GetFiles()), ".hbs") diff --git a/pages/apiClient.go b/pages/apiClient.go new file mode 100644 index 0000000..5b6d0e4 --- /dev/null +++ b/pages/apiClient.go @@ -0,0 +1,12 @@ +package pages + +import ( + "codeberg.org/video-prize-ranch/rimgo/api" + "codeberg.org/video-prize-ranch/rimgo/utils" +) + +var ApiClient *api.Client + +func InitializeApiClient() { + ApiClient = api.NewClient(utils.Config.ImgurId) +} \ No newline at end of file diff --git a/pages/embed.go b/pages/embed.go index ea7e122..9cf1b23 100644 --- a/pages/embed.go +++ b/pages/embed.go @@ -16,11 +16,11 @@ func HandleEmbed(c *fiber.Ctx) error { post, err := api.Album{}, error(nil) switch { case strings.HasPrefix(c.Path(), "/a"): - post, err = api.FetchAlbum(c.Params("postID")) + post, err = ApiClient.FetchAlbum(c.Params("postID")) case strings.HasPrefix(c.Path(), "/gallery"): - post, err = api.FetchPosts(c.Params("postID")) + post, err = ApiClient.FetchPosts(c.Params("postID")) default: - post, err = api.FetchMedia(c.Params("postID")) + post, err = ApiClient.FetchMedia(c.Params("postID")) } if err != nil && err.Error() == "ratelimited by imgur" { return c.Status(429).Render("errors/429", nil) diff --git a/pages/post.go b/pages/post.go index 8a5f41a..93b4d55 100644 --- a/pages/post.go +++ b/pages/post.go @@ -17,11 +17,11 @@ func HandlePost(c *fiber.Ctx) error { post, err := api.Album{}, error(nil) switch { case strings.HasPrefix(c.Path(), "/a"): - post, err = api.FetchAlbum(c.Params("postID")) + post, err = ApiClient.FetchAlbum(c.Params("postID")) case strings.HasPrefix(c.Path(), "/gallery"): - post, err = api.FetchPosts(c.Params("postID")) + post, err = ApiClient.FetchPosts(c.Params("postID")) default: - post, err = api.FetchMedia(c.Params("postID")) + post, err = ApiClient.FetchMedia(c.Params("postID")) } if err != nil && err.Error() == "ratelimited by imgur" { return c.Status(429).Render("errors/429", nil) @@ -36,7 +36,7 @@ func HandlePost(c *fiber.Ctx) error { comments := []api.Comment{} if post.SharedWithCommunity { c.Set("Cache-Control", "public,max-age=604800") - comments, err = api.FetchComments(c.Params("postID")) + comments, err = ApiClient.FetchComments(c.Params("postID")) if err != nil { return err } diff --git a/pages/tag.go b/pages/tag.go index 093ffa3..611148d 100644 --- a/pages/tag.go +++ b/pages/tag.go @@ -3,7 +3,6 @@ package pages import ( "strconv" - "codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" ) @@ -29,7 +28,7 @@ func HandleTag(c *fiber.Ctx) error { displayPrevPage = false } - tag, err := api.FetchTag(c.Params("tag"), c.Query("sort"), page) + tag, err := ApiClient.FetchTag(c.Params("tag"), c.Query("sort"), page) if err != nil && err.Error() == "ratelimited by imgur" { return c.Status(429).Render("errors/429", nil) } diff --git a/pages/user.go b/pages/user.go index 9a990b4..2756a22 100644 --- a/pages/user.go +++ b/pages/user.go @@ -3,7 +3,6 @@ package pages import ( "strconv" - "codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" ) @@ -24,7 +23,7 @@ func HandleUser(c *fiber.Ctx) error { pageNumber = 0 } - user, err := api.FetchUser(c.Params("userID")) + user, err := ApiClient.FetchUser(c.Params("userID")) if err != nil && err.Error() == "ratelimited by imgur" { return c.Status(429).Render("errors/429", nil) } @@ -35,7 +34,7 @@ func HandleUser(c *fiber.Ctx) error { return c.Status(404).Render("errors/404", nil) } - submissions, err := api.FetchSubmissions(c.Params("userID"), "newest", page) + submissions, err := ApiClient.FetchSubmissions(c.Params("userID"), "newest", page) if err != nil && err.Error() == "ratelimited by imgur" { c.Status(429) return c.Render("errors/429", nil)