From e9e6bd69f9afa5a5eefe8b89341658aa28629ff6 Mon Sep 17 00:00:00 2001 From: Kruti Shah Date: Sun, 12 Oct 2025 21:59:55 +0530 Subject: [PATCH] Adding Postgres integration --- build.gradle | 4 ++ docker-compose.yaml | 21 ++++++++++- .../resources/application-local.properties | 9 ++++- src/main/resources/application.properties | 13 ++++++- src/main/resources/schema.sql | 37 +++++++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/schema.sql diff --git a/build.gradle b/build.gradle index bfc0540..2c09e0d 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + runtimeOnly 'org.postgresql:postgresql' + implementation 'org.hibernate.validator:hibernate-validator' } tasks.named('test') { diff --git a/docker-compose.yaml b/docker-compose.yaml index b014137..772008b 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,6 +12,23 @@ services: - OAUTH_CLIENT_ID=${OAUTH_CLIENT_ID} - OAUTH_CLIENT_SECRET=${OAUTH_CLIENT_SECRET} - HOST_PORT=${HOST_PORT} + - POSTGRES_USER=${POSTGRES_USERNAME} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} env_file: - - stack.env - restart: unless-stopped \ No newline at end of file + - .env + restart: unless-stopped + postgres: + image: postgres:15 + container_name: cftunnel-db + environment: + POSTGRES_DB: cftunnel + POSTGRES_USER: ${POSTGRES_USERNAME} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + restart: unless-stopped + ports: + - "5432:5432" + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: {} \ No newline at end of file diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index fdb25ce..21709da 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -1 +1,8 @@ -api.baseUrl=http://localhost:8080 \ No newline at end of file +api.baseUrl=http://localhost:8080 + +management.health.db.enabled=true +management.endpoints.web.exposure.include=health +management.endpoint.health.show-details=always + +logging.level.org.hibernate.SQL=DEBUG +debug=true diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5f1c452..16bd18b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,4 +22,15 @@ spring.security.oauth2.client.provider.cftunnels.authorization-uri=https://auth. spring.security.oauth2.client.provider.cftunnels.token-uri=https://auth.hithomelabs.com/application/o/token/ spring.security.oauth2.client.provider.cftunnels.user-info-uri=https://auth.hithomelabs.com/application/o/userinfo/ spring.security.oauth2.client.provider.cftunnels.jwk-set-uri=https://auth.hithomelabs.com/application/o/cftunnels/jwks/ -spring.security.oauth2.client.provider.cftunnels.issuer-uri=https://auth.hithomelabs.com/application/o/cftunnels/ \ No newline at end of file +spring.security.oauth2.client.provider.cftunnels.issuer-uri=https://auth.hithomelabs.com/application/o/cftunnels/ + +spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel +spring.datasource.username=${POSTGRES_USERNAME} +spring.datasource.password=${POSTGRES_PASSWORD} +spring.datasource.driver-class-name=org.postgresql.Driver +spring.sql.init.mode=always + +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect + diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000..1fa876d --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,37 @@ +-- schema.sql + +-- Roles table +CREATE TABLE IF NOT EXISTS roles ( + role_id SERIAL PRIMARY KEY, + role_name VARCHAR(50) UNIQUE NOT NULL +); + +-- Users table +CREATE TABLE IF NOT EXISTS users ( + user_id SERIAL PRIMARY KEY, + user_name VARCHAR(100) NOT NULL, + password VARCHAR(255) NOT NULL +); + +-- User-Role Mapping table (many-to-many relationship) +CREATE TABLE IF NOT EXISTS user_role_mapping ( + mapping_id SERIAL PRIMARY KEY, + user_id INTEGER NOT NULL REFERENCES users(user_id) ON DELETE CASCADE, + role_id INTEGER NOT NULL REFERENCES roles(role_id) ON DELETE CASCADE +); + +-- Tunnels table +CREATE TABLE IF NOT EXISTS tunnels ( + tunnel_id SERIAL PRIMARY KEY, + tunnel_name VARCHAR(100) NOT NULL, + tunnel_type VARCHAR(50) NOT NULL +); + +-- Mapping Requests table +CREATE TABLE IF NOT EXISTS mapping_requests ( + request_id SERIAL PRIMARY KEY, + request_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + status VARCHAR(20) NOT NULL, + user_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL, + tunnel_id INTEGER REFERENCES tunnels(tunnel_id) ON DELETE SET NULL +);