Skip to content

Commit

Permalink
Migrate from mongodb to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
aegan123 committed Nov 10, 2024
1 parent 5c62cbc commit 6716bff
Show file tree
Hide file tree
Showing 54 changed files with 415 additions and 420 deletions.
10 changes: 5 additions & 5 deletions .env-dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ SMTP_PASSWORD=password
[email protected]
DEFAULT_DAYS_TO_ARCHIVE_PAST_EVENTS=100
DEFAULT_IMAGE_PATH=/tmp
MONGO_HOST=localhost
MONGO_PORT=27017
MONGO_DATABASE_NAME=databasename
MONGO_USERNAME=username
MONGO_PASSWORD=password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=eventsignup
DB_USERNAME=postgres
DB_PASSWORD=1234
KEYCLOAK_URL=http://localhost:9090
ALLOWED_CORS_DOMAIN=https://asteriski.fi
20 changes: 11 additions & 9 deletions .github/workflows/build-artifacts-on-tag-and-on-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ jobs:
runs-on: ubuntu-latest
container: eclipse-temurin:21
env:
MONGO_HOST: mongodb
DB_HOST: postgres
DB_PORT: ${{ job.services.postgres.ports[5432] }}
KEYCLOAK_URL: localhost
SPRING_PROFILES_ACTIVE: dev
GIT_TAG: ${GITHUB_REF_NAME}
services:
mongodb:
image: mongo:5.0.5
postgres:
image: postgres:17.0
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 27017:27017
options: >-
--health-cmd mongo
--health-interval 10s
--health-timeout 5s
--health-retries 5
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3

Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/ci-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
Expand All @@ -17,10 +16,6 @@ jobs:
java-version: '21'
distribution: 'oracle'

- name: Start MongoDB
uses: supercharge/[email protected]
with:
mongodb-version: '5.0.5'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@ec92e829475ac0c2315ea8f9eced72db85bb337a # v3.0.0
- name: Spotless check
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ Backend microservice for event signup system. Includes Keycloak integration for
- Springboot 3.2.x
- Apache IO Commons
- Gradle
- MongoDB 5.0.5
- Postgres 17
- JUnit 5 & Mockito
- Project lombok
- Keycloak for authentication
- Quartz scheduler
- OpenApi
- Spotless for code formatting
- Flyway

## Development

- Expects Mongodb at `localhost:27017`
- Expects Postgres at `localhost:5432`
- Server will be at `localhost:8080`
- OpenAPI definitions are available at http://localhost:8080/v1/api-docs.<yaml | json>
- OpenAPI swagger ui is available at http://localhost:8080/swagger-ui/index.html
Expand All @@ -37,7 +38,7 @@ To run the service:
2. Linux: Install from your distro's repo
3. [macOs](https://adoptium.net/temurin/releases/?os=mac&version=21)
2. Install podman or docker. See [container setup](#running-locally)
1. Run mongodb in a container: `podman run -dt --pod new:eventsignup -p 27017:27017 -p 8080:8080 mongo:5.0.5` or `docker run -d -p 27017:27017 --name mongodb mongo:5.0.5`
1. Run Postgres in a container: `podman run -dt --pod new:postgres17 -e POSTGRES_PASSWORD=1234 -p 5432:5432 postgres:17.0` or `docker run -d -p 5432:5432 --name postgres17 postgres:17.0`
3. Import existing sources as a new project in your favorite IDE (e.g. IntelliJ IDEA)
4. Import Gradle project

Expand Down Expand Up @@ -81,15 +82,17 @@ If you just need the service up and running e.g. for front-end development or pr

### Running locally

Note: Never use these commands in production!

1. Install Podman or docker
1. Podman
1. [Windows](https://podman-desktop.io/docs/installation/windows-install)
2. Linux: install from your distro's repo
3. [macOs](https://podman-desktop.io/docs/installation/macos-install)
2. Docker Desktop: [Windows](https://docs.docker.com/desktop/install/windows-install/), [Linux](https://docs.docker.com/desktop/install/linux-install/), [MacOs](https://docs.docker.com/desktop/install/mac-install/)
2. Run Mongodb
1. `docker run -d -p 27017:27017 --name mongodb mongo:5.0.5` or
2. `podman run -dt --pod new:eventsignup -p 27017:27017 -p 8080:8080 mongo:5.0.5`
1. `docker run -d -p 5432:5432 --name postgresql17 postgres:17.0` or
2. `podman run -dt --pod new:eventsignup -p 8080:8080 -e POSTGRES_PASSWORD=1234 -p 5432:5432 postgres:17.0`
3. Authenticate to [GHCR](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-with-a-personal-access-token-classic)
4. Download .env-dev file from this repo
5. (Only for podman) `podman run -d --pod=eventsignup --env-file=path/to/.env-dev ghcr.io/asteriskiry/eventsignup_backend:latest`
Expand All @@ -105,11 +108,11 @@ If Podman gives an error try adding `--network=slirp4netns:enable_ipv6=false` to

### Running in production

Prerequisite: Mongodb should be already running somewhere.
Prerequisite: Postgres should be already running somewhere and the database exists.

1. Create a .env file somewhere with values for all the variables listed in [environment variables](#environment-variables) table.
2. Run container
1. `podman run -d --pod=new:eventsignup -p 8080:8080 --env-file=path/to/.env ghcr.io/asteriskiry/eventsignup_backend:<tag>`
1. `podman run -d --pod=new:eventsignup -p 8080:8080 --env-file=path/to/.env ghcr.io/asteriskiry/eventsignup_backend:<tag>` or
2. `docker run -d -p 8080:8080 --env-file=path/to/.env ghcr.io/asteriskiry/eventsignup_backend:<tag>`

Note
Expand All @@ -124,19 +127,19 @@ For production these variables are needed.
| SERVER_PORT | Which port the server is listening in | 8080 |
| SERVER_HOST | Hostname | localhost |
| SERVER_ENABLE_SSL | Whether to enable SSL support | true |
| MONGO_HOST | Hostname of mongodb server | localhost |
| MONGO_PORT | Which port mongodb is running | 27017 |
| MONGO_DATABASE_NAME | Name of the used database | databaseName |
| MONGO_USERNAME | User to connect to db with | user |
| MONGO_PASSWORD | Database user's password | password |
| DB_HOST | Hostname of postgresql server | localhost |
| DB_PORT | Which port postgresql is running | 5432 |
| DB_NAME | Name of the used database | databaseName |
| DB_USERNAME | User to connect to db with | user |
| DB_PASSWORD | Database user's password | password |
| SMTP_HOST | Hostname of SMTP server | localhost |
| SMTP_PORT | Port what SMTP server listens to | 25 |
| SMTP_USERNAME | SMTP servers user | user |
| SMTP_PASSWORD | SMTP user's password | password |
| DEFAULT_SENDER_EMAIL | Email address for outgoing email | [email protected] |
| DEFAULT_DAYS_TO_ARCHIVE_PAST_EVENTS | After how many days old events are automatically archived | 180 |
| DEFAULT_IMAGE_PATH | Directory where uploaded pictures are stored | /tmp |
| BASE_URL | Used to generate urls in emails | http://exapmle.org |
| BASE_URL | Used to generate urls in emails | http://example.org |
| KEYCLOAK_ISSUER_URI | Url where Keycloak can be found | http://localhost:9090/realms/realmName |
| KEYCLOAK_CLIENT_NAME | Client name in Keycloak | example |
| KEYCLOAK_CLIENT_ID | Client id in Keycloak | example |
Expand Down
13 changes: 6 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ configurations {

repositories {
mavenCentral()
maven {
url "https://jcenter.bintray.com/"
}
}

node {
Expand All @@ -41,7 +38,7 @@ openApi {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-quartz'
implementation 'org.springframework.boot:spring-boot-starter-security'
Expand All @@ -52,17 +49,19 @@ dependencies {
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.1'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
// Unfortunately not maintained. See https://github.com/michaelklishin/quartz-mongodb
implementation "com.novemberain:quartz-mongodb:2.2.0-rc2"
implementation 'org.keycloak:keycloak-spring-boot-starter:23.0.4'
implementation 'io.hypersistence:hypersistence-utils-hibernate-63:3.8.3'

compileOnly 'org.projectlombok:lombok:1.18.30'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

runtimeOnly 'org.postgresql:postgresql'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test:6.2.1'
testRuntimeOnly 'com.h2database:h2'
}

tasks.named('test') {
Expand Down Expand Up @@ -103,7 +102,7 @@ tasks.register("bootRunDev") {
doFirst {
tasks.bootRun.configure {
systemProperty("spring.profiles.active", "dev")
systemProperty("MONGO_HOST", "localhost")
systemProperty("DB_HOST", "localhost")
}
}
finalizedBy("bootRun")
Expand Down
10 changes: 5 additions & 5 deletions dev-database-stack.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: '2'

services:
mongodb:
image: mongo:5.0.5
postgres:
image: postgres:17.0
ports:
- "27017:27017"
- "5432:5432"
volumes:
- mongodb_data:/data/db
- db_data:/data/db
restart: always
keycloak:
image: quay.io/keycloak/keycloak:23.0.4
Expand All @@ -21,5 +21,5 @@ services:
command: 'start-dev --import-realm'

volumes:
mongodb_data:
db_data:
driver: local
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ services:
env_file:
- .env-dev
environment:
- MONGO_HOST=db
- DB_HOST=db
- KEYCLOAK_URL=http://keycloak:9090
db:
image: mongo:5.0.5
image: postgres:17.0
restart: always
volumes:
- mongodb_data:/data/db
- db_data:/data/db
keycloak:
image: quay.io/keycloak/keycloak:23.0.4
environment:
Expand All @@ -31,5 +31,5 @@ services:
command: 'start-dev --import-realm'

volumes:
mongodb_data:
db_data:
driver: local
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.config.EnableMongoAuditing;

@SpringBootApplication
@EnableMongoAuditing
public class EventsignupApplication {

public static void main(String[] args) {
Expand Down

This file was deleted.

62 changes: 0 additions & 62 deletions src/main/java/fi/asteriski/eventsignup/config/QuartzConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -95,7 +96,7 @@ public List<ParticipantDto> getAllParticipants() {
})
})
@GetMapping("participants/{eventId}")
public List<ParticipantDto> getAllParticipantsForEvent(@PathVariable String eventId) {
public List<ParticipantDto> getAllParticipantsForEvent(@PathVariable UUID eventId) {
return adminService.getAllParticipantsForEvent(eventId);
}
}
Loading

0 comments on commit 6716bff

Please sign in to comment.