From 918d3272d0ce7c3661a05db3f452ce3a62a8b1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20J=2E=20Schilling?= Date: Wed, 19 Jun 2024 12:16:59 +0200 Subject: [PATCH] Fix CORS issue --- chucknorris-integration-test/build.gradle | 2 + .../docker-compose.yml | 38 +++++++++++++++++++ .../script/run-integration-test.sh | 16 ++++++++ .../java/io/chucknorris/api/Application.java | 16 ++++++++ .../CrossOriginResourceSharingConfig.java | 28 -------------- docker-compose.yml | 8 ---- gradle.properties | 1 + settings.gradle | 1 + 8 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 chucknorris-integration-test/build.gradle create mode 100644 chucknorris-integration-test/docker-compose.yml create mode 100755 chucknorris-integration-test/script/run-integration-test.sh delete mode 100644 chucknorris-web/src/main/java/io/chucknorris/api/configuration/CrossOriginResourceSharingConfig.java delete mode 100644 docker-compose.yml create mode 100644 gradle.properties diff --git a/chucknorris-integration-test/build.gradle b/chucknorris-integration-test/build.gradle new file mode 100644 index 0000000..7d82dc7 --- /dev/null +++ b/chucknorris-integration-test/build.gradle @@ -0,0 +1,2 @@ +dependencies { +} diff --git a/chucknorris-integration-test/docker-compose.yml b/chucknorris-integration-test/docker-compose.yml new file mode 100644 index 0000000..e159f27 --- /dev/null +++ b/chucknorris-integration-test/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3.8' + +services: + database: + image: chucknorrisio/postgres + container_name: chucknorris-database + healthcheck: + interval: 30s + retries: 5 + start_period: 80s + test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ] + timeout: 60s + ports: + - '5432:5432' + + service: + environment: + - APPLICATION_EVENT_SNS_TOPIC_ARN=my-application-event-sns-topic-arn + - AWS_ACCESS_KEY_ID=my-aws-access-key-id + - AWS_ACCESS_KEY_SECRET=my-aws-access-key-secret + - AWS_REGION=my-aws-region + - DAILYCHUCK_BUCKET_NAME=my-dailychuck-bucket-name + - DAILYCHUCK_KEY_NAME=my-dailychuck-key-name + - DATABASE_URL=postgres://postgres:postgres@database:5432/chuck + - MAILCHIMP_API_KEY=my-mailchimp-api-key + - MAILCHIMP_DAILYCHUCK_LIST_ID=my-mailchimp-dailychuck-list-id + - SLACK_CONTENT_WHITELISTED_CATEGORIES=career,dev,fashion,food,money,movie,travel + - SLACK_OAUTH_ACCESS_TOKEN=xxxx-xxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + - SLACK_OAUTH_CLIENT_ID=xxxxxxxxxxx.xxxxxxxxxxxx + - SLACK_OAUTH_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + - SLACK_OAUTH_REDIRECT_URI=https://api.chucknorris.io/connect/slack + image: chucknorris/chucknorris-web + container_name: chucknorris-web + depends_on: + database: + condition: service_healthy + ports: + - '8080:8080' diff --git a/chucknorris-integration-test/script/run-integration-test.sh b/chucknorris-integration-test/script/run-integration-test.sh new file mode 100755 index 0000000..74243ed --- /dev/null +++ b/chucknorris-integration-test/script/run-integration-test.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +readonly SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) +readonly MODULE_DIR=$(cd "${SCRIPT_DIR}/.." && pwd) +readonly PROJECT_DIR=$(cd "${SCRIPT_DIR}/../.." && pwd) + +./gradlew --build-file "$PROJECT_DIR/build.gradle" clean dockerTagCurrent + +# docker-compose --file "$MODULE_DIR/docker-compose.yml" up \ +# --exit-code-from "integration-test" \ +# --quiet-pull \ +# --remove-orphans + +docker-compose --file "$MODULE_DIR/docker-compose.yml" up \ + --quiet-pull \ + --remove-orphans \ No newline at end of file diff --git a/chucknorris-web/src/main/java/io/chucknorris/api/Application.java b/chucknorris-web/src/main/java/io/chucknorris/api/Application.java index 5b6e859..6304b36 100644 --- a/chucknorris-web/src/main/java/io/chucknorris/api/Application.java +++ b/chucknorris-web/src/main/java/io/chucknorris/api/Application.java @@ -4,12 +4,28 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @ComponentScan(basePackages = {"io.chucknorris"}) @EnableJpaAuditing @SpringBootApplication public class Application { + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry + .addMapping("/**") + .allowCredentials(true) + .allowedHeaders("*") + .allowedMethods("*") + .allowedOrigins("*"); + } + }; + } + public static void main(String[] args) { SpringApplication.run(Application.class, args); } diff --git a/chucknorris-web/src/main/java/io/chucknorris/api/configuration/CrossOriginResourceSharingConfig.java b/chucknorris-web/src/main/java/io/chucknorris/api/configuration/CrossOriginResourceSharingConfig.java deleted file mode 100644 index 7307af0..0000000 --- a/chucknorris-web/src/main/java/io/chucknorris/api/configuration/CrossOriginResourceSharingConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package io.chucknorris.api.configuration; - -import org.springframework.boot.web.servlet.FilterRegistrationBean; -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 org.springframework.web.filter.CorsFilter; - -@Configuration -public class CrossOriginResourceSharingConfig { - - /** Returns cors filter registration bean {@link CorsConfiguration}. */ - public @Bean FilterRegistrationBean corsFilter() { - CorsConfiguration config = new CorsConfiguration(); - config.setAllowCredentials(true); - config.addAllowedOrigin("*"); - config.addAllowedHeader("*"); - config.addAllowedMethod("*"); - - UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); - source.registerCorsConfiguration("/**", config); - FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); - bean.setOrder(0); - - return bean; - } -} diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index e20e41e..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: '3.3' - -services: - postgres: - image: chucknorrisio/postgres - container_name: chucknorris-postgres - ports: - - '5432:5432' \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..8168ba5 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +org.gradle.logging.level=quiet \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 9c51228..4b1e181 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,4 +9,5 @@ rootProject.name = 'chucknorris' +include("chucknorris-integration-test") include("chucknorris-web")