Skip to content

Commit

Permalink
Merge pull request #5 from wndlthsk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wndlthsk authored Oct 4, 2024
2 parents 1369557 + 3039148 commit c085270
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 51 deletions.
51 changes: 34 additions & 17 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
name: Java CI with Gradle

# Master, develop 브랜치에 push 혹은 pull request가 발생할 경우 동작한다.
# Master, develop, weekly/* 브랜치에 push가 발생할 경우 동작한다.
on:
push:
branches: [ "Master", "develop" ]
pull_request:
branches: [ "Master", "develop" ]
branches: [ "Master", "develop", "weekly/*" ]

permissions:
contents: read

jobs:
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
build-docker-image:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
# 1. Java 17 세팅
# Java 17 세팅
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# Gradle Wrapper 파일 실행 권한 주기
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew

# 2. Spring Boot 애플리케이션 빌드
# Spring Boot 애플리케이션 빌드
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: clean bootJar

# 3. Docker 이미지 빌드
# 테스트 실행
# - name: Run tests
# run: ./gradlew test

# Docker 이미지 빌드
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/inplace .

# 4. DockerHub 로그인
# DockerHub 로그인
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# 5. Docker Hub 이미지 푸시
# Docker Hub 이미지 푸시
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/inplace

Expand All @@ -55,23 +58,37 @@ jobs:
needs: build-docker-image
runs-on: self-hosted

# weekly/* 브랜치에서는 실행되지 않도록 조건 추가
if: startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/heads/develop')

steps:
# 1. 최신 이미지를 pull
# 최신 이미지를 pull
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/inplace

# 2. 기존의 컨테이너를 중지
# 기존의 컨테이너를 중지
- name: docker stop container
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true

# 3. 브랜치에 따라 다른 환경 변수를 설정하여 컨테이너 실행
# .env 파일 생성
- name: Create .env file
run: |
echo "KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }}" >> .env
echo "KAKAO_CLIENT_SECRET=${{ secrets.KAKAO_CLIENT_SECRET }}" >> .env
echo "KAKAO_REDIRECT_URI=${{ secrets.KAKAO_REDIRECT_URI }}" >> .env
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env
echo "JWT_ACCESS_TOKEN_EXPIRED_TIME=${{ secrets.JWT_ACCESS_TOKEN_EXPIRED_TIME }}" >> .env
echo "JWT_REFRESH_TOKEN_EXPIRED_TIME=${{ secrets.JWT_REFRESH_TOKEN_EXPIRED_TIME }}" >> .env
# 브랜치에 따라 다른 환경 변수를 설정하여 컨테이너 실행
- name: docker run new container
run: |
if [ "${{ github.ref }}" == "refs/heads/master" ]; then
sudo docker run --name inplace --rm -d -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/inplace
sudo docker run --name inplace --rm -d -p 8080:8080 --env-file .env ${{ secrets.DOCKERHUB_USERNAME }}/inplace
elif [ "${{ github.ref }}" == "refs/heads/develop" ]; then
sudo docker run --name inplace-dev --rm -d -p 8081:8080 ${{ secrets.DOCKERHUB_USERNAME }}/inplace
# 4. 미사용 이미지를 정리
sudo docker run --name inplace-dev --rm -d -p 8081:8080 --env-file .env ${{ secrets.DOCKERHUB_USERNAME }}/inplace
fi
# 미사용 이미지를 정리
- name: delete old docker image
run: sudo docker system prune -f
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dockerfile

# jdk17 Image Start
#FROM --platform=linux/amd64 ubuntu:latest
FROM openjdk:17

# 인자 설정 - Jar_File
ARG JAR_FILE=build/libs/*.jar

# jar 파일 복제
COPY ${JAR_FILE} app.jar

# 인자 설정 부분과 jar 파일 복제 부분 합쳐서 진행해도 무방
#COPY build/libs/*.jar app.jar

# 실행 명령어
ENTRYPOINT ["java", "-jar", "app.jar"]
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.3'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/team7/inplace/cicd/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package team7.inplace.cicd;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
@GetMapping("/cicd")
public ResponseEntity test() {
return ResponseEntity.ok("ci/cd 테스트");
}
}
37 changes: 34 additions & 3 deletions src/main/java/team7/inplace/video/application/VideoService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package team7.inplace.video.application;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.influencer.persistence.InfluencerRepository;
import team7.inplace.place.application.dto.PlaceForVideo;
import team7.inplace.place.domain.Place;
import team7.inplace.place.application.dto.PlaceForVideo;
import team7.inplace.place.persistence.PlaceRepository;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.domain.Video;
import team7.inplace.video.persistence.VideoRepository;
import team7.inplace.video.presentation.dto.VideoSearchParams;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class VideoService {
private final VideoRepository videoRepository;
private final InfluencerRepository influencerRepository;
private final PlaceRepository placeRepository;

public List<VideoInfo> findByInfluencer(List<String> influencers) {
// 인플루언서 정보 처리
Expand All @@ -31,6 +37,31 @@ public List<VideoInfo> findByInfluencer(List<String> influencers) {
return videoToInfo(savedVideos);
}

public List<VideoInfo> findAllDesc() {
// id를 기준으로 내림차순 정렬하여 비디오 정보 불러오기
List<Video> savedVideos = videoRepository.findAllByOrderByIdDesc();

// DTO 형식에 맞게 대입
return videoToInfo(savedVideos);
}

public List<VideoInfo> findBySurround(VideoSearchParams videoSearchParams, Pageable pageable) {
Page<Place> placesByDistance = placeRepository.getPlacesByDistance(
videoSearchParams.longitude(),
videoSearchParams.latitude(),
pageable
);

List<Place> places = placesByDistance.getContent();
List<Video> videos = new ArrayList<>();
for (Place place : places) {
if(videos.size() == places.size())
break;
videos.add(videoRepository.findTopByPlaceOrderByIdDesc(place));
}
return videoToInfo(videos);
}

private List<VideoInfo> videoToInfo(List<Video> savedVideos) {
List<VideoInfo> videoInfos = new ArrayList<>();
for (Video savedVideo : savedVideos) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package team7.inplace.video.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import team7.inplace.place.domain.Place;
import team7.inplace.video.domain.Video;

import java.util.List;

public interface VideoRepository extends JpaRepository<Video, Long> {
List<Video> findVideosByInfluencerIdIn(List<Long> influencerIds);

List<Video> findAllByOrderByIdDesc();

Video findTopByPlaceOrderByIdDesc(Place place);
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,81 @@
package team7.inplace.video.presentation;

import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team7.inplace.video.application.VideoService;
import org.springframework.web.bind.annotation.*;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.presentation.dto.VideoResponse;
import team7.inplace.video.application.VideoService;
import team7.inplace.video.presentation.dto.VideoSearchParams;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/videos")
public class VideoController {
private final VideoService videoService;

// 내 인플루언서가 방문한 그 곳 ( 토큰 0 )
@GetMapping("/video")
public ResponseEntity<List<VideoResponse>> readByInfluencer(
@RequestParam(name = "influencer", required = false) List<String> influencers
// 토큰 필요 메서드
@GetMapping()
@Operation(
summary = "내 인플루언서가 방문한 or 내 주변 그곳 ",
description = "토큰의 유무에 따라 다른 동작을 수행합니다."
)
public ResponseEntity<List<VideoResponse>> readVideos(
HttpServletRequest request,
@RequestParam(name = "influencer", required = false) List<String> influencers,
@ModelAttribute VideoSearchParams searchParams,
@RequestParam(defaultValue = "0", required = false) int page,
@RequestParam(defaultValue = "10", required = false) int size
) {
// Authorization 헤더 추출
String authHeader = request.getHeader("Authorization");
// 토큰 존재 여부 검사
if(authHeader != null && authHeader.startsWith("Bearer ")) {
// 토큰 유효성 검사

// 토큰이 있는 경우
return readByInfluencer(influencers);
}

// 토큰이 없는 경우
return readBySurround(searchParams, page, size);
}

private ResponseEntity<List<VideoResponse>> readByInfluencer(List<String> influencers){
List<VideoInfo> videoInfos = videoService.findByInfluencer(influencers);
List<VideoResponse> videoResponses = videoInfos.stream().map(VideoResponse::from).toList();
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}

private ResponseEntity<List<VideoResponse>> readBySurround(VideoSearchParams searchParams, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
List<VideoInfo> videoInfos = videoService.findBySurround(searchParams, pageable);
List<VideoResponse> videoResponses = videoInfos.stream().map(VideoResponse::from).toList();
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}

@GetMapping("/new")
@Operation(summary = "새로 등록된 그 곳", description = "id를 기준으로 내림차순 정렬한 Video 정보를 조회합니다.")
public ResponseEntity<List<VideoResponse>> readByNew() {
List<VideoInfo> videoInfos = videoService.findAllDesc();
List<VideoResponse> videoResponses = videoInfos.stream().map(VideoResponse::from).toList();
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}

// 조회수 반환 기능 개발 시 개발
@GetMapping("/cool")
@Operation(summary = "쿨한 그 곳", description = "조회수를 기준으로 내림차순 정렬한 Video 정보를 조회합니다.")
public ResponseEntity<List<VideoResponse>> readByCool() {
List<VideoResponse> videoResponses = new ArrayList<>();
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team7.inplace.video.presentation.dto;

public record VideoSearchParams(
String longitude,
String latitude
) {
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
config:
import: classpath:application-security.yaml, classpath:application-youtube.yaml
import: classpath:application-security.yaml, classpath:application-youtube.yaml, optional:file:.env[.properties]
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package team7.inplace.VideoTest.domain;
package team7.inplace.video.domain;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.place.domain.Place;
import team7.inplace.video.domain.Video;

public class VideoTest {
@Test
Expand Down
Loading

0 comments on commit c085270

Please sign in to comment.