Skip to content

Commit

Permalink
Merge pull request #43 from edu-pi/develop
Browse files Browse the repository at this point in the history
[#42]deploy: gateway 서버 배포
  • Loading branch information
SongGwanSeok authored Nov 7, 2024
2 parents 27ac1bb + 05e08e4 commit 4c3b504
Show file tree
Hide file tree
Showing 18 changed files with 583 additions and 26 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/cicd-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Deploy to Amazon ECS

on:
push:
branches: [ "main" ]

env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: edupi_gateway
ECS_SERVICE: gateway
ECS_CLUSTER: BE-fargate
ECS_TASK_DEFINITION: edupi_gateway.json
CONTAINER_NAME: gateway

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Copy Secret yml
env:
CREATE_SECRET: ${{secrets.EDUPI_GATEWAY_DEV_APPLICATION_YML}}
CREATE_SECRET_DIR: src/main/resources
CREATE_SECRET_DIR_FILE_NAME: application.yml
run: echo $CREATE_SECRET | base64 --decode > $CREATE_SECRET_DIR/$CREATE_SECRET_DIR_FILE_NAME

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_ID }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: latest
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
45 changes: 45 additions & 0 deletions .github/workflows/pull-request-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Pull Request build

on:
pull_request:
branches: [ "develop" ]

permissions:
contents: read

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Copy Secret yml
env:
CREATE_SECRET: ${{secrets.EDUPI_GATEWAY_DEV_APPLICATION_YML}}
CREATE_SECRET_DIR: src/main/resources
CREATE_SECRET_DIR_FILE_NAME: application.yml
run: echo $CREATE_SECRET | base64 --decode > $CREATE_SECRET_DIR/$CREATE_SECRET_DIR_FILE_NAME

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Test with Gradle
run: ./gradlew test

- name: Cleanup Gradle Cache
if: ${{ always() }}
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ out/
### VS Code ###
.vscode/

*.yml
src/main/resources/*.yml
application.properties

.DS_Store
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openjdk:17

ARG JAR_FILE=build/libs/*.jar

COPY ${JAR_FILE} app.jar

ENTRYPOINT ["java","-jar","/app.jar"]
39 changes: 22 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
id 'java'
id 'org.springframework.boot' version '3.3.2'
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'soma.haeya'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

repositories {
mavenCentral()
mavenCentral()
}

ext {
set('springCloudVersion', "2023.0.3")
set('springCloudVersion', "2023.0.3")
}

dependencies {
// spring cloud gateway
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// spring cloud gateway
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'

// jjwt
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.0.2'

// jjwt
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}
53 changes: 53 additions & 0 deletions edupi_gateway.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"containerDefinitions": [
{
"name": "gateway",
"image": "590184013289.dkr.ecr.ap-northeast-2.amazonaws.com/edupi_gateway:latest",
"cpu": 0,
"portMappings": [
{
"name": "gateway-port",
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"environmentFiles": [],
"mountPoints": [],
"volumesFrom": [],
"ulimits": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/edupi_gateway",
"mode": "non-blocking",
"awslogs-create-group": "true",
"max-buffer-size": "25m",
"awslogs-region": "ap-northeast-2",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
},
"systemControls": []
}
],
"family": "edupi_gateway",
"executionRoleArn": "arn:aws:iam::590184013289:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"volumes": [],
"placementConstraints": [],
"requiresCompatibilities": [
"EC2",
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
},
"tags": []
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package soma.haeya.edupi_gateway;
package soma.edupigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EdupiGatewayApplication {

public static void main(String[] args) {
SpringApplication.run(EdupiGatewayApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(EdupiGatewayApplication.class, args);
}

}
43 changes: 43 additions & 0 deletions src/main/java/soma/edupigateway/auth/TokenProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package soma.edupigateway.auth;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.util.Date;
import javax.crypto.SecretKey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import soma.edupigateway.exception.UnAuthorizedException;

@Component
public class TokenProvider {

private final SecretKey key;

public TokenProvider(@Value("${jwt.secret}") String secret) {
byte[] keyBytes = Decoders.BASE64.decode(secret);
this.key = Keys.hmacShaKeyFor(keyBytes);
}

public Claims getClaimsJson(String token) {
Claims claims = extractClaimsFromJwt(token);

if (claims.getExpiration().before(new Date())) {
throw new UnAuthorizedException(HttpStatus.UNAUTHORIZED, "토큰이 유효하지 않습니다.");
}

return claims;
}

private Claims extractClaimsFromJwt(String token) {
try {
return Jwts.parser().verifyWith(key).build().parseSignedClaims(token).getPayload();
} catch (JwtException e) {
throw new UnAuthorizedException(HttpStatus.UNAUTHORIZED, "토큰이 유효하지 않습니다.");
}
}

}
43 changes: 43 additions & 0 deletions src/main/java/soma/edupigateway/config/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package soma.edupigateway.config;

import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

@Bean
public CorsWebFilter corsWebFilter() {
return new CorsWebFilter(corsConfigurationSource());
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(
List.of(
"http://localhost:5000",
"http://d33notepxaalcd.cloudfront.net",
"https://d33notepxaalcd.cloudfront.net",
"http://edupi.co.kr",
"https://edupi.co.kr"
)
);
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(
List.of("Content-Type", "Authorization", "Accept", "Access-Control-Allow-Origin"));
configuration.setExposedHeaders(List.of("Content-Type"));
configuration.setAllowCredentials(true);
configuration.setMaxAge(3600L);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);

return source;
}
}
Loading

0 comments on commit 4c3b504

Please sign in to comment.