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