From 16293db6cb51045a1cbbbeeabd3e06237e0bba76 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sat, 3 Sep 2022 21:09:26 +0700 Subject: [PATCH] chore: tackle issues --- .github/workflows/ci.yml | 2 +- .github/workflows/pr.yml | 2 +- api/core/joke/getter.go | 9 +++++++-- api/core/joke/getter_test.go | 6 +++--- api/core/joke/joke.go | 3 +++ api/core/joke/joke_test.go | 8 ++++---- api/core/joke/list.go | 3 +++ api/core/joke/today.go | 3 +++ api/core/joke/total.go | 1 + api/core/joke/uploader.go | 3 ++- api/handler/joke/get.go | 2 +- api/main.go | 8 ++++---- api/routes/health.go | 1 + api/routes/joke.go | 1 + 14 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ed9111..34a40b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: env: ENV: development PORT: 5000 - MINIO_HOST: minio:9000 + MINIO_HOST: bucket:9000 MINIO_ACCESS_ID: minio MINIO_SECRET_KEY: password REDIS_URL: redis://@redis:6379 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 3ed5344..73c2239 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -88,7 +88,7 @@ jobs: env: ENV: development PORT: 5000 - MINIO_HOST: minio:9000 + MINIO_HOST: bucket:9000 MINIO_ACCESS_ID: minio MINIO_SECRET_KEY: password REDIS_URL: redis://@redis:6379 diff --git a/api/core/joke/getter.go b/api/core/joke/getter.go index e947074..17893f2 100644 --- a/api/core/joke/getter.go +++ b/api/core/joke/getter.go @@ -16,6 +16,7 @@ import ( "github.com/minio/minio-go/v7" ) +// GetRandomJoke will acquire a random joke from the bucket. func GetRandomJoke(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) (image []byte, contentType string, err error) { totalJokes, err := GetTotalJoke(ctx, bucket, cache, memory) if err != nil { @@ -24,7 +25,7 @@ func GetRandomJoke(ctx context.Context, bucket *minio.Client, cache *redis.Clien randomIndex := rand.Intn(totalJokes - 1) - joke, contentType, err := GetJokeById(ctx, bucket, cache, memory, randomIndex) + joke, contentType, err := GetJokeByID(ctx, bucket, cache, memory, randomIndex) if err != nil { return []byte{}, "", fmt.Errorf("getting joke by id: %w", err) } @@ -32,7 +33,11 @@ func GetRandomJoke(ctx context.Context, bucket *minio.Client, cache *redis.Clien return joke, contentType, nil } -func GetJokeById(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache, id int) (image []byte, contentType string, err error) { +// GetJokeByID wil acquire a joke by its' ID. +// +// An ID is defined as the index on the joke list that is sorted +// by it's creation (or modification) time. +func GetJokeByID(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache, id int) (image []byte, contentType string, err error) { jokeFromMemory, err := memory.Get("id:" + strconv.Itoa(id)) if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) { return []byte{}, "", fmt.Errorf("acquiring joke from memory: %w", err) diff --git a/api/core/joke/getter_test.go b/api/core/joke/getter_test.go index aa81d08..792c75d 100644 --- a/api/core/joke/getter_test.go +++ b/api/core/joke/getter_test.go @@ -29,7 +29,7 @@ func TestGetJokeById(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() - image, contentType, err := joke.GetJokeById(ctx, bucket, cache, memory, 0) + image, contentType, err := joke.GetJokeByID(ctx, bucket, cache, memory, 0) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -42,7 +42,7 @@ func TestGetJokeById(t *testing.T) { t.Error("empty image") } - cachedImage, cachedContentType, err := joke.GetJokeById(ctx, bucket, cache, memory, 0) + cachedImage, cachedContentType, err := joke.GetJokeByID(ctx, bucket, cache, memory, 0) if err != nil { t.Errorf("unexpected error: %v", err) } @@ -55,7 +55,7 @@ func TestGetJokeById(t *testing.T) { t.Errorf("difference in image bytes") } - cachedImage2, cachedContentType2, err := joke.GetJokeById(ctx, bucket, cache, memory, 0) + cachedImage2, cachedContentType2, err := joke.GetJokeByID(ctx, bucket, cache, memory, 0) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/api/core/joke/joke.go b/api/core/joke/joke.go index 0e475cb..8932f7d 100644 --- a/api/core/joke/joke.go +++ b/api/core/joke/joke.go @@ -2,8 +2,11 @@ package joke import "time" +// JokesBapak2Bucket defines the bucket that the jokes resides in. const JokesBapak2Bucket = "jokesbapak2" +// Joke provides a simple struct that points +// to the information of the joke. type Joke struct { FileName string ContentType string diff --git a/api/core/joke/joke_test.go b/api/core/joke/joke_test.go index 5a23744..feaad14 100644 --- a/api/core/joke/joke_test.go +++ b/api/core/joke/joke_test.go @@ -19,9 +19,9 @@ var cache *redis.Client var memory *bigcache.BigCache func TestMain(m *testing.M) { - redisUrl, ok := os.LookupEnv("REDIS_URL") + redisURL, ok := os.LookupEnv("REDIS_URL") if !ok { - redisUrl = "redis://@localhost:6379" + redisURL = "redis://@localhost:6379" } minioHost, ok := os.LookupEnv("MINIO_HOST") @@ -44,13 +44,13 @@ func TestMain(m *testing.M) { minioToken = "" } - parsedRedisUrl, err := redis.ParseURL(redisUrl) + parsedRedisURL, err := redis.ParseURL(redisURL) if err != nil { log.Fatalf("parsing redis url: %s", err.Error()) return } - redisClient := redis.NewClient(parsedRedisUrl) + redisClient := redis.NewClient(parsedRedisURL) minioClient, err := minio.New(minioHost, &minio.Options{ Creds: credentials.NewStaticV4(minioID, minioSecret, minioToken), diff --git a/api/core/joke/list.go b/api/core/joke/list.go index db50f25..5bac8bf 100644 --- a/api/core/joke/list.go +++ b/api/core/joke/list.go @@ -13,6 +13,9 @@ import ( "github.com/minio/minio-go/v7" ) +// ListJokesFromBucket provides a sorted list of joke by its' last modified field. +// +// It will return an empty slice if there is nothing on the bucket. func ListJokesFromBucket(ctx context.Context, bucket *minio.Client, cache *redis.Client) ([]Joke, error) { cached, err := cache.Get(ctx, "jokes:list").Result() if err != nil && !errors.Is(err, redis.Nil) { diff --git a/api/core/joke/today.go b/api/core/joke/today.go index 9d3eb82..e3471d3 100644 --- a/api/core/joke/today.go +++ b/api/core/joke/today.go @@ -13,6 +13,9 @@ import ( "github.com/minio/minio-go/v7" ) +// GetTodaysJoke will acquire today's joke. If it's not exists yet, +// it will acquire a random joke (from the GetRandomJoke method) +// and set it as today's joke. func GetTodaysJoke(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) (image []byte, contentType string, err error) { // Today's date: today := time.Now().Format("2006-01-02") diff --git a/api/core/joke/total.go b/api/core/joke/total.go index 0a03e3f..4ac0b21 100644 --- a/api/core/joke/total.go +++ b/api/core/joke/total.go @@ -13,6 +13,7 @@ import ( "github.com/minio/minio-go/v7" ) +// GetTotalJoke returns the total jokes that exists on the bucket. func GetTotalJoke(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) (int, error) { totalJokesFromMemory, err := memory.Get("total") if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) { diff --git a/api/core/joke/uploader.go b/api/core/joke/uploader.go index 19a9584..6f73b5a 100644 --- a/api/core/joke/uploader.go +++ b/api/core/joke/uploader.go @@ -8,7 +8,8 @@ import ( "github.com/minio/minio-go/v7" ) -func Uploader(bucket *minio.Client, ctx context.Context, key string, payload io.Reader, fileSize int64, contentType string) (string, error) { +// Uploader uploads a reader stream (io.Reader) to bucket. +func Uploader(ctx context.Context, bucket *minio.Client, key string, payload io.Reader, fileSize int64, contentType string) (string, error) { info, err := bucket.PutObject( ctx, JokesBapak2Bucket, // bucketName diff --git a/api/handler/joke/get.go b/api/handler/joke/get.go index e2e8b8b..310e682 100644 --- a/api/handler/joke/get.go +++ b/api/handler/joke/get.go @@ -49,7 +49,7 @@ func (d *Dependencies) JokeByID(w http.ResponseWriter, r *http.Request) { return } - joke, contentType, err := core.GetJokeById(r.Context(), d.Bucket, d.Redis, d.Memory, parsedId) + joke, contentType, err := core.GetJokeByID(r.Context(), d.Bucket, d.Redis, d.Memory, parsedId) if err != nil { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusInternalServerError) diff --git a/api/main.go b/api/main.go index c82c9ca..059769d 100644 --- a/api/main.go +++ b/api/main.go @@ -23,9 +23,9 @@ import ( ) func main() { - redisUrl, ok := os.LookupEnv("REDIS_URL") + redisURL, ok := os.LookupEnv("REDIS_URL") if !ok { - redisUrl = "redis://@localhost:6379" + redisURL = "redis://@localhost:6379" } minioHost, ok := os.LookupEnv("MINIO_HOST") @@ -84,13 +84,13 @@ func main() { return } - parsedRedisUrl, err := redis.ParseURL(redisUrl) + parsedRedisURL, err := redis.ParseURL(redisURL) if err != nil { log.Fatalf("parsing redis url: %s", err.Error()) return } - redisClient := redis.NewClient(parsedRedisUrl) + redisClient := redis.NewClient(parsedRedisURL) defer func() { err := redisClient.Close() if err != nil { diff --git a/api/routes/health.go b/api/routes/health.go index d309169..d6cda70 100644 --- a/api/routes/health.go +++ b/api/routes/health.go @@ -8,6 +8,7 @@ import ( "github.com/minio/minio-go/v7" ) +// Health provides route for healthcheck endpoints. func Health(bucket *minio.Client, cache *redis.Client) *chi.Mux { dependency := &health.Dependencies{ Bucket: bucket, diff --git a/api/routes/joke.go b/api/routes/joke.go index f8b2f7d..df1fa4f 100644 --- a/api/routes/joke.go +++ b/api/routes/joke.go @@ -9,6 +9,7 @@ import ( "github.com/minio/minio-go/v7" ) +// Joke provides route for jokes. func Joke(bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) *chi.Mux { deps := &joke.Dependencies{ Memory: memory,