Skip to content
Snippets Groups Projects
Commit d8a9ceed authored by thib's avatar thib
Browse files

(feat): backend simple controller

parent d3ddfcc3
Branches
No related tags found
No related merge requests found
Showing
with 175 additions and 4 deletions
services:
postgres:
image: 'postgres:latest'
container_name: 'todo-db'
restart: 'unless-stopped'
env_file:
- '.env'
environment:
- 'POSTGRES_DB=${POSTGRES_DB}'
- 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
- 'POSTGRES_USER=${POSTGRES_USER}'
ports:
- '5432'
- '5432:5432'
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- todo-network
networks:
todo-network:
driver: bridge
volumes:
pgdata:
driver: local
......@@ -78,6 +78,21 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
......@@ -5,9 +5,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoApplication {
// https://medium.com/@faroukymedia/de-la-th%C3%A9orie-%C3%A0-la-pratique-spring-boot-architecture-hexagonale-et-ddd-pour-des-applications-f1110d83bced
public static void main(String[] args) {
SpringApplication.run(TodoApplication.class, args);
}
}
package com.hepia.backend.config;
import org.springframework.web.filter.CorsFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
import java.util.Collections;
import static org.springframework.http.HttpHeaders.*;
@Configuration
public class BeanConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(Collections.singletonList("http://localhost:4200"));
config.setAllowedHeaders(Arrays.asList(
ORIGIN,
CONTENT_TYPE,
ACCEPT,
AUTHORIZATION
));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
package com.hepia.backend.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true)
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(withDefaults())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(req ->
req.requestMatchers(
"/todos/**"
).permitAll()
.anyRequest()
.authenticated()
)
.sessionManagement(s ->
s.sessionCreationPolicy(STATELESS)
);
return http.build();
}
}
package com.hepia.backend.controller;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/todos")
@RequiredArgsConstructor
public class TodoController {
@GetMapping
public String getTodos() {
return "Hello World!";
}
}
package com.hepia.backend.domain.entity;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "todos")
public class TodoEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private boolean completed;
private String createdAt;
}
package com.hepia.backend.domain.model;
import lombok.*;
import java.time.LocalDateTime;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Todo {
private Long id;
private String name;
private boolean completed;
private LocalDateTime createdAt;
}
spring.application.name=todo
spring:
datasource:
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USER}
password: ${SPRING_DATASOURCE_PASSWORD}
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
database: postgresql
database-platform: org.hibernate.dialect.PostgreSQLDialect
server:
servlet:
context-path: /api/v1
port: 8080
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment