From e608f1638b70841a9bff36f6a0e1b367f9070c71 Mon Sep 17 00:00:00 2001 From: JoelWiebe Date: Mon, 21 Oct 2024 23:44:10 -0400 Subject: [PATCH 1/4] Updated angular environment variables and backend .env variables to provide all URLs for prod and dev configurations --- backend/src/server.ts | 6 ++++- backend/src/socket/socket.ts | 26 +++++++++++++++---- frontend/src/app/app.module.ts | 6 ++--- frontend/src/app/utils/interceptor.ts | 3 ++- frontend/src/environments/environment.prod.ts | 12 +++------ frontend/src/environments/environment.ts | 12 +++------ 6 files changed, 37 insertions(+), 28 deletions(-) diff --git a/backend/src/server.ts b/backend/src/server.ts index 7d966a94..5c99afc1 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -35,7 +35,11 @@ const redisPort = (process.env.REDIS_PORT || 6379) as number; const redisPassword = process.env.REDIS_PASSWORD || ''; const app = express(); -app.use(cors()); +app.use(cors({ + origin: process.env.CKBOARD_SERVER_ADDRESS, + methods: ['GET', 'POST', 'PUT', 'DELETE'], + allowedHeaders: ['Content-Type', 'Authorization'], +})); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, '../../frontend/dist/ck-board'))); const server = http.createServer(app); diff --git a/backend/src/socket/socket.ts b/backend/src/socket/socket.ts index b09f8f13..417438fa 100644 --- a/backend/src/socket/socket.ts +++ b/backend/src/socket/socket.ts @@ -5,6 +5,9 @@ import events from './events'; import SocketManager from './socketManager'; import RedisClient from '../utils/redis'; import { createAdapter } from '@socket.io/redis-adapter'; +import dotenv from 'dotenv'; + +dotenv.config(); class Socket { private static _instance: Socket; @@ -30,13 +33,26 @@ class Socket { */ init(redis: RedisClient) { + const origins = []; + + if (process.env.CKBOARD_SERVER_ADDRESS) { + try { + const url = new URL(process.env.CKBOARD_SERVER_ADDRESS); + if (url.protocol === 'https:' || url.protocol === 'http:') { + origins.push(process.env.CKBOARD_SERVER_ADDRESS); + } else { + console.error('Invalid protocol in CKBOARD_SERVER_ADDRESS.'); + } + } catch (error) { + console.error('Invalid URL in CKBOARD_SERVER_ADDRESS.', error); + } + } else { + console.error('CKBOARD_SERVER_ADDRESS environment variable not set.'); + } + const io = new socketIO.Server(8000, { cors: { - origin: [ - 'http://localhost:4200', - 'http://localhost:4201', - 'http://localhost:8001', - ], + origin: origins }, adapter: createAdapter(redis.getPublisher, redis.getSubscriber), }); diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index ba80f96d..1245db47 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -65,7 +65,7 @@ import { ToolbarMenuComponent } from './components/toolbar-menu/toolbar-menu.com import { ViewNavigationComponent } from './components/view-navigation/view-navigation.component'; const config: SocketIoConfig = { - url: 'https://ck-board.oise.utoronto.ca/', + url: environment.socketUrl, options: {}, }; @@ -125,8 +125,8 @@ export function tokenGetter() { JwtModule.forRoot({ config: { tokenGetter: tokenGetter, - allowedDomains: ['https://ck-board.oise.utoronto.ca/'], - disallowedRoutes: ['https://ck-board.oise.utoronto.ca/api/auth'], + allowedDomains: [environment.ckboardDomain], + disallowedRoutes: [`${environment.ckboardDomain}/api/auth`], }, }), SocketIoModule.forRoot(config), diff --git a/frontend/src/app/utils/interceptor.ts b/frontend/src/app/utils/interceptor.ts index f0109848..2b846e9b 100644 --- a/frontend/src/app/utils/interceptor.ts +++ b/frontend/src/app/utils/interceptor.ts @@ -9,6 +9,7 @@ import { import { Observable, of } from 'rxjs'; import { tap, timeout } from 'rxjs/operators'; import { UserService } from '../services/user.service'; +import { environment } from 'src/environments/environment'; export const DEFAULT_TIMEOUT = 30000; @@ -27,7 +28,7 @@ export class APIInterceptor implements HttpInterceptor { const timeoutValue = Number(req.headers.get('timeout') || DEFAULT_TIMEOUT); const apiReq = req.clone({ - url: `https://ck-board.oise.utoronto.ca/api/${req.url}`, + url: `${environment.ckboardDomain}/api/${req.url}`, setHeaders: { Authorization: `Bearer ${this.auth.token}`, }, diff --git a/frontend/src/environments/environment.prod.ts b/frontend/src/environments/environment.prod.ts index c3777f06..31924bfa 100644 --- a/frontend/src/environments/environment.prod.ts +++ b/frontend/src/environments/environment.prod.ts @@ -1,12 +1,6 @@ export const environment = { production: true, - firebaseConfig: { - apiKey: 'AIzaSyCmsP1M4ie5rx1BYPwpPWhsalw6LpHcrMw', - authDomain: 'ck-board.firebaseapp.com', - projectId: 'ck-board', - storageBucket: 'ck-board.appspot.com', - messagingSenderId: '473282317820', - appId: '1:473282317820:web:0bc303a476e0f514153679', - measurementId: 'G-00CHHWR3SW', - }, + apiUrl: 'https://ck-board.oise.utoronto.ca/api', + socketUrl: 'https://ck-board.oise.utoronto.ca:8000', + ckboardDomain: 'https://ck-board.oise.utoronto.ca', }; diff --git a/frontend/src/environments/environment.ts b/frontend/src/environments/environment.ts index 1e8318bd..a4fcb01c 100644 --- a/frontend/src/environments/environment.ts +++ b/frontend/src/environments/environment.ts @@ -4,15 +4,9 @@ export const environment = { production: false, - firebaseConfig: { - apiKey: 'AIzaSyCmsP1M4ie5rx1BYPwpPWhsalw6LpHcrMw', - authDomain: 'ck-board.firebaseapp.com', - projectId: 'ck-board', - storageBucket: 'ck-board.appspot.com', - messagingSenderId: '473282317820', - appId: '1:473282317820:web:0bc303a476e0f514153679', - measurementId: 'G-00CHHWR3SW', - }, + apiUrl: 'http://localhost/api', + socketUrl: 'http://localhost:8000', + ckboardDomain: 'http://localhost:8001', }; /* From 430bf818a00d9660b3fc0cc37a1d9bbbad159bf2 Mon Sep 17 00:00:00 2001 From: JoelWiebe Date: Mon, 21 Oct 2024 23:51:48 -0400 Subject: [PATCH 2/4] Prettier code --- backend/src/server.ts | 12 +++++++----- backend/src/socket/socket.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/src/server.ts b/backend/src/server.ts index 5c99afc1..0d923f15 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -35,11 +35,13 @@ const redisPort = (process.env.REDIS_PORT || 6379) as number; const redisPassword = process.env.REDIS_PASSWORD || ''; const app = express(); -app.use(cors({ - origin: process.env.CKBOARD_SERVER_ADDRESS, - methods: ['GET', 'POST', 'PUT', 'DELETE'], - allowedHeaders: ['Content-Type', 'Authorization'], -})); +app.use( + cors({ + origin: process.env.CKBOARD_SERVER_ADDRESS, + methods: ['GET', 'POST', 'PUT', 'DELETE'], + allowedHeaders: ['Content-Type', 'Authorization'], + }) +); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, '../../frontend/dist/ck-board'))); const server = http.createServer(app); diff --git a/backend/src/socket/socket.ts b/backend/src/socket/socket.ts index 417438fa..f51da3ce 100644 --- a/backend/src/socket/socket.ts +++ b/backend/src/socket/socket.ts @@ -52,7 +52,7 @@ class Socket { const io = new socketIO.Server(8000, { cors: { - origin: origins + origin: origins, }, adapter: createAdapter(redis.getPublisher, redis.getSubscriber), }); From 9367bff550fae9e092119599a2ffd21311fbf27c Mon Sep 17 00:00:00 2001 From: JoelWiebe Date: Tue, 22 Oct 2024 10:18:55 -0400 Subject: [PATCH 3/4] Updated README with new environment variable --- README.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ef021c26..e192feb9 100644 --- a/README.md +++ b/README.md @@ -46,15 +46,9 @@ DB_URL=[MongoDB URL] DB_NAME=[MongoDB Name] JWT_SECRET=[JWT Secret Token] PORT=8001 -``` - -**Google Vertex AI** - -This application utilizes Google Vertex AI for its AI Assistant feature. To enable this functionality, you'll need to set up the following environment variables: - -``` +CKBOARD_SERVER_ADDRESS=[For dev, use "http://localhost:4201"; for production, use server URL, e.g., "https://ck-board.oise.utoronto.ca"] GOOGLE_APPLICATION_CREDENTIALS="./secrets/keyfile.json" -GOOGLE_CLOUD_PROJECT=ck-ai-assistant +GOOGLE_CLOUD_PROJECT=[Google Cloud Project ID] ``` **1. `GOOGLE_APPLICATION_CREDENTIALS`** From d0d1319723113ebb77150e07de2f03921eff4964 Mon Sep 17 00:00:00 2001 From: markiianbabiak Date: Tue, 22 Oct 2024 11:13:18 -0400 Subject: [PATCH 4/4] Fix the console error when creating posts --- backend/src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/server.ts b/backend/src/server.ts index 0d923f15..a234688e 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -39,7 +39,7 @@ app.use( cors({ origin: process.env.CKBOARD_SERVER_ADDRESS, methods: ['GET', 'POST', 'PUT', 'DELETE'], - allowedHeaders: ['Content-Type', 'Authorization'], + allowedHeaders: ['Content-Type', 'Authorization', 'cache', 'timeout'], }) ); app.use(bodyParser.json());