Skip to content

Commit

Permalink
Merge pull request #611 from encorelab/610-bug-avoid-hard-coding-prod…
Browse files Browse the repository at this point in the history
…uction-urls-and-fix-web-sockets

Fixed hardcoding dev/prod URLs and web socket port
  • Loading branch information
markiianbabiak authored Oct 22, 2024
2 parents 9d2571c + d0d1319 commit 1c1414f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 36 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**
Expand Down
8 changes: 7 additions & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ 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', 'cache', 'timeout'],
})
);
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../../frontend/dist/ck-board')));
const server = http.createServer(app);
Expand Down
26 changes: 21 additions & 5 deletions backend/src/socket/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
};

Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/utils/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}`,
},
Expand Down
12 changes: 3 additions & 9 deletions frontend/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -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',
};
12 changes: 3 additions & 9 deletions frontend/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

/*
Expand Down

0 comments on commit 1c1414f

Please sign in to comment.