Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jonasfschuh/kotlin-avengers-api

Avengers API Development with Kotlin
https://github.com/jonasfschuh/kotlin-avengers-api

api java-17-maven kotlin

Last synced: 11 days ago
JSON representation

Avengers API Development with Kotlin

Awesome Lists containing this project

README

        

# Kotlin Avengers API

Development of an API using SpringBoot + Kotlin with the aim of registering Avengers.

![Kotlin](https://img.shields.io/badge/kotlin-%237F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)
![Java](https://img.shields.io/badge/java-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white)
![Spring](https://img.shields.io/badge/spring-%236DB33F.svg?style=for-the-badge&logo=spring&logoColor=white)
![Apache Maven](https://img.shields.io/badge/Apache%20Maven-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white)
![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)
![Heroku](https://img.shields.io/badge/heroku-%23430098.svg?style=for-the-badge&logo=heroku&logoColor=white)
![IntelliJ IDEA](https://img.shields.io/badge/IntelliJIDEA-000000.svg?style=for-the-badge&logo=intellij-idea&logoColor=white)

## Technologies / Frameworks / IDE

- Intellij 2023.3.7
- SpringBoot 3.3.3
- Maven
- Kotlin
- SpringData JPA
- PostgreSQL
- Flyway
- Java 17
- Heroku

## Criação do esqueleto do projeto

- https://start.spring.io/
-
[spring initializr](https://start.spring.io/#!type=maven-project&language=kotlin&platformVersion=3.3.3&packaging=jar&jvmVersion=17&groupId=io.github.jonasfschuh&artifactId=kotlin-avengers-api&name=kotlin-avengers-api&description=Avengers%20API%20Development%20with%20Kotlin&packageName=io.github.jonasfschuh.kotlin-avengers-api&dependencies=web,data-jpa,lombok,cloud-resilience4j,postgresql,flyway,devtools,validation)

## Define API Contract

- https://editor.swagger.io/

- Resource `avenger`
- GET - 200 OK
- GET {id}/detail - 200 OK ou 404 Not Found
- POST - 201 Created ou 400 Bad Request
- PUT {id} - 202 Accepted ou 404 Not Found
- DELETE {id} - 202 Accepted ou 404 Not Found

```json
{
"nick": "spider-man",
"person": "Peter Parker",
"description": "about powers",
"history": "history"
}
```

## Architectural Design

- Application Layer (controllers, configs, exception handle, request and response data, bean validations)
- Domain Layer (Avenger model, repository interface, service)
- Infrastructure Layer (JPA repository, Avenger entity, proxy implements repository interface and uses the JPA repository to communicate with the database)
- Tests

### Flyway (db/migration)

```sql
create table avenger (
id bigserial not null,
nick varchar(36),
person varchar(128),
description varchar(128),
history text,
primary key (id)
);

alter table avenger add constraint UK_5r88eemotwgru6k0ilqb2ledh unique (nick);
```

### Profiles

- application.yaml
- application-dev.yaml
- application-heroku.yaml

```yaml
spring:
application:
name: kotlin-avengers-api
config:
# This configuration allow use profiles as spring 2.3.x version
# In spring 2.4.x version, has changed to:
# spring:
# profiles:
# group:
# : dev, auth
use-legacy-processing: true
profiles:
active: dev
jmx:
enabled: false
data:
jpa:
repositories:
bootstrap-mode: deferred
jpa:
open-in-view: false
properties:
hibernate.jdbc.time_zone: UTC
hibernate.generate_statistics: false
hibernate.jdbc.batch_size: 25
hibernate.order_inserts: true
hibernate.order_updates: true
hibernate.query.fail_on_pagination_over_collection_fetch: true
hibernate.query.in_clause_parameter_padding: true
hibernate:
ddl-auto: none
naming:
physical_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
main:
allow-bean-definition-overriding: true
task:
execution:
thread-name-prefix: avengers-task-
pool:
core-size: 2
max-size: 50
queue-capacity: 10000
scheduling:
thread-name-prefix: avengers-scheduling-
pool:
size: 2
output:
ansi:
console-available: true

server:
port: 9090
servlet:
session:
cookie:
http-only: true
context-path: /avengers
```

```yaml
spring:
# profiles:
# active: dev
jackson:
serialization:
indent-output: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://localhost:25433/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
```

```yaml
spring:
profiles:
active: heroku
jackson:
serialization:
indent-output: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://${HOST}/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
show-sql: false
```

## Dcoker

### Environment Config

```sh
DB_USER=dio.avenger
DB_PASSWORD=dio.avenger
DB_NAME=avengers
```

### YAML (backend-services.yaml)

```yaml
version: '3.2'
services:
postgres:
image: postgres:12-alpine
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_DB: ${DB_NAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ALLOW_IP_RANGE: 0.0.0.0/0
ports:
- "25433:5432"
volumes:
- pdb12:/var/lib/postgresql/data
networks:
- postgres-compose-network

teste-pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "[email protected]"
PGADMIN_DEFAULT_PASSWORD: "123456"
ports:
- "5556:80"
depends_on:
- postgres
networks:
- postgres-compose-network

volumes:
pdb12:
networks:
postgres-compose-network:
driver: bridge
```

### Script / Commands

- `docker-compose -f backend-services.yaml up -d` (deploy) / `docker-compose -f backend-services.yaml down` (undeploy)

- Start API
```sh
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev -Dspring-boot.run.jvmArguments="-Xmx256m -Xms128m" -Dspring-boot.run.arguments="'--DB_USER=avenger' '--DB_PASSWORD=avenger' '--DB_NAME=avengers'"
```

## Heroku

- Criar app
- Linkar com Github
- Setar variáveis de ambiente

### Procfile

```text
web: java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap $JAVA_OPTS -Dserver.port=$PORT -Dspring.profiles.active=heroku -jar target/*.jar
```

### API URL
http://localhost:9090/avengers/v1/api/avenger