Skip to content

Commit

Permalink
feat(common): ajout du database service, ajout d'un endpoint de version
Browse files Browse the repository at this point in the history
  • Loading branch information
dthib committed Jul 22, 2024
1 parent 735f80b commit 815cf87
Show file tree
Hide file tree
Showing 9 changed files with 652 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ backend-build:
backend-deploy: ## Déploie le backend dans une app Koyeb existante
ARG --required KOYEB_API_KEY
FROM +koyeb
RUN ./koyeb services update $ENV_NAME-backend/backend --docker $BACKEND_IMG_NAME --env GIT_COMMIT_SHORT_SHA=$GIT_COMMIT_SHORT_SHA --env GCLOUD_SERVICE_ACCOUNT_KEY=@GCLOUD_SERVICE_ACCOUNT_KEY
RUN ./koyeb services update $ENV_NAME-backend/backend \
--docker $BACKEND_IMG_NAME \
--env GIT_COMMIT_SHORT_SHA=$GIT_COMMIT_SHORT_SHA \
--env GCLOUD_SERVICE_ACCOUNT_KEY=@GCLOUD_SERVICE_ACCOUNT_KEY \
--env DATABASE_URL=@SUPABASE_DATABASE_URL_PREPROD

app-build: ## construit l'image de l'app
ARG PLATFORM
Expand Down
3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"@sentry/nestjs": "^8.19.0",
"@sentry/profiling-node": "^8.19.0",
"async-retry": "^1.3.3",
"drizzle-orm": "^0.32.0",
"gaxios": "^6.7.0",
"google-auth-library": "^9.11.0",
"googleapis": "^140.0.1",
"postgres": "^3.4.4",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand All @@ -43,6 +45,7 @@
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"drizzle-kit": "^0.23.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
6 changes: 3 additions & 3 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SheetModule } from './spreadsheets/SheetModule';

import { SheetModule } from './spreadsheets/sheet.module';
import { CommonModule } from './common/common.module';
@Module({
imports: [SheetModule],
imports: [CommonModule, SheetModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
10 changes: 10 additions & 0 deletions backend/src/common/common.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { VersionController } from './controllers/version.controller';
import DatabaseService from './services/database.service';

@Module({
providers: [DatabaseService],
exports: [DatabaseService],
controllers: [VersionController],
})
export class CommonModule {}
12 changes: 12 additions & 0 deletions backend/src/common/controllers/version.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';

@Controller()
export class VersionController {
//@PublicEndpoint()
@Get('version')
async getVersion() {
return {
commit: process.env.GIT_COMMIT_SHORT_SHA,
};
}
}
18 changes: 18 additions & 0 deletions backend/src/common/services/database.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import * as postgres from 'postgres';

@Injectable()
export default class DatabaseService {
public readonly db: PostgresJsDatabase;

constructor() {
if (!process.env.DATABASE_URL) {
process.env.DATABASE_URL =
'postgresql://postgres:postgres@localhost:54322/postgres';
}
console.log(`Initializing database service`);
const client = postgres(process.env.DATABASE_URL!);
this.db = drizzle(client);
}
}
File renamed without changes.
9 changes: 7 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// WARNING: Do this import first
import './common/configs/SentryInit';
import './common/services/sentry.service';
import * as Sentry from '@sentry/nestjs';
import {
BaseExceptionFilter,
HttpAdapterHost,
NestFactory,
} from '@nestjs/core';
import { AppModule } from './app.module';
import { SENTRY_DSN } from './common/configs/SentryInit';
import { SENTRY_DSN } from './common/services/sentry.service';
import * as fs from 'fs';

const port = process.env.PORT || 8080;
Expand All @@ -30,6 +30,11 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
const { httpAdapter } = app.get(HttpAdapterHost);

// Seulement une v1 pour l'instant
app.setGlobalPrefix('api/v1', {
exclude: ['version'],
});

if (SENTRY_DSN) {
console.log('Sentry enabled with DSN:', SENTRY_DSN);
Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
Expand Down
Loading

0 comments on commit 815cf87

Please sign in to comment.