Compare commits
3 Commits
0d676b2803
...
abe411ce62
| Author | SHA1 | Date | |
|---|---|---|---|
| abe411ce62 | |||
| 2b6433d57f | |||
| 5c201f4189 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
target/
|
||||||
22
Dockerfile
22
Dockerfile
@ -1,30 +1,18 @@
|
|||||||
# Dockerfile multi-stage para otimizar o tamanho da imagem
|
FROM maven:3.9.9-eclipse-temurin-24 AS builder
|
||||||
# FROM openjdk:26-jdk-slim AS builder
|
|
||||||
FROM openjdk:26-jdk-slim
|
|
||||||
|
|
||||||
# Definir diretório de trabalho
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copiar arquivos de dependências
|
|
||||||
COPY pom.xml .
|
COPY pom.xml .
|
||||||
COPY mvnw .
|
|
||||||
COPY .mvn .mvn
|
|
||||||
|
|
||||||
# Dar permissão de execução ao mvnw
|
|
||||||
RUN chmod +x mvnw
|
|
||||||
|
|
||||||
|
RUN mvn wrapper:wrapper
|
||||||
# Baixar dependências (cache layer)
|
# Baixar dependências (cache layer)
|
||||||
RUN ./mvnw dependency:go-offline -B
|
RUN ./mvnw dependency:go-offline -B
|
||||||
|
|
||||||
# Copiar código fonte
|
# Copiar código fonte
|
||||||
COPY src src
|
COPY src src
|
||||||
|
|
||||||
# Compilar aplicação
|
# Compilar aplicação
|
||||||
RUN ./mvnw clean package -DskipTests
|
RUN ./mvnw clean package -DskipTests
|
||||||
|
FROM openjdk:26-jdk-slim AS runtime
|
||||||
# Stage final com imagem otimizada
|
|
||||||
# FROM openjdk:26-jre-slim
|
|
||||||
|
|
||||||
# Instalar curl para health checks
|
# Instalar curl para health checks
|
||||||
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
@ -35,8 +23,8 @@ RUN groupadd -r spring && useradd -r -g spring spring
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copiar JAR da aplicação do stage anterior
|
# Copiar JAR da aplicação do stage anterior
|
||||||
# COPY --from=builder /app/target/*.jar app.jar
|
COPY --from=builder /app/target/*.jar app.jar
|
||||||
COPY /app/target/*.jar app.jar
|
# COPY /app/target/*.jar app.jar
|
||||||
|
|
||||||
# Definir propriedades do usuário
|
# Definir propriedades do usuário
|
||||||
RUN chown spring:spring app.jar
|
RUN chown spring:spring app.jar
|
||||||
|
|||||||
@ -28,7 +28,7 @@ services:
|
|||||||
SPRING_DATASOURCE_PASSWORD: senha123
|
SPRING_DATASOURCE_PASSWORD: senha123
|
||||||
SPRING_JPA_HIBERNATE_DDL_AUTO: update
|
SPRING_JPA_HIBERNATE_DDL_AUTO: update
|
||||||
ports:
|
ports:
|
||||||
- "8080:8080"
|
- "8282:8080"
|
||||||
networks:
|
networks:
|
||||||
- tarefas-network
|
- tarefas-network
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
61
flake.lock
generated
Normal file
61
flake.lock
generated
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1731533236,
|
||||||
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "flake-utils",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1749794982,
|
||||||
|
"narHash": "sha256-Kh9K4taXbVuaLC0IL+9HcfvxsSUx8dPB5s5weJcc9pc=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "ee930f9755f58096ac6e8ca94a1887e0534e2d81",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-utils": "flake-utils",
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"systems": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1681028828,
|
||||||
|
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nix-systems",
|
||||||
|
"repo": "default",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
118
flake.nix
Normal file
118
flake.nix
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
{
|
||||||
|
description = "Ambiente de desenvolvimento para API de Tarefas Spring Boot";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs, flake-utils }:
|
||||||
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
|
let
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
|
||||||
|
# Versões específicas para consistência
|
||||||
|
jdk = pkgs.openjdk17;
|
||||||
|
maven = pkgs.maven;
|
||||||
|
|
||||||
|
# Script para inicializar o banco MySQL
|
||||||
|
mysql-init-script = pkgs.writeScriptBin "mysql-init" ''
|
||||||
|
#!/bin/bash
|
||||||
|
echo "🗄️ Iniciando MySQL..."
|
||||||
|
|
||||||
|
# Criar diretório para dados MySQL se não existir
|
||||||
|
mkdir -p .devenv/mysql
|
||||||
|
|
||||||
|
# Inicializar MySQL se necessário
|
||||||
|
if [ ! -d ".devenv/mysql/mysql" ]; then
|
||||||
|
echo "📦 Inicializando banco de dados MySQL..."
|
||||||
|
${pkgs.mysql80}/bin/mysqld --initialize-insecure \
|
||||||
|
--datadir=.devenv/mysql \
|
||||||
|
--basedir=${pkgs.mysql80}
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Iniciar servidor MySQL
|
||||||
|
echo "🚀 Iniciando servidor MySQL..."
|
||||||
|
${pkgs.mysql80}/bin/mysqld \
|
||||||
|
--datadir=.devenv/mysql \
|
||||||
|
--socket=.devenv/mysql.sock \
|
||||||
|
--port=3306 \
|
||||||
|
--bind-address=127.0.0.1 \
|
||||||
|
--skip-networking=false &
|
||||||
|
|
||||||
|
# Aguardar MySQL inicializar
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
# Criar banco e usuário
|
||||||
|
echo "👤 Configurando banco e usuário..."
|
||||||
|
${pkgs.mysql80}/bin/mysql \
|
||||||
|
--socket=.devenv/mysql.sock \
|
||||||
|
-u root \
|
||||||
|
-e "CREATE DATABASE IF NOT EXISTS tarefas_db;"
|
||||||
|
|
||||||
|
${pkgs.mysql80}/bin/mysql \
|
||||||
|
--socket=.devenv/mysql.sock \
|
||||||
|
-u root \
|
||||||
|
-e "CREATE USER IF NOT EXISTS 'tarefas_user'@'localhost' IDENTIFIED BY 'senha123';"
|
||||||
|
|
||||||
|
${pkgs.mysql80}/bin/mysql \
|
||||||
|
--socket=.devenv/mysql.sock \
|
||||||
|
-u root \
|
||||||
|
-e "GRANT ALL PRIVILEGES ON tarefas_db.* TO 'tarefas_user'@'localhost';"
|
||||||
|
|
||||||
|
${pkgs.mysql80}/bin/mysql \
|
||||||
|
--socket=.devenv/mysql.sock \
|
||||||
|
-u root \
|
||||||
|
-e "FLUSH PRIVILEGES;"
|
||||||
|
|
||||||
|
echo "✅ MySQL configurado com sucesso!"
|
||||||
|
'';
|
||||||
|
|
||||||
|
# Script para executar a aplicação
|
||||||
|
run-app-script = pkgs.writeScriptBin "run-app" ''
|
||||||
|
#!/bin/bash
|
||||||
|
echo "🚀 Executando aplicação Spring Boot..."
|
||||||
|
|
||||||
|
# Verificar se MySQL está rodando
|
||||||
|
if ! pgrep -f mysqld > /dev/null; then
|
||||||
|
echo "❌ MySQL não está rodando. Execute 'mysql-init' primeiro."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Executar aplicação
|
||||||
|
${maven}/bin/mvn spring-boot:run \
|
||||||
|
-Dspring-boot.run.profiles=dev
|
||||||
|
'';
|
||||||
|
|
||||||
|
in
|
||||||
|
{
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
# Java e Maven
|
||||||
|
jdk
|
||||||
|
maven
|
||||||
|
|
||||||
|
# Banco de dados
|
||||||
|
mysql80
|
||||||
|
|
||||||
|
# Scripts customizados
|
||||||
|
mysql-init-script
|
||||||
|
run-app-script
|
||||||
|
|
||||||
|
# Ferramentas
|
||||||
|
curl
|
||||||
|
docker
|
||||||
|
kubectl
|
||||||
|
];
|
||||||
|
|
||||||
|
shellHook = ''
|
||||||
|
echo "🚀 Ambiente de desenvolvimento Spring Boot"
|
||||||
|
echo "Execute 'mysql-init' para configurar MySQL"
|
||||||
|
echo "Execute 'run-app' para iniciar a aplicação"
|
||||||
|
|
||||||
|
export JAVA_HOME="${jdk}"
|
||||||
|
export PATH="${jdk}/bin:${maven}/bin:$PATH"
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
89
k8s/app-deployment.yaml
Normal file
89
k8s/app-deployment.yaml
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: tarefas-api
|
||||||
|
namespace: tarefas-app
|
||||||
|
spec:
|
||||||
|
replicas: 2
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: tarefas-api
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: tarefas-api
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: tarefas-api
|
||||||
|
image: gitea.adnangonzagaci.com/adnangonzaga/tarefas-app/tarefas-api:latest
|
||||||
|
imagePullPolicy: Always
|
||||||
|
env:
|
||||||
|
- name: SPRING_DATASOURCE_URL
|
||||||
|
value: "jdbc:mysql://mysql:3306/tarefas_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||||
|
- name: SPRING_DATASOURCE_USERNAME
|
||||||
|
value: "tarefas_user"
|
||||||
|
- name: SPRING_DATASOURCE_PASSWORD
|
||||||
|
value: "senha123"
|
||||||
|
- name: SPRING_JPA_HIBERNATE_DDL_AUTO
|
||||||
|
value: "update"
|
||||||
|
- name: SPRING_PROFILES_ACTIVE
|
||||||
|
value: "production"
|
||||||
|
ports:
|
||||||
|
- containerPort: 8080
|
||||||
|
livenessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /api/actuator/health
|
||||||
|
port: 8080
|
||||||
|
initialDelaySeconds: 60
|
||||||
|
periodSeconds: 10
|
||||||
|
timeoutSeconds: 5
|
||||||
|
failureThreshold: 3
|
||||||
|
readinessProbe:
|
||||||
|
httpGet:
|
||||||
|
path: /api/actuator/health
|
||||||
|
port: 8080
|
||||||
|
initialDelaySeconds: 30
|
||||||
|
periodSeconds: 5
|
||||||
|
timeoutSeconds: 3
|
||||||
|
failureThreshold: 3
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: "1Gi"
|
||||||
|
cpu: "500m"
|
||||||
|
requests:
|
||||||
|
memory: "512Mi"
|
||||||
|
cpu: "250m"
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: tarefas-api
|
||||||
|
namespace: tarefas-app
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: tarefas-api
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: 8080
|
||||||
|
type: ClusterIP
|
||||||
|
---
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: tarefas-api-ingress
|
||||||
|
namespace: tarefas-app
|
||||||
|
annotations:
|
||||||
|
nginx.ingress.kubernetes.io/rewrite-target: /
|
||||||
|
spec:
|
||||||
|
ingressClassName: traefik
|
||||||
|
rules:
|
||||||
|
- host: tarefas.adnangonzaga.com
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- path: /
|
||||||
|
pathType: Prefix
|
||||||
|
backend:
|
||||||
|
service:
|
||||||
|
name: tarefas-api
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
68
k8s/mysql-deployment.yaml
Normal file
68
k8s/mysql-deployment.yaml
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: mysql
|
||||||
|
namespace: tarefas-app
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: mysql
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mysql
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: mysql
|
||||||
|
image: mysql:8.0
|
||||||
|
env:
|
||||||
|
- name: MYSQL_ROOT_PASSWORD
|
||||||
|
value: "senha123"
|
||||||
|
- name: MYSQL_DATABASE
|
||||||
|
value: "tarefas_db"
|
||||||
|
- name: MYSQL_USER
|
||||||
|
value: "tarefas_user"
|
||||||
|
- name: MYSQL_PASSWORD
|
||||||
|
value: "senha123"
|
||||||
|
ports:
|
||||||
|
- containerPort: 3306
|
||||||
|
volumeMounts:
|
||||||
|
- name: mysql-storage
|
||||||
|
mountPath: /var/lib/mysql
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: "1Gi"
|
||||||
|
cpu: "500m"
|
||||||
|
requests:
|
||||||
|
memory: "512Mi"
|
||||||
|
cpu: "250m"
|
||||||
|
volumes:
|
||||||
|
- name: mysql-storage
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: mysql-pvc
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: mysql
|
||||||
|
namespace: tarefas-app
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: mysql
|
||||||
|
ports:
|
||||||
|
- port: 3306
|
||||||
|
targetPort: 3306
|
||||||
|
type: ClusterIP
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
metadata:
|
||||||
|
name: mysql-pvc
|
||||||
|
namespace: tarefas-app
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 10Gi
|
||||||
6
k8s/namespace.yaml
Normal file
6
k8s/namespace.yaml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Namespace
|
||||||
|
metadata:
|
||||||
|
name: tarefas-app
|
||||||
|
labels:
|
||||||
|
name: tarefas-app
|
||||||
56
pom.xml
56
pom.xml
@ -0,0 +1,56 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.exemplo</groupId>
|
||||||
|
<artifactId>tarefas-api</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>tarefas-api</name>
|
||||||
|
<description>API para gerenciamento de tarefas</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.33</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@ -1,12 +0,0 @@
|
|||||||
target/
|
|
||||||
!target/*.jar
|
|
||||||
.mvn/wrapper/maven-wrapper.jar
|
|
||||||
.git
|
|
||||||
.gitignore
|
|
||||||
README.md
|
|
||||||
Dockerfile
|
|
||||||
.dockerignore
|
|
||||||
*.md
|
|
||||||
*.log
|
|
||||||
.DS_Store
|
|
||||||
node_modules/
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.exemplo.tarefasapi;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class TarefasApiApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(TarefasApiApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/com/exemplo/tarefasapi/config/WebConfig.java
Normal file
17
src/main/java/com/exemplo/tarefasapi/config/WebConfig.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.exemplo.tarefasapi.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/**")
|
||||||
|
.allowedOrigins("*")
|
||||||
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||||
|
.allowedHeaders("*");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,103 @@
|
|||||||
|
package com.exemplo.tarefasapi.controller;
|
||||||
|
|
||||||
|
import com.exemplo.tarefasapi.model.Tarefa;
|
||||||
|
import com.exemplo.tarefasapi.repository.TarefaRepository;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/tarefas")
|
||||||
|
@CrossOrigin(origins = "*")
|
||||||
|
public class TarefaController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TarefaRepository tarefaRepository;
|
||||||
|
|
||||||
|
// Criar uma nova tarefa
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<Tarefa> criarTarefa(@Valid @RequestBody Tarefa tarefa) {
|
||||||
|
try {
|
||||||
|
Tarefa novaTarefa = tarefaRepository.save(tarefa);
|
||||||
|
return new ResponseEntity<>(novaTarefa, HttpStatus.CREATED);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar todas as tarefas
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<List<Tarefa>> obterTodasTarefas() {
|
||||||
|
try {
|
||||||
|
List<Tarefa> tarefas = tarefaRepository.findAll();
|
||||||
|
if (tarefas.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(tarefas, HttpStatus.OK);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar uma tarefa específica pelo ID
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<Tarefa> obterTarefaPorId(@PathVariable("id") Long id) {
|
||||||
|
Optional<Tarefa> tarefa = tarefaRepository.findById(id);
|
||||||
|
|
||||||
|
if (tarefa.isPresent()) {
|
||||||
|
return new ResponseEntity<>(tarefa.get(), HttpStatus.OK);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Atualizar uma tarefa existente
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<Tarefa> atualizarTarefa(@PathVariable("id") Long id,
|
||||||
|
@Valid @RequestBody Tarefa tarefaAtualizada) {
|
||||||
|
Optional<Tarefa> tarefaExistente = tarefaRepository.findById(id);
|
||||||
|
|
||||||
|
if (tarefaExistente.isPresent()) {
|
||||||
|
Tarefa tarefa = tarefaExistente.get();
|
||||||
|
tarefa.setNome(tarefaAtualizada.getNome());
|
||||||
|
tarefa.setDataEntrega(tarefaAtualizada.getDataEntrega());
|
||||||
|
tarefa.setResponsavel(tarefaAtualizada.getResponsavel());
|
||||||
|
|
||||||
|
return new ResponseEntity<>(tarefaRepository.save(tarefa), HttpStatus.OK);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remover uma tarefa
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<HttpStatus> removerTarefa(@PathVariable("id") Long id) {
|
||||||
|
try {
|
||||||
|
Optional<Tarefa> tarefa = tarefaRepository.findById(id);
|
||||||
|
if (tarefa.isPresent()) {
|
||||||
|
tarefaRepository.deleteById(id);
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
} else {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar tarefas por responsável
|
||||||
|
@GetMapping("/responsavel/{responsavel}")
|
||||||
|
public ResponseEntity<List<Tarefa>> obterTarefasPorResponsavel(@PathVariable("responsavel") String responsavel) {
|
||||||
|
try {
|
||||||
|
List<Tarefa> tarefas = tarefaRepository.findByResponsavel(responsavel);
|
||||||
|
return new ResponseEntity<>(tarefas, HttpStatus.OK);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
82
src/main/java/com/exemplo/tarefasapi/model/Tarefa.java
Normal file
82
src/main/java/com/exemplo/tarefasapi/model/Tarefa.java
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package com.exemplo.tarefasapi.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "tarefas")
|
||||||
|
public class Tarefa {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@NotBlank(message = "Nome da tarefa é obrigatório")
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String nome;
|
||||||
|
|
||||||
|
@NotNull(message = "Data de entrega é obrigatória")
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd")
|
||||||
|
@Column(name = "data_entrega", nullable = false)
|
||||||
|
private LocalDate dataEntrega;
|
||||||
|
|
||||||
|
@NotBlank(message = "Responsável é obrigatório")
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String responsavel;
|
||||||
|
|
||||||
|
// Construtores
|
||||||
|
public Tarefa() {}
|
||||||
|
|
||||||
|
public Tarefa(String nome, LocalDate dataEntrega, String responsavel) {
|
||||||
|
this.nome = nome;
|
||||||
|
this.dataEntrega = dataEntrega;
|
||||||
|
this.responsavel = responsavel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters e Setters
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNome() {
|
||||||
|
return nome;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNome(String nome) {
|
||||||
|
this.nome = nome;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getDataEntrega() {
|
||||||
|
return dataEntrega;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDataEntrega(LocalDate dataEntrega) {
|
||||||
|
this.dataEntrega = dataEntrega;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResponsavel() {
|
||||||
|
return responsavel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponsavel(String responsavel) {
|
||||||
|
this.responsavel = responsavel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Tarefa{" +
|
||||||
|
"id=" + id +
|
||||||
|
", nome='" + nome + '\'' +
|
||||||
|
", dataEntrega=" + dataEntrega +
|
||||||
|
", responsavel='" + responsavel + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package com.exemplo.tarefasapi.repository;
|
||||||
|
|
||||||
|
import com.exemplo.tarefasapi.model.Tarefa;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface TarefaRepository extends JpaRepository<Tarefa, Long> {
|
||||||
|
|
||||||
|
// Métodos de consulta customizados
|
||||||
|
List<Tarefa> findByResponsavel(String responsavel);
|
||||||
|
|
||||||
|
List<Tarefa> findByDataEntregaBetween(LocalDate dataInicio, LocalDate dataFim);
|
||||||
|
|
||||||
|
@Query("SELECT t FROM Tarefa t WHERE t.nome LIKE %:nome%")
|
||||||
|
List<Tarefa> findByNomeContaining(@Param("nome") String nome);
|
||||||
|
}
|
||||||
29
src/main/resources/application-dev.properties
Normal file
29
src/main/resources/application-dev.properties
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# Configuração específica para desenvolvimento com Nix
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/tarefas_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
|
||||||
|
spring.datasource.username=tarefas_user
|
||||||
|
spring.datasource.password=senha123
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
|
# JPA/Hibernate para desenvolvimento
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||||
|
spring.jpa.properties.hibernate.format_sql=true
|
||||||
|
|
||||||
|
# Logging detalhado para desenvolvimento
|
||||||
|
logging.level.com.exemplo.tarefasapi=DEBUG
|
||||||
|
logging.level.org.springframework.web=DEBUG
|
||||||
|
logging.level.org.hibernate.SQL=DEBUG
|
||||||
|
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
|
||||||
|
|
||||||
|
# Actuator para desenvolvimento
|
||||||
|
management.endpoints.web.exposure.include=*
|
||||||
|
management.endpoint.health.show-details=always
|
||||||
|
|
||||||
|
# Configurações de servidor para dev
|
||||||
|
server.port=8080
|
||||||
|
server.servlet.context-path=/api
|
||||||
|
|
||||||
|
# Hot reload
|
||||||
|
spring.devtools.restart.enabled=true
|
||||||
|
spring.devtools.livereload.enabled=true
|
||||||
24
src/main/resources/application-production.properties
Normal file
24
src/main/resources/application-production.properties
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Configuração para produção
|
||||||
|
spring.datasource.url=${SPRING_DATASOURCE_URL}
|
||||||
|
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
|
||||||
|
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
|
# JPA/Hibernate
|
||||||
|
spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO:validate}
|
||||||
|
spring.jpa.show-sql=false
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
logging.level.com.exemplo.tarefasapi=INFO
|
||||||
|
logging.level.org.springframework.web=INFO
|
||||||
|
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
|
||||||
|
|
||||||
|
# Actuator para health checks
|
||||||
|
management.endpoints.web.exposure.include=health,info
|
||||||
|
management.endpoint.health.show-details=when-authorized
|
||||||
|
management.health.db.enabled=true
|
||||||
|
|
||||||
|
# Server
|
||||||
|
server.port=8080
|
||||||
|
server.servlet.context-path=/api
|
||||||
23
src/main/resources/application.properties
Normal file
23
src/main/resources/application.properties
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Configuração do Banco de Dados MySQL
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/tarefas_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=senha123
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
|
# Configuração do JPA/Hibernate
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||||
|
spring.jpa.properties.hibernate.format_sql=true
|
||||||
|
|
||||||
|
# Configuração do servidor
|
||||||
|
server.port=8080
|
||||||
|
server.servlet.context-path=/api
|
||||||
|
|
||||||
|
# Configuração de encoding
|
||||||
|
spring.http.encoding.charset=UTF-8
|
||||||
|
spring.http.encoding.enabled=true
|
||||||
|
|
||||||
|
# Actuator
|
||||||
|
management.endpoints.web.exposure.include=health,info
|
||||||
|
management.endpoint.health.show-details=when-authorized
|
||||||
Loading…
Reference in New Issue
Block a user