Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
devyubin committed Aug 12, 2023
2 parents 8d7187c + c672381 commit 7a5d426
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 36 deletions.
57 changes: 38 additions & 19 deletions .github/workflows/haemil-CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,55 @@ env:
AWS_CODE_DEPLOY_APPLICATION: haemil-cicd
AWS_CODE_DEPLOY_GROUP: haemil-cicd-deploy-group

permissions:
contents: read

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

steps:
# (1) 기본 체크아웃
- name: Checkout
uses: actions/checkout@v3


# (2) JDK 11 세팅
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'

- name: gradlew에 실행 권한 부여
run: chmod +x ./gradlew

- name: 프로젝트 빌드
run: ./gradlew clean build

- name: AWS credential 설정
java-version: '11'

# (3) Gradle build (Test 제외)
- name: Build with Gradle
uses: gradle/gradle-build-action@0d13054264b0bb894ded474f08ebb30921341cee
with:
arguments: clean build -x test

# (4) AWS 인증 (IAM 사용자 Access Key, Secret Key 활용)
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.IAM_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.IAM_SECRET_ACCESS_KEY }}

- name: S3에 업로드
run: aws deploy push --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --ignore-hidden-files --s3-location s3://$AWS_S3_BUCKET/cicdtest/$GITHUB_SHA.zip --source .

- name: EC2에 배포
run: aws deploy create-deployment --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} --s3-location bucket=$AWS_S3_BUCKET,key=cicdtest/$GITHUB_SHA.zip,bundleType=zip
aws-region: ${{ env.AWS_REGION }}

# (5) 빌드 결과물을 S3 버킷에 업로드
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} \
--ignore-hidden-files \
--s3-location s3://$AWS_S3_BUCKET/$GITHUB_SHA.zip \
--source .
# (6) S3 버킷에 있는 파일을 대상으로 CodeDeploy 실행
- name: Deploy to AWS EC2 from S3
run: |
aws deploy create-deployment \
--application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} \
--s3-location bucket=$AWS_S3_BUCKET,key=$GITHUB_SHA.zip,bundleType=zip
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public ResponseEntity<BaseResponse> sendGetRequest() {

try {
String jsonString = alertService.getAlertInfo(getApiDto);
log.debug("jsonString: "+jsonString);
// log.debug("jsonString: "+jsonString);
alertService.isJson(jsonString);

List<ApiInfoDto> infoList = alertService.ParsingJson(jsonString);
return new BaseResponse<>(infoList).convert();
ApiInfoDto apiInfoDto = alertService.ParsingJson(jsonString);
return new BaseResponse<>(apiInfoDto).convert();
} catch (BaseException e){
// 실패시 custom한 status로 code 헤더 설정, body로 메세지 반환
return new BaseResponse<>(e.getStatus()).convert();
Expand Down
53 changes: 39 additions & 14 deletions src/main/java/com/haemil/backend/alert/service/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.UnknownContentTypeException;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,28 +42,50 @@ public String getAlertInfo(GetApiDto reqGetApiDto) throws BaseException {
String pageNo = reqGetApiDto.getPageNo();
String numOfRows = reqGetApiDto.getNumOfRows();

log.debug("serviceKey: " + serviceKey);

StringBuilder urlBuilder = new StringBuilder(apiUrl);
urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + "=" + serviceKey);
urlBuilder.append("&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(type, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode(pageNo, "UTF-8"));
urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode(numOfRows, "UTF-8"));

ResponseEntity<String> response = restTemplate.getForEntity(urlBuilder.toString(), String.class);
// log.debug("urlBuilder: {}", urlBuilder);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "*/*;q=0.9"); // HTTP_ERROR 방지
HttpEntity<String> httpRequest = new HttpEntity<>(null, headers);
// log.debug("httpRequest = {}", httpRequest);

RestTemplate restTemplate = new RestTemplate();
// log.debug("restTemplate = {}", restTemplate);

responseBody = response.getBody();
HttpStatus httpStatus = null;
ResponseEntity<String> httpResponse = null;

URI uri = new URI(urlBuilder.toString()); // service key is not registered 오류 방지
// log.debug("uri = {}", uri);
httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpRequest, new ParameterizedTypeReference<String>(){});
// log.debug("httpResponse = {}", httpResponse);

if (httpResponse != null && httpResponse.getBody() != null) {
responseBody = httpResponse.getBody();
}
// log.debug("responseBody = {}",responseBody);

} catch (UnsupportedEncodingException e) { // 에러가 발생했을 때 예외 status 명시
log.debug("UnsupportedEncodingException 발생 ");
throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING);
} catch (URISyntaxException e) {
log.debug("URISyntaxException 발생 ");
throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING);
}

return responseBody;
}

public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
public ApiInfoDto ParsingJson(String responseBody) throws BaseException {
ApiInfoDto apiInfoDto;

List<ApiInfoDto> apiInfoDtoList;
try {
List<AlertApi> alertApiList = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -72,8 +98,8 @@ public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
String msg = nextNode.get("msg").asText();
String location = nextNode.get("location_name").asText(); // 예시로 location_name을 사용하여 location 값을 가져옴

log.debug("msg: " + msg);
log.debug("location: " + location);
// log.debug("msg: " + msg);
// log.debug("location: " + location);

AlertApi alertApi = AlertApi.builder()
.msg(msg)
Expand All @@ -83,19 +109,18 @@ public List<ApiInfoDto> ParsingJson(String responseBody) throws BaseException {
alertApiList.add(alertApi);

// 변환된 데이터를 ApiInfoDto 형태로 리스트로 반환
apiInfoDtoList = new ArrayList<>();
apiInfoDto = new ApiInfoDto();

for (AlertApi a : alertApiList) {
ApiInfoDto apiInfoDto = new ApiInfoDto();
apiInfoDto.setMsg(a.getMsg());
apiInfoDto.setLocation(a.getLocation());
apiInfoDtoList.add(apiInfoDto);
}
log.debug("apiInfoDtoList: " + apiInfoDtoList);
// log.debug("apiInfoDto: " + apiInfoDto);

} catch (JsonProcessingException e) { // 에러가 발생했을 때 예외 status 명시
throw new BaseException(ResponseStatus.CANNOT_CONVERT_JSON);
}
return apiInfoDtoList;
return apiInfoDto;
}

public boolean isJson(String xmlString) throws BaseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum ResponseStatus {
// --- 55x Custom Error --
CANNOT_CONVERT_JSON(false, 550, "JSON 문자열로 변경할 수 없습니다."),
UNSUPPORTED_ENCODING(false, 551, "지원되지 않는 인코딩 형식입니다."),
URI_SYNT(false, 551, "URISyntaxException이 발생했습니다."),
INVALID_XML_FORMAT(false, 552, "SERVICE ERROR가 발생했습니다."),
UNKNOWN_ADDR(false, 553, "알 수 없는 주소를 입력받았습니다.");

Expand Down

0 comments on commit 7a5d426

Please sign in to comment.