Compare commits

...

2 Commits

6 changed files with 87 additions and 43 deletions

9
.env.example Normal file
View File

@ -0,0 +1,9 @@
SPRING_PROFILES_ACTIVE=dev
APP_ALLOWED_ORIGINS='http://127.0.0.1:3000, http://localhost:3000'
DB_NAME=EXAMPLE_DB
DB_USER=EXAMPLE
DB_PASSWORD=SECRET
DB_HOST=127.0.0.1
DB_PORT=5432

2
.gitignore vendored
View File

@ -2,5 +2,5 @@
*.toml
*.db
target
.env
Icon?

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
# Stage 1: Build
FROM maven:3.9-eclipse-temurin-21-alpine AS build
WORKDIR /app
# Copiamos todo el proyecto
COPY . .
# Compilamos todo el proyecto (todos los módulos)
RUN mvn clean package -DskipTests
# Stage 2: Run
FROM openjdk:21-jdk
WORKDIR /app
COPY --from=build /app/bootstrap/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-Xmx512m", "-Xms256m", "-jar", "app.jar","--spring.profiles.active=prod"]

View File

@ -1,6 +1,9 @@
info:
app:
version: @project.version@
app:
cors:
allowed-origins: ${APP_ALLOWED_ORIGINS:http://localhost:8080}
spring:
application:
name: portfolio-api
@ -8,13 +11,26 @@ spring:
resources:
add-mappings: false
datasource:
url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:portfolio}
username: ${DB_USER:postgres}
password: ${DB_PASSWORD:postgres}
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 3
minimum-idle: 1
idle-timeout: 30000
connection-timeout: 10000
leak-detection-threshold: 10000
jpa:
hibernate:
ddl-auto: update
ddl-auto: validate
properties:
hibernate.transaction.jta.platform: org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform
hibernate:
format_sql: true
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
jackson:
@ -32,46 +48,7 @@ server:
servlet:
context-path: /api
forward-headers-strategy: framework
---
spring:
config:
activate:
on-profile: default
datasource:
url: jdbc:h2:file:./portfolio-db
driver-class-name: org.h2.Driver
username: sa
password:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
path: /h2-console
---
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:portfolio}
username: ${DB_USER:postgres}
password: ${DB_PASSWORD:postgres}
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 3
minimum-idle: 1
idle-timeout: 30000
connection-timeout: 10000
leak-detection-threshold: 10000
jpa:
hibernate:
ddl-auto: update

35
docker-compose.yml Normal file
View File

@ -0,0 +1,35 @@
services:
db:
image: postgres:15-alpine
restart: unless-stopped
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ports:
- "5432:5432"
networks:
common_network:
ipv4_address: 10.1.0.121
volumes:
- db_data:/var/lib/postgresql/data
api:
build: .
restart: unless-stopped
ports:
- "8095:8080"
networks:
common_network:
ipv4_address: 10.1.0.121
environment:
DB_NAME: ${DB_NAME}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
depends_on:
- db
volumes:
db_data:

View File

@ -1,5 +1,6 @@
package com.pablotj.portfolio.infrastructure.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -7,10 +8,16 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Value("${app.cors.allowed-origins}")
private String allowedOriginsString;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // todos los endpoints que comiencen con /api/
.allowedOrigins("http://127.0.0.1:3000", "http://localhost:3000", "https://pablotj.com")
String [] allowedOrigins = allowedOriginsString.split(",");
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}