chore: tackle issues

This commit is contained in:
Reinaldy Rafli 2022-09-03 21:09:26 +07:00
parent 7a90e11203
commit 16293db6cb
No known key found for this signature in database
GPG Key ID: EA1B2522C1693EC8
14 changed files with 35 additions and 17 deletions

View File

@ -51,7 +51,7 @@ jobs:
env: env:
ENV: development ENV: development
PORT: 5000 PORT: 5000
MINIO_HOST: minio:9000 MINIO_HOST: bucket:9000
MINIO_ACCESS_ID: minio MINIO_ACCESS_ID: minio
MINIO_SECRET_KEY: password MINIO_SECRET_KEY: password
REDIS_URL: redis://@redis:6379 REDIS_URL: redis://@redis:6379

View File

@ -88,7 +88,7 @@ jobs:
env: env:
ENV: development ENV: development
PORT: 5000 PORT: 5000
MINIO_HOST: minio:9000 MINIO_HOST: bucket:9000
MINIO_ACCESS_ID: minio MINIO_ACCESS_ID: minio
MINIO_SECRET_KEY: password MINIO_SECRET_KEY: password
REDIS_URL: redis://@redis:6379 REDIS_URL: redis://@redis:6379

View File

@ -16,6 +16,7 @@ import (
"github.com/minio/minio-go/v7" "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) { 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) totalJokes, err := GetTotalJoke(ctx, bucket, cache, memory)
if err != nil { if err != nil {
@ -24,7 +25,7 @@ func GetRandomJoke(ctx context.Context, bucket *minio.Client, cache *redis.Clien
randomIndex := rand.Intn(totalJokes - 1) 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 { if err != nil {
return []byte{}, "", fmt.Errorf("getting joke by id: %w", err) 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 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)) jokeFromMemory, err := memory.Get("id:" + strconv.Itoa(id))
if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) { if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) {
return []byte{}, "", fmt.Errorf("acquiring joke from memory: %w", err) return []byte{}, "", fmt.Errorf("acquiring joke from memory: %w", err)

View File

@ -29,7 +29,7 @@ func TestGetJokeById(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel() 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 { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -42,7 +42,7 @@ func TestGetJokeById(t *testing.T) {
t.Error("empty image") 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 { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -55,7 +55,7 @@ func TestGetJokeById(t *testing.T) {
t.Errorf("difference in image bytes") 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 { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }

View File

@ -2,8 +2,11 @@ package joke
import "time" import "time"
// JokesBapak2Bucket defines the bucket that the jokes resides in.
const JokesBapak2Bucket = "jokesbapak2" const JokesBapak2Bucket = "jokesbapak2"
// Joke provides a simple struct that points
// to the information of the joke.
type Joke struct { type Joke struct {
FileName string FileName string
ContentType string ContentType string

View File

@ -19,9 +19,9 @@ var cache *redis.Client
var memory *bigcache.BigCache var memory *bigcache.BigCache
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
redisUrl, ok := os.LookupEnv("REDIS_URL") redisURL, ok := os.LookupEnv("REDIS_URL")
if !ok { if !ok {
redisUrl = "redis://@localhost:6379" redisURL = "redis://@localhost:6379"
} }
minioHost, ok := os.LookupEnv("MINIO_HOST") minioHost, ok := os.LookupEnv("MINIO_HOST")
@ -44,13 +44,13 @@ func TestMain(m *testing.M) {
minioToken = "" minioToken = ""
} }
parsedRedisUrl, err := redis.ParseURL(redisUrl) parsedRedisURL, err := redis.ParseURL(redisURL)
if err != nil { if err != nil {
log.Fatalf("parsing redis url: %s", err.Error()) log.Fatalf("parsing redis url: %s", err.Error())
return return
} }
redisClient := redis.NewClient(parsedRedisUrl) redisClient := redis.NewClient(parsedRedisURL)
minioClient, err := minio.New(minioHost, &minio.Options{ minioClient, err := minio.New(minioHost, &minio.Options{
Creds: credentials.NewStaticV4(minioID, minioSecret, minioToken), Creds: credentials.NewStaticV4(minioID, minioSecret, minioToken),

View File

@ -13,6 +13,9 @@ import (
"github.com/minio/minio-go/v7" "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) { func ListJokesFromBucket(ctx context.Context, bucket *minio.Client, cache *redis.Client) ([]Joke, error) {
cached, err := cache.Get(ctx, "jokes:list").Result() cached, err := cache.Get(ctx, "jokes:list").Result()
if err != nil && !errors.Is(err, redis.Nil) { if err != nil && !errors.Is(err, redis.Nil) {

View File

@ -13,6 +13,9 @@ import (
"github.com/minio/minio-go/v7" "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) { func GetTodaysJoke(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) (image []byte, contentType string, err error) {
// Today's date: // Today's date:
today := time.Now().Format("2006-01-02") today := time.Now().Format("2006-01-02")

View File

@ -13,6 +13,7 @@ import (
"github.com/minio/minio-go/v7" "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) { func GetTotalJoke(ctx context.Context, bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) (int, error) {
totalJokesFromMemory, err := memory.Get("total") totalJokesFromMemory, err := memory.Get("total")
if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) { if err != nil && !errors.Is(err, bigcache.ErrEntryNotFound) {

View File

@ -8,7 +8,8 @@ import (
"github.com/minio/minio-go/v7" "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( info, err := bucket.PutObject(
ctx, ctx,
JokesBapak2Bucket, // bucketName JokesBapak2Bucket, // bucketName

View File

@ -49,7 +49,7 @@ func (d *Dependencies) JokeByID(w http.ResponseWriter, r *http.Request) {
return 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 { if err != nil {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -23,9 +23,9 @@ import (
) )
func main() { func main() {
redisUrl, ok := os.LookupEnv("REDIS_URL") redisURL, ok := os.LookupEnv("REDIS_URL")
if !ok { if !ok {
redisUrl = "redis://@localhost:6379" redisURL = "redis://@localhost:6379"
} }
minioHost, ok := os.LookupEnv("MINIO_HOST") minioHost, ok := os.LookupEnv("MINIO_HOST")
@ -84,13 +84,13 @@ func main() {
return return
} }
parsedRedisUrl, err := redis.ParseURL(redisUrl) parsedRedisURL, err := redis.ParseURL(redisURL)
if err != nil { if err != nil {
log.Fatalf("parsing redis url: %s", err.Error()) log.Fatalf("parsing redis url: %s", err.Error())
return return
} }
redisClient := redis.NewClient(parsedRedisUrl) redisClient := redis.NewClient(parsedRedisURL)
defer func() { defer func() {
err := redisClient.Close() err := redisClient.Close()
if err != nil { if err != nil {

View File

@ -8,6 +8,7 @@ import (
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
) )
// Health provides route for healthcheck endpoints.
func Health(bucket *minio.Client, cache *redis.Client) *chi.Mux { func Health(bucket *minio.Client, cache *redis.Client) *chi.Mux {
dependency := &health.Dependencies{ dependency := &health.Dependencies{
Bucket: bucket, Bucket: bucket,

View File

@ -9,6 +9,7 @@ import (
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
) )
// Joke provides route for jokes.
func Joke(bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) *chi.Mux { func Joke(bucket *minio.Client, cache *redis.Client, memory *bigcache.BigCache) *chi.Mux {
deps := &joke.Dependencies{ deps := &joke.Dependencies{
Memory: memory, Memory: memory,