LibraryApi is a Java-based web application that provides a REST fulL API for managing a library system. It is built with Spring Boot and uses MySQL for data persistence.
Index
- Java 21
- Spring Boot 3.2.5
- MySQL
- Maven
- Flyway for database migrations
- Spring Security for authentication and authorization
In this project, Spring Security is used to handle user authentication and authorization. It has been configured to authenticate users through a custom user details service that loads user details by their username. User roles are used to authorize requests to different API endpoints.
Roles in Spring Security are used to authorize users to access different parts of the application(POST, PUT, DELETE). In this project, two roles are used: ROLE_USER
and ROLE_ADMIN
. These roles must be added to the database with the prefix ROLE_
for Spring Security to recognize them.
To run this project, you need to have Java and Maven installed on your machine.
-
Clone the repository:
git clone https://github.com/andre-carbajal/LibraryApi.git
-
Navigate to the project directory:
cd LibraryApi
-
Create a MySQL database:
CREATE DATABASE libraryApi;
-
Download the dependencies:
mvn dependency:resolve
-
Build the project:
mvn clean install
-
Run the Flyway migrations to set up the database schema:
mvn flyway:migrate
-
Run the project:
mvn spring-boot:run
This application can be run using Docker and Docker Compose. Docker allows you to create a container for your application, ensuring it has all the dependencies it needs to run, and Docker Compose allows you to manage applications that require multiple containers, like this one which requires a separate container for the MySQL database.
Ensure you have Docker and Docker Compose installed on your machine. If not, you can download them from the official Docker website.
-
Build the Docker images:
Docker can build images automatically by reading the instructions from a Dockerfile. In the root directory of the project, you can find a
Dockerfile
which contains instructions on how to build the image for the application.Run the following command in the terminal to build the Docker image:
docker build -t library-api .
This command tells Docker to build an image using the Dockerfile in the current directory and tag the image as
library-api
. -
Run the Docker container:
After building the Docker image, you can run the Docker container with the following command:
docker run -e 'DB_HOST=<your_database_host>' -e 'DB_PORT=<your_database_port>' -e 'DB_USERNAME=<your_database_username>' -e 'DB_PASSWORD=<your_database_password>' -p 8080:8080 library-api
Replace
<your_database_host>
,<your_database_port>
,<your_database_username>
, and<your_database_password>
with your actual database credentials. If you are using a local database, you can usehost.docker.internal
as theDB_HOST
.This command tells Docker to run the
library-api
image as a container, with the specified environment variables and port mapping. The application will be accessible athttp://localhost:8080
. -
Run the Docker Compose:
Docker Compose uses a file named
docker-compose.yml
for defining and running multi-container Docker applications.Run the following command in the terminal to start the application and the database using Docker Compose:
docker compose up
This command tells Docker Compose to start the services defined in the
docker-compose.yml
file.The application will be accessible at
http://localhost:8080
. -
Stop the Docker Compose:
You can stop the services started by Docker Compose by running the following command in the terminal:
docker compose down
This command tells Docker Compose to stop the services defined in the
docker-compose.yml
file.
The API documentation is generated using Springdoc OpenAPI
. This allows for interactive exploration of the API via a web interface. The documentation is available at http://localhost:8080/doc after starting the application.
This project is licensed under the MIT License - see the LICENSE file for details.