diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ced82a0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,33 @@ +# Git +.git +.gitignore + +# Maven +target/ +!target/*.jar +.mvn/ +mvnw +mvnw.cmd + +# IDE files +.idea/ +*.iml +.vscode/ +*.swp +*.swo + +# Logs +logs/ +*.log + +# OS specific +.DS_Store +Thumbs.db + +# Docker +.dockerignore +docker-compose*.yml + +# Documentation +README*.md +LICENSE \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0d71edc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM maven:3.8.6-openjdk-17-slim AS build +WORKDIR /app + +# Copy the Maven POM file +COPY pom.xml . + +# Copy the source code +COPY src ./src + +# Build the application +RUN mvn clean package -DskipTests + +# Create the runtime image +FROM openjdk:17-slim +WORKDIR /app + +# Copy the JAR file from the build stage +COPY --from=build /app/target/*.jar app.jar + +# Environment variables +ENV MYSQL_HOST=mysql \ + MYSQL_PORT=3306 \ + MYSQL_DATABASE=restapi \ + MYSQL_USER=root \ + MYSQL_PASSWORD=password \ + SERVER_PORT=8080 + +# Expose the port the app runs on +EXPOSE ${SERVER_PORT} + +# Command to run the application +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/README.md b/README.md index 55bdebb..64302ba 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,121 @@ # REST API -![REST API](https://img.shields.io/badge/REST_API-v1.0-blue) +[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.3.2-brightgreen)](https://spring.io/projects/spring-boot) +[![Java](https://img.shields.io/badge/Java-17-orange)](https://www.java.com/) +[![MySQL](https://img.shields.io/badge/MySQL-8.0-blue)](https://www.mysql.com/) +[![Maven](https://img.shields.io/badge/Maven-3.8.6-red)](https://maven.apache.org/) +[![Docker](https://img.shields.io/badge/Docker-Ready-blue)](https://www.docker.com/) +[![License](https://img.shields.io/badge/License-MIT-yellow)](LICENSE) -## Deskripsi +## Description -REST API ini adalah aplikasi yang dirancang untuk menyediakan antarmuka pemrograman aplikasi (API) yang mengikuti prinsip REST. API ini memungkinkan pengguna untuk melakukan operasi dasar seperti **CRUD (Create, Read, Update, Delete)** untuk data tertentu. +This REST API is a robust application designed to provide a RESTful interface following industry best practices. Built with Spring Boot, it offers a comprehensive solution for performing CRUD (Create, Read, Update, Delete) operations on data resources. The API is designed with scalability, security, and ease of use in mind. -## Fitur +## Features -- **Pengelolaan Data**: Buat, baca, perbarui, dan hapus data. -- **Autentikasi Pengguna**: Sistem autentikasi sederhana untuk keamanan API. -- **Response Format**: Menggunakan format JSON untuk semua response. -- **Penggunaan yang Mudah**: Dokumentasi lengkap dan contoh penggunaan. +- **Complete CRUD Operations**: Create, read, update, and delete data with well-defined endpoints +- **RESTful Architecture**: Follows REST principles for predictable and standardized API behavior +- **JSON Response Format**: All API responses are formatted in JSON for easy integration +- **Error Handling**: Comprehensive error handling with appropriate HTTP status codes +- **Data Validation**: Input validation to ensure data integrity +- **User Authentication**: Secure endpoints with authentication mechanisms +- **API Documentation**: Detailed documentation for all endpoints +- **Docker Support**: Easy deployment with Docker containers -## Teknologi yang Digunakan +## Technologies Used -- **Bahasa Pemrograman**: [Java](https://www.java.com/) -- **Framework**: [Spring Boot](https://spring.io/projects/spring-boot) -- **Database**: [MySQL](https://www.mysql.com/) -- **Dependency Management**: [Maven](https://maven.apache.org/) +- **Programming Language**: [Java 17](https://www.java.com/) +- **Framework**: [Spring Boot 3.3.2](https://spring.io/projects/spring-boot) +- **Database**: [MySQL 8.0](https://www.mysql.com/) +- **ORM**: [Spring Data JPA](https://spring.io/projects/spring-data-jpa) +- **Build Tool**: [Maven](https://maven.apache.org/) +- **Containerization**: [Docker](https://www.docker.com/) ## Prerequisites -Sebelum menjalankan aplikasi, pastikan Anda memiliki: +Before running the application, ensure you have: -- Java JDK 17 atau yang lebih baru -- MySQL Server -- Maven +- Java JDK 17 or newer +- MySQL Server 8.0 or newer +- Maven 3.6+ (or use the included Maven wrapper) +- Docker and Docker Compose (for containerized deployment) -## Instalasi +## Installation -1. **Clone Repositori** +### Option 1: Standard Installation + +1. **Clone the Repository** + ```bash + git clone https://github.com/yourbee03/REST_API.git + cd REST_API + ``` + +2. **Configure Database** + + Update the database configuration in `src/main/resources/application.properties` with your MySQL credentials. + +3. **Build the Application** + ```bash + ./mvnw clean install + ``` + +4. **Run the Application** + ```bash + ./mvnw spring-boot:run + ``` + +### Option 2: Docker Installation + +1. **Clone the Repository** ```bash git clone https://github.com/yourbee03/REST_API.git cd REST_API + ``` + +2. **Build and Run with Docker Compose** + ```bash + docker compose up -d + ``` + + This will start both the Spring Boot application and a MySQL database in containers. + +## API Endpoints + +The API provides the following endpoints: + +- `GET /api/resource` - Retrieve all resources +- `GET /api/resource/{id}` - Retrieve a specific resource +- `POST /api/resource` - Create a new resource +- `PUT /api/resource/{id}` - Update an existing resource +- `DELETE /api/resource/{id}` - Delete a resource + +## Environment Variables + +When using Docker, you can configure the application using the following environment variables: + +- `MYSQL_HOST` - MySQL database host +- `MYSQL_PORT` - MySQL database port +- `MYSQL_DATABASE` - MySQL database name +- `MYSQL_USER` - MySQL username +- `MYSQL_PASSWORD` - MySQL password +- `SERVER_PORT` - Application server port + +## Testing + +Run the automated tests with: + +```bash +./mvnw test +``` + +## Contributing + +1. Fork the repository +2. Create your feature branch (`git checkout -b feature/amazing-feature`) +3. Commit your changes (`git commit -m 'Add some amazing feature'`) +4. Push to the branch (`git push origin feature/amazing-feature`) +5. Open a Pull Request + +## License + +This project is licensed under the MIT License - see the LICENSE file for details. diff --git a/README_ENG.md b/README_ENG.md deleted file mode 100644 index 26998b3..0000000 --- a/README_ENG.md +++ /dev/null @@ -1,38 +0,0 @@ -# REST API - -![REST API](https://img.shields.io/badge/REST_API-v1.0-blue) - -## Description - -This REST API is an application designed to provide an Application Programming Interface (API) that follows the principles of REST. This API allows users to perform basic CRUD (Create, Read, Update, Delete) operations on certain data. - -## Features - -- **Data Management**: Create, read, update, and delete data. -- **User Authentication**: Simple authentication system for API security. -- **Response Format**: Uses JSON format for all responses. -- **Easy to Use**: Comprehensive documentation and usage examples. - -## Technologies Used - -- **Programming Language**: [Java](https://www.java.com/) -- **Framework**: [Spring Boot](https://spring.io/projects/spring-boot) -- **Database**: [MySQL](https://www.mysql.com/) -- **Dependency Management**: [Maven](https://maven.apache.org/) - -## Prerequisites - -Before running the application, make sure you have: - -- Java JDK 17 or newer -- MySQL Server -- Maven - -## Installation - -1. **Clone the Repository** - -```bash -git clone https://github.com/yourbee03/REST_API.git -cd REST_API -``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d28d51d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,47 @@ +version: '3.8' + +services: + # Spring Boot Application + app: + build: + context: . + dockerfile: Dockerfile + container_name: rest-api-app + ports: + - "8080:8080" + environment: + - MYSQL_HOST=mysql + - MYSQL_PORT=3306 + - MYSQL_DATABASE=restapi + - MYSQL_USER=root + - MYSQL_PASSWORD=password + - SERVER_PORT=8080 + depends_on: + - mysql + networks: + - rest-api-network + restart: unless-stopped + + # MySQL Database + mysql: + image: mysql:8.0 + container_name: rest-api-mysql + ports: + - "3306:3306" + environment: + - MYSQL_DATABASE=restapi + - MYSQL_ROOT_PASSWORD=password + volumes: + - mysql-data:/var/lib/mysql + networks: + - rest-api-network + restart: unless-stopped + command: --default-authentication-plugin=mysql_native_password + +networks: + rest-api-network: + driver: bridge + +volumes: + mysql-data: + driver: local \ No newline at end of file diff --git a/docker-start.sh b/docker-start.sh new file mode 100755 index 0000000..3505ca0 --- /dev/null +++ b/docker-start.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Build and start the Docker containers +echo "Building and starting Docker containers..." +docker-compose up -d --build + +# Check if containers are running +echo "Checking container status..." +docker-compose ps + +echo "" +echo "REST API is now running!" +echo "Access the API at: http://localhost:8080/api" +echo "" +echo "To stop the containers, run: docker-compose down" \ No newline at end of file diff --git a/docker-stop.sh b/docker-stop.sh new file mode 100755 index 0000000..0e7532e --- /dev/null +++ b/docker-stop.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# Stop and remove Docker containers +echo "Stopping Docker containers..." +docker-compose down + +echo "" +echo "Docker containers have been stopped." +echo "To start the containers again, run: ./docker-start.sh" \ No newline at end of file