Skip to content

Commit 353d3bb

Browse files
committed
Introduce REST API for book management with PostgreSQL integration, Docker support, and CI pipeline
1 parent c52d0c9 commit 353d3bb

File tree

15 files changed

+729
-110
lines changed

15 files changed

+729
-110
lines changed

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
pull_request:
7+
branches: [ master, main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
postgres:
15+
image: postgres:16-alpine
16+
env:
17+
POSTGRES_DB: book_db
18+
POSTGRES_USER: postgres
19+
POSTGRES_PASSWORD: postgres
20+
ports:
21+
- 5432:5432
22+
options: >-
23+
--health-cmd pg_isready
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
28+
steps:
29+
- uses: actions/checkout@v4
30+
31+
- name: Set up JDK 17
32+
uses: actions/setup-java@v4
33+
with:
34+
java-version: '17'
35+
distribution: 'temurin'
36+
cache: maven
37+
38+
- name: Build with Maven
39+
run: mvn -B package --file pom.xml
40+
env:
41+
DB_HOST: localhost
42+
DB_PORT: 5432
43+
DB_NAME: book_db
44+
DB_USERNAME: postgres
45+
DB_PASSWORD: postgres
46+
47+
- name: Run tests
48+
run: mvn -B test
49+
env:
50+
DB_HOST: localhost
51+
DB_PORT: 5432
52+
DB_NAME: book_db
53+
DB_USERNAME: postgres
54+
DB_PASSWORD: postgres

.gitignore

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
# Maven
2-
target
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
pom.xml.tag
5+
pom.xml.releaseBackup
6+
pom.xml.versionsBackup
7+
pom.xml.next
8+
release.properties
39

410
# Eclipse
511
.project
6-
.settings
12+
.settings/
713
.classpath
14+
bin/
815

916
# IntelliJ IDEA
10-
.idea
17+
.idea/
1118
*.iml
19+
out/
1220

13-
/app-build-deploy.bat
14-
/travis-ci.bat
21+
# VS Code
22+
.vscode/
23+
24+
# OS
25+
.DS_Store
26+
Thumbs.db
27+
28+
# Logs
29+
*.log
30+
logs/
31+
32+
# Environment
33+
.env
34+
*.env.local
35+
36+
# Build scripts
37+
app-build-deploy.bat
38+
travis-ci.bat

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM eclipse-temurin:17-jdk-alpine AS build
2+
WORKDIR /app
3+
COPY pom.xml .
4+
COPY src ./src
5+
RUN apk add --no-cache maven && mvn clean package -DskipTests
6+
7+
FROM eclipse-temurin:17-jre-alpine
8+
WORKDIR /app
9+
COPY --from=build /app/target/*.jar app.jar
10+
EXPOSE 8080
11+
ENTRYPOINT ["java", "-jar", "app.jar"]

README.md

Lines changed: 139 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,148 @@
1-
## Plain REST API CRUD with Spring Boot 3 and PostgreSQL.
1+
# Spring Boot REST API with PostgreSQL
22

3-
[![Build Status](https://travis-ci.org/OKaluzny/spring-boot-rest-api-postgresql.svg?branch=master)](https://travis-ci.org/OKaluzny/spring-boot-rest-api-postgresql)
3+
![Build](https://github.com/OKaluzny/spring-boot-rest-api-postgresql/actions/workflows/ci.yml/badge.svg)
44

5-
### Technology stack:
5+
REST API CRUD application built with Spring Boot 4 and PostgreSQL.
66

7-
* Spring Boot 3;
8-
* Spring Web;
9-
* Spring Data;
10-
* PostgreSQL database;
11-
* Hibernate;
12-
* Lombok;
13-
* Spring Security (as basic authentication).
7+
## Tech Stack
148

15-
#### To run this application use:
9+
- Java 17
10+
- Spring Boot 4.0.2
11+
- Spring Data JPA
12+
- Spring Security (Basic Auth)
13+
- PostgreSQL
14+
- Lombok
15+
- Docker
16+
17+
## Requirements
18+
19+
- JDK 17+
20+
- Maven 3.8+
21+
- PostgreSQL 14+ (or Docker)
22+
23+
## Quick Start
24+
25+
### 1. Start PostgreSQL
26+
27+
```bash
28+
docker-compose up -d postgres
29+
```
30+
31+
### 2. Run Application
1632

1733
```bash
1834
mvn spring-boot:run
1935
```
2036

21-
### The view in the Postman:
22-
[http://localhost:8080/api/books](http://localhost:8080/api/books)
37+
Application starts at `http://localhost:8080`
38+
39+
### 3. Default Credentials
40+
41+
- **API Auth:** `user:user`
42+
- **Database:** `postgres:postgres`
43+
44+
## API Endpoints
45+
46+
| Method | Endpoint | Description |
47+
|--------|----------|-------------|
48+
| POST | `/api/books` | Create book |
49+
| GET | `/api/books` | Get all books |
50+
| GET | `/api/books/{id}` | Get book by ID |
51+
| GET | `/api/books?name={name}` | Search by name |
52+
| PUT | `/api/books/{id}` | Update book |
53+
| DELETE | `/api/books/{id}` | Delete book |
54+
| DELETE | `/api/books` | Delete all books |
55+
56+
### Example Request
57+
58+
```bash
59+
curl -X POST http://localhost:8080/api/books \
60+
-H "Content-Type: application/json" \
61+
-H "Authorization: Basic dXNlcjp1c2Vy" \
62+
-d '{
63+
"name": "Java Programming",
64+
"description": "Learn Java",
65+
"tags": ["java", "programming"]
66+
}'
67+
```
68+
69+
### Example Response
70+
71+
```json
72+
{
73+
"id": 1,
74+
"name": "Java Programming",
75+
"description": "Learn Java",
76+
"tags": ["java", "programming"]
77+
}
78+
```
79+
80+
## Configuration
81+
82+
Environment variables:
83+
84+
| Variable | Default | Description |
85+
|----------|---------|-------------|
86+
| `DB_HOST` | localhost | Database host |
87+
| `DB_PORT` | 5432 | Database port |
88+
| `DB_NAME` | book_db | Database name |
89+
| `DB_USERNAME` | postgres | Database user |
90+
| `DB_PASSWORD` | postgres | Database password |
91+
| `SECURITY_USER` | user | API username |
92+
| `SECURITY_PASSWORD` | user | API password |
93+
| `SERVER_PORT` | 8080 | Application port |
94+
95+
## Docker
96+
97+
### Run full stack
98+
99+
```bash
100+
docker-compose --profile full up -d
101+
```
102+
103+
### Run only PostgreSQL
104+
105+
```bash
106+
docker-compose up -d postgres
107+
```
108+
109+
### Build image only
110+
111+
```bash
112+
docker build -t book-api .
113+
```
114+
115+
## Actuator Endpoints
116+
117+
- Health: `GET /actuator/health`
118+
- Info: `GET /actuator/info`
119+
- Metrics: `GET /actuator/metrics`
120+
121+
## Project Structure
122+
123+
```
124+
src/main/java/com/kaluzny/
125+
├── Application.java # Entry point
126+
├── domain/
127+
│ ├── Book.java # JPA Entity
128+
│ └── BookRepository.java # Spring Data Repository
129+
├── exception/
130+
│ ├── BookNotFoundException.java
131+
│ └── GlobalExceptionHandler.java
132+
└── web/
133+
└── BookRestController.java # REST Controller
134+
```
135+
136+
## Testing
137+
138+
```bash
139+
# Run tests (requires PostgreSQL)
140+
mvn test
141+
142+
# Skip tests
143+
mvn package -DskipTests
144+
```
145+
146+
## License
147+
148+
MIT

docker-compose.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
services:
2+
postgres:
3+
image: postgres:16-alpine
4+
container_name: book-db
5+
environment:
6+
POSTGRES_DB: book_db
7+
POSTGRES_USER: postgres
8+
POSTGRES_PASSWORD: postgres
9+
ports:
10+
- "5432:5432"
11+
volumes:
12+
- postgres_data:/var/lib/postgresql/data
13+
healthcheck:
14+
test: ["CMD-SHELL", "pg_isready -U postgres"]
15+
interval: 10s
16+
timeout: 5s
17+
retries: 5
18+
19+
app:
20+
build: .
21+
container_name: book-api
22+
depends_on:
23+
postgres:
24+
condition: service_healthy
25+
environment:
26+
DB_HOST: postgres
27+
DB_PORT: 5432
28+
DB_NAME: book_db
29+
DB_USERNAME: postgres
30+
DB_PASSWORD: postgres
31+
SECURITY_USER: admin
32+
SECURITY_PASSWORD: admin123
33+
ports:
34+
- "8080:8080"
35+
profiles:
36+
- full
37+
38+
volumes:
39+
postgres_data:

0 commit comments

Comments
 (0)