diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6d483c6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,50 @@ +# github repository Actions 페이지에 나타낼 이름 +name: devocean A CI with Gradle + +# event trigger +# main, develop 브랜치에 pr 생성시 실행되는 트리거 +on: + # pr 생성시 CI까지만 진행 + pull_request: + branches: [ "main", "develop" ] + + +# permissions: write-all + + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + ## jdk setting + - uses: actions/checkout@v3 + - name: 🐧Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + ## gradle caching + - name: 🐧Gradle Caching + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: 🐧application.yml 파일을 생성 합니다. + run: | + cd ./src/main/resources + touch ./application.yml + echo "${{ secrets.PROPERTIES }}" > ./application.yml + shell: bash + + - name: 🐧gradle build를 위한 권한을 부여합니다. + run: chmod +x gradlew + + - name: 🐧gradle build 중입니다. + run: ./gradlew build + shell: bash # ci는 여기까지 diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..47f8dab --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,68 @@ +# github repository Actions 페이지에 나타낼 이름.. +name: devocean A CI/CD with Gradle + +# event trigger +# main, develop 브랜치에 push 시실행되는 트리거 +on: + # 푸시했을 때 ci/cd + push: + branches: [ "main", "develop" ] + +# permissions: write-all + + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + ## jdk setting + - uses: actions/checkout@v3 + - name: 🐧Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + ## gradle caching + - name: 🐧Gradle Caching + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: 🐧gradle build를 위한 권한을 부여합니다. + run: chmod +x gradlew + + - name: 🐧gradle build 중입니다. + run: ./gradlew build + shell: bash # ci는 여기까지 + + - name: 🐧docker image build 후 docker hub에 push합니다. + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} . + docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }} + + ## deploy to production + - name: docker hub에서 pull 후 deploy합니다. + uses: appleboy/ssh-action@master + with: + username: ${{ secrets.EC2_USERNAME }} + host: ${{ secrets.EC2_HOST }} + key: ${{ secrets.EC2_PRIVATE_KEY }} + envs: GITHUB_SHA + script: | + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest + sudo docker stop $(sudo docker ps -q) 2>/dev/null || true + sudo docker run --rm -d --network='host' -p 80:8080 --name ${{ secrets.DOCKER_IMAGE }} \ + -e RDB_HOST=${{ secrets.RDB_HOST }} \ + -e RDB_PORT=${{ secrets.RDB_PORT }} \ + -e RDB_SCHEMA=${{ secrets.RDB_SCHEMA }} \ + -e RDB_USER=${{ secrets.RDB_USER }} \ + -e RDB_PASSWORD=${{ secrets.RDB_PASSWORD }} \ + ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE }}:latest + sudo docker system prune -f diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cde9ab8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1a0e199..bdd1867 100644 --- a/build.gradle +++ b/build.gradle @@ -25,15 +25,12 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' - implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.springframework.security:spring-security-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/src/main/java/com/paws/devocean/config/WebConfig.java b/src/main/java/com/paws/devocean/config/WebConfig.java new file mode 100644 index 0000000..b96c583 --- /dev/null +++ b/src/main/java/com/paws/devocean/config/WebConfig.java @@ -0,0 +1,16 @@ +package com.paws.devocean.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") // 허용할 출처 : 특정 도메인만 받을 수 있음 + .allowedMethods("GET", "POST") // 허용할 HTTP method + .allowCredentials(true); // 쿠키 인증 요청 허용 + } +} diff --git a/src/main/java/com/paws/devocean/controller/ProjectController.java b/src/main/java/com/paws/devocean/controller/ProjectController.java new file mode 100644 index 0000000..59deb70 --- /dev/null +++ b/src/main/java/com/paws/devocean/controller/ProjectController.java @@ -0,0 +1,25 @@ +package com.paws.devocean.controller; + +import com.paws.devocean.dto.ProjectDetailResponse; +import com.paws.devocean.dto.ProjectListResponse; +import com.paws.devocean.service.ProjectService; +import com.paws.devocean.vo.Type; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/projects") +public class ProjectController { + private final ProjectService projectService; + + @GetMapping + public ProjectListResponse getProjects(@RequestParam Type type) { + return projectService.getProject(type); + } + + @GetMapping("/{id}") + public ProjectDetailResponse getProject(@PathVariable Long id) { + return projectService.getProjectById(id); + } +} diff --git a/src/main/java/com/paws/devocean/dto/ProjectDetailResponse.java b/src/main/java/com/paws/devocean/dto/ProjectDetailResponse.java new file mode 100644 index 0000000..9564dd3 --- /dev/null +++ b/src/main/java/com/paws/devocean/dto/ProjectDetailResponse.java @@ -0,0 +1,10 @@ +package com.paws.devocean.dto; + +import lombok.Builder; + +@Builder +public record ProjectDetailResponse( + String title, + String content +) { +} diff --git a/src/main/java/com/paws/devocean/dto/ProjectListResponse.java b/src/main/java/com/paws/devocean/dto/ProjectListResponse.java new file mode 100644 index 0000000..c8ec716 --- /dev/null +++ b/src/main/java/com/paws/devocean/dto/ProjectListResponse.java @@ -0,0 +1,11 @@ +package com.paws.devocean.dto; + +import lombok.Builder; + +import java.util.List; + +@Builder +public record ProjectListResponse ( + List projects +){} + diff --git a/src/main/java/com/paws/devocean/dto/ProjectResponse.java b/src/main/java/com/paws/devocean/dto/ProjectResponse.java new file mode 100644 index 0000000..6bb5b67 --- /dev/null +++ b/src/main/java/com/paws/devocean/dto/ProjectResponse.java @@ -0,0 +1,10 @@ +package com.paws.devocean.dto; + +import lombok.Builder; + +@Builder +public record ProjectResponse( + String title, + String thumbnail, + Long project_id +){} diff --git a/src/main/java/com/paws/devocean/entity/Project.java b/src/main/java/com/paws/devocean/entity/Project.java new file mode 100644 index 0000000..d9c7749 --- /dev/null +++ b/src/main/java/com/paws/devocean/entity/Project.java @@ -0,0 +1,30 @@ +package com.paws.devocean.entity; + +import com.paws.devocean.vo.Type; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor +public class Project { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String thumbnail; + + @Column(nullable = false) + private String title; + + @Column(columnDefinition = "TEXT", nullable = false) + private String content; + + @Column(nullable = false) + @Enumerated(EnumType.STRING) + private Type type; +} diff --git a/src/main/java/com/paws/devocean/repository/ProjectRepository.java b/src/main/java/com/paws/devocean/repository/ProjectRepository.java new file mode 100644 index 0000000..d7306ed --- /dev/null +++ b/src/main/java/com/paws/devocean/repository/ProjectRepository.java @@ -0,0 +1,11 @@ +package com.paws.devocean.repository; + +import com.paws.devocean.entity.Project; +import com.paws.devocean.vo.Type; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface ProjectRepository extends JpaRepository { + List findAllByType(Type type); +} diff --git a/src/main/java/com/paws/devocean/service/ProjectService.java b/src/main/java/com/paws/devocean/service/ProjectService.java new file mode 100644 index 0000000..117864e --- /dev/null +++ b/src/main/java/com/paws/devocean/service/ProjectService.java @@ -0,0 +1,38 @@ +package com.paws.devocean.service; + +import com.paws.devocean.dto.ProjectDetailResponse; +import com.paws.devocean.dto.ProjectListResponse; +import com.paws.devocean.dto.ProjectResponse; +import com.paws.devocean.entity.Project; +import com.paws.devocean.repository.ProjectRepository; +import com.paws.devocean.vo.Type; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@Transactional +@RequiredArgsConstructor +public class ProjectService { + private final ProjectRepository projectRepository; + + public ProjectListResponse getProject(Type type) { + return new ProjectListResponse( + projectRepository.findAllByType(type).stream() + .map(project -> ProjectResponse.builder() + .thumbnail(project.getThumbnail()) + .title(project.getTitle()) + .project_id(project.getId()) + .build()) + .toList() + ); + } + + public ProjectDetailResponse getProjectById(Long projectId) { + Project project = projectRepository.findById(projectId).orElseThrow(); + return ProjectDetailResponse.builder() + .title(project.getTitle()) + .content(project.getContent()) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/paws/devocean/vo/Type.java b/src/main/java/com/paws/devocean/vo/Type.java new file mode 100644 index 0000000..5940b9e --- /dev/null +++ b/src/main/java/com/paws/devocean/vo/Type.java @@ -0,0 +1,6 @@ +package com.paws.devocean.vo; + +public enum Type { + DEVOCEAN_YOUNG, + OPEN_LAB +} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6c91490..2addd3d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,10 @@ spring: - profiles: - active: - - prod \ No newline at end of file + datasource: + url: jdbc:mysql://${RDB_HOST}:${RDB_PORT}/${RDB_SCHEMA}?serverTimezone=Asia/Seoul&characterEncoding=utf8 + username: ${RDB_USER} + password: ${RDB_PASSWORD} + driver-class-name: com.mysql.cj.jdbc.Driver + + jpa: + hibernate: + ddl-auto: update \ No newline at end of file