From 0e26e1a0dbf6d02098945511e26bef8bef25b119 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sat, 30 Mar 2024 23:23:33 +0100 Subject: [PATCH 1/4] adding compose for launching properly everything --- backend/Dockerfile | 14 +++++-- backend/settings.gradle.kts | 13 +++++++ .../java/ovh/herisson/Clyde/JdbcConfig.java | 37 ------------------- .../src/main/resources/application.properties | 11 +++++- compose.yaml | 22 +++++++++++ frontend/Dockerfile | 11 ++++++ frontend/src/rest/restConsumer.js | 2 +- 7 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 backend/settings.gradle.kts delete mode 100644 backend/src/main/java/ovh/herisson/Clyde/JdbcConfig.java create mode 100644 compose.yaml create mode 100644 frontend/Dockerfile diff --git a/backend/Dockerfile b/backend/Dockerfile index 5475dc9..89e069b 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,6 +1,14 @@ +## Building phase +FROM gradle:jdk21-alpine +# WORKDIR /app/back + +COPY . . +RUN gradle build -x test + +## Running Phase FROM eclipse-temurin:21-jdk-alpine -VOLUME /tmp VOLUME /cdn -ENV SPRING_PROFILES_ACTIVE=prod -COPY build/libs/backend-0.0.1-SNAPSHOT.jar /app.jar +# ENV SPRING_PROFILES_ACTIVE=prod +COPY build/libs/Clyde-0.0.1-SNAPSHOT.jar app.jar +EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"] diff --git a/backend/settings.gradle.kts b/backend/settings.gradle.kts new file mode 100644 index 0000000..7d39fc5 --- /dev/null +++ b/backend/settings.gradle.kts @@ -0,0 +1,13 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.6/userguide/multi_project_builds.html in the Gradle documentation. + */ + +plugins { + // Apply the foojay-resolver plugin to allow automatic download of JDKs + id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" +} + +rootProject.name = "Clyde" diff --git a/backend/src/main/java/ovh/herisson/Clyde/JdbcConfig.java b/backend/src/main/java/ovh/herisson/Clyde/JdbcConfig.java deleted file mode 100644 index 801e549..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/JdbcConfig.java +++ /dev/null @@ -1,37 +0,0 @@ -package ovh.herisson.Clyde; - -import javax.sql.DataSource; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.scheduling.annotation.EnableScheduling; - -@Configuration -@EnableScheduling -public class JdbcConfig { - - @Bean - @Profile("!prod") - public DataSource psqlSource(){ - DriverManagerDataSource source = new DriverManagerDataSource(); - source.setDriverClassName("org.postgresql.Driver"); - source.setUrl("jdbc:postgresql://localhost:5442/clyde"); - source.setUsername("devel"); - source.setPassword("devel"); - - return source; - } - - @Bean - @Profile("prod") - public DataSource psqlSourceProd(){ - DriverManagerDataSource source = new DriverManagerDataSource(); - source.setDriverClassName("org.postgresql.Driver"); - source.setUrl("jdbc:postgresql:clyde?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432"); - source.setUsername("clyde"); - - return source; - } -} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 5d00d8e..190c76b 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,3 +1,12 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect -spring.sql.init.mode=always \ No newline at end of file +spring.sql.init.mode=always + +# spring.datasource.url=jdbc:postgresql://localhost:5442/clyde +spring.datasource.url=jdbc:postgresql://db:5432/clyde +spring.datasource.username=devel +spring.datasource.password=devel + +# spring.config.activate.on-profile=prod +# spring.datasource.url=jdbc:postgresql:clyde?socketFactory=org.newsclub.net.unix.AFUNIXSocketFactory$FactoryArg&socketFactoryArg=/var/run/postgresql/.s.PGSQL.5432 +# spring.datasource.username=clyde diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..a127b25 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,22 @@ +services: + db: + image: 'postgres:16' + environment: + - 'POSTGRES_DB=clyde' + - 'POSTGRES_USER=devel' + - 'POSTGRES_PASSWORD=devel' + # Uncomment this to allow connections to the db from outside the container + # ports: + # - '5442:5432' + back: + build: backend/. + ports: + - "8080:8080" + ulimits: + nofile: + soft: 65536 + hard: 65536 + front: + build: frontend/. + ports: + - "8000:8080" diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..2df53ae --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,11 @@ +# https://v2.vuejs.org/v2/cookbook/dockerize-vuejs-app +FROM node:lts-alpine +RUN npm install -g http-server +WORKDIR /app/front +COPY package*.json ./ +RUN npm install +COPY . . +ENV VITE_CLYDE_MODE=container +RUN npm run build +EXPOSE 8080 +CMD [ "http-server", "dist" ] diff --git a/frontend/src/rest/restConsumer.js b/frontend/src/rest/restConsumer.js index 2ac4cc3..1af979e 100644 --- a/frontend/src/rest/restConsumer.js +++ b/frontend/src/rest/restConsumer.js @@ -1,7 +1,7 @@ import { getCookie } from '../utils.js' import { toast } from 'vue3-toastify' -const restURL = import.meta.env.PROD ? "https://clyde.herisson.ovh/api" : "http://localhost:8080" +const restURL = import.meta.env.VITE_CLYDE_MODE === 'container' ? "http://localhost:8080": import.meta.env.DEV ? "http://localhost:8080" : "https://clyde.herisson.ovh/api" export async function restGet(endPoint) { return await _rest(endPoint, {method: "GET"}); From 2e2837fec4a5b3905f8967bdb2ba1a5366e38b6d Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sat, 30 Mar 2024 23:33:16 +0100 Subject: [PATCH 2/4] adding info about compose --- README.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5a99ac9..2d33f08 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,33 @@ Projet du groupe 01: - William Karpinski: Extension gestion des horaires - Léo Moulin: Extension inscription des étudiants +## Running + +Le projet peut être lancé grace à docker compose. + +```sh +$ docker compose up +``` + +Dans le cas ou vous modifiers des fichiers, pour éviter que les images de docker soient recrées avec les changement + +```sh +$ docker compose up --force-recreate --build +``` + ## Dévelopement -``` -$ ./gradlew backend:run frontend:run --parallel -``` +Dans le cas ou vous êtes dans une phase de développement, il est plus simple d'utiliser gradle pour lancer le backend et frontend dans un mode de développement. +**Attention**: Ce mode n'est pas fait pour être utilisé en production! +```sh +$ ./gradlew run --parallel +``` permet de lancer le frontend sur [http://localhost:5173](http://localhost:5173) ansi que le frontend sur [http://localhost:8080](http://localhost:8080) + +Ceci requière également docker pour lancer une instance de postgresql pris en charge par spring. + +Il est possible de se passer entièrement de docker en supprimant la dépendance dans le fichier `backend/build.gradle.kts`: ~~`developmentOnly("org.springframework.boot:spring-boot-docker-compose")`~~ +Il est alors nécéssaire d'avoir une instance de postgresql tournant sur `localhost:5432` avec une table `clyde`, utilisateur: `devel` et password: `devel` +(cette configuration peut également être changée dans le fichier resources/application.properties de spring) + From cb36aa8a3070ac1d53a7e73d28ea9c1403bab4fe Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 1 Apr 2024 22:49:24 +0200 Subject: [PATCH 3/4] Fixing file locations --- backend/Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 89e069b..1ed9969 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,14 +1,15 @@ ## Building phase -FROM gradle:jdk21-alpine -# WORKDIR /app/back +FROM gradle:jdk21-alpine AS BUILD +WORKDIR /backend COPY . . RUN gradle build -x test ## Running Phase FROM eclipse-temurin:21-jdk-alpine +WORKDIR /backend VOLUME /cdn # ENV SPRING_PROFILES_ACTIVE=prod -COPY build/libs/Clyde-0.0.1-SNAPSHOT.jar app.jar +COPY --from=BUILD /backend/build/libs/Clyde-0.0.1-SNAPSHOT.jar /backend/app.jar EXPOSE 8080 -ENTRYPOINT ["java", "-jar", "/app.jar"] +ENTRYPOINT ["java", "-jar", "/backend/app.jar"] From 3761fa6f49c2df313c209c88794c03b9dfb5dcc5 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 2 Apr 2024 00:21:15 +0200 Subject: [PATCH 4/4] Adding cdn support with compose Now when you upload a file, the path is stored to the db and the file is accesible on the client with : `localhost:8000/cdn/3ed026aa-366f-4f33-bc51-fb59d37e35ee.png` for instance. --- compose.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compose.yaml b/compose.yaml index a127b25..ec5f6bc 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,11 +12,18 @@ services: build: backend/. ports: - "8080:8080" + volumes: + - cdn:/backend/cdn ulimits: nofile: soft: 65536 hard: 65536 front: build: frontend/. + volumes: + - cdn:/app/front/dist/cdn ports: - "8000:8080" + +volumes: + cdn: