chore: tackle issues
This commit is contained in:
parent
7a90e11203
commit
16293db6cb
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue