/image-service

Image caching service

Primary LanguageJavaMIT LicenseMIT

Image Service

Motivation

This project is an assignment. Implemented code would be a nice example of Image Service that cache image in S3.

Features

Image Service APIs provides

  • Fetching image from cache in S3.

  • Fetch original image if cached image is not present.

functional-diagram.png screenshot
sequence-diagram screenshot

How to run

Prerequisite
  • JDK 17.0.6 (Tested with Amazon Corretto JDK)

  • Maven 3.8.x+

  • Docker (20.10.14), Docker Compose (1.29.2)

Run ImageService, LocalStack and MariaDB (for Prod logging) in Docker
$ docker build -t mmahmoodictbd/image-service .
$ docker-compose up
Build and Run
$ docker-compose -f docker-compose-infra.yml up
$ mvn clean install
$ java -jar target/image-service-1.0.0.jar

or

$ docker-compose -f docker-compose-infra.yml up
$ mvn spring-boot:run
Quick test

For testing purpose, source-root-url is set to https://www.gstatic.com/.

Available image types are - original, thumbnail and detail-large.

Open browser and hit -

http://localhost:8080/image/show/detail-large/fake-seo?reference=/webp/gallery3/1.sm.png
http://localhost:8080/image/show/thumbnail/fake-seo?reference=/webp/gallery3/1.sm.png
http://localhost:8080/image/original/thumbnail/fake-seo?reference=/webp/gallery3/1.sm.png

You should see the image -

sample-response.png screenshot

Using awslocal CLI, you can verify cached S3 images

➜  ~ awslocal s3 ls --recursive s3://images/
2023-04-12 18:10:45      48061 detail-large/_web/p_ga/_webp_gallery3_1.sm.png
2023-04-12 18:10:06      48061 original/_web/p_ga/_webp_gallery3_1.sm.png

To flush the cache, use -

curl --location --request DELETE 'http://localhost:8080/image/flush/original?reference=/webp/gallery3/1.sm.png'

Using awslocal CLI, you can verify removal of cached S3 images

➜  ~ awslocal s3 ls --recursive s3://images/
2023-04-12 14:16:16      48061 detail-large/_web/p_ga/_webp_gallery3_1.sm.png
2023-04-12 14:16:16      48061 original/_web/p_ga/_webp_gallery3_1.sm.png
➜  ~ curl --location --request DELETE 'http://localhost:8080/image/flush/original?reference=/webp/gallery3/1.sm.png'
➜  ~ awslocal s3 ls --recursive s3://images/
➜  ~

Production

How to view logs in DB

Production logs (default active profile is prod) stored in MariaDB. If you are running docker-compose-infra.yml, access phpmyadmin (http://localhost:3307, username/pass: root/ root) to view logs in the DB.

logdb.png screenshot

Development

How to run tests
How to run unit tests

To run the unit tests, execute the following commands

mvn clean test-compile test

Improvements to make

  • Fix TODO in src/main/resources/logback-spring.xml

  • Generate and check OWASP report.

  • Improve code coverage, e.g. adding end-to-end tests.

Licensed under the MIT License, see the LICENSE file for details.