Skip to content

Commit

Permalink
Merge pull request #148 from bl4drnnr/web-interface-dev
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
bl4drnnr authored Apr 5, 2024
2 parents 33d185f + 21d99f4 commit 1d501a1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
42 changes: 20 additions & 22 deletions mnemosyne-web-interface/mnemosyne-api-proxy/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@ import * as fs from 'fs';
import * as yaml from 'yaml';
import * as bodyParser from 'body-parser';
import { urlencoded, json } from 'express';
import { ImATeapotException } from '@nestjs/common';

(async () => {
const whitelist = ['http://localhost:4200', 'https://mnemosyne.io'];
const whitelist = [
'http://localhost:4200',
'https://mnemosyne.io',
'https://proxy.mnemosyne.io'
];

const app = await NestFactory.create(AppModule, {
cors: {
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
console.log('origin API Proxy: ', origin);
if (!origin) {
callback(null, true);
return;
}
if (whitelist.includes(origin) || !!origin.match(/mnemosyne\.io$/)) {
console.log('Allowed CORS (API Proxy) for: ', origin);
callback(null, true);
} else {
console.log('Blocked CORS (API Proxy) for: ', origin);
callback(new ImATeapotException('Not allowed by CORS'), false);
}
}
}
});
const app = await NestFactory.create(AppModule);
const port = process.env.PROXY_PORT;

app.setGlobalPrefix('/api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));

app.enableCors({
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
console.log('allowed cors for (API Proxy):', origin);
callback(null, true);
} else {
console.log('blocked cors for:', origin);
callback(new Error('Not allowed by CORS (API Proxy)'));
}
}
});

// app.enableCors({
// allowedHeaders: ['content-type'],
// origin: 'https://mnemosyne.io',
Expand Down
12 changes: 6 additions & 6 deletions mnemosyne-web-interface/mnemosyne-api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import { Category } from '@models/category.model';
username: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
},
// dialectOptions: {
// ssl: {
// require: true,
// rejectUnauthorized: false
// }
// },
transactionType: TYPES.EXCLUSIVE,
models: [
User,
Expand Down
42 changes: 20 additions & 22 deletions mnemosyne-web-interface/mnemosyne-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,35 @@ import * as bodyParser from 'body-parser';
import * as fs from 'fs';
import * as yaml from 'yaml';
import { urlencoded, json } from 'express';
import { ImATeapotException } from '@nestjs/common';

(async () => {
const whitelist = ['http://localhost:4201', 'https://proxy.mnemosyne.io'];
const whitelist = [
'http://localhost:4201',
'https://mnemosyne.io',
'https://proxy.mnemosyne.io'
];

const app = await NestFactory.create(AppModule, {
cors: {
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
console.log('origin API: ', origin);
if (!origin) {
callback(null, true);
return;
}
if (whitelist.includes(origin) || !!origin.match(/mnemosyne\.io$/)) {
console.log('Allowed CORS (API) for: ', origin);
callback(null, true);
} else {
console.log('Blocked CORS (API) for: ', origin);
callback(new ImATeapotException('Not allowed by CORS'), false);
}
}
}
});
const app = await NestFactory.create(AppModule);
const port = process.env.API_PORT || 3000;

app.setGlobalPrefix('/api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));

app.enableCors({
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
credentials: true,
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
console.log('allowed cors for (API):', origin);
callback(null, true);
} else {
console.log('blocked cors for:', origin);
callback(new Error('Not allowed by CORS (API)'));
}
}
});

// app.enableCors({
// origin: 'https://proxy.mnemosyne.io',
// credentials: true
Expand Down

0 comments on commit 1d501a1

Please sign in to comment.