Skip to content

Commit 1611e83

Browse files
committed
fix: udpated to server.ts file
1 parent 780fa84 commit 1611e83

File tree

2 files changed

+84
-78
lines changed

2 files changed

+84
-78
lines changed

src/index.ts

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,22 @@
1+
import app from './server';
12
import express from 'express';
2-
import bodyParser from 'body-parser';
3-
import fs from 'fs';
4-
import swaggerUi from 'swagger-ui-express';
5-
import YAML from 'yamljs';
6-
7-
import { createSchema, getSchemaById } from './controller/schema_controller';
83
import { createConnection } from 'typeorm';
94
import { dbConfig } from './dbconfig';
105
import { addDelegateAsRegistryDelegate } from './init';
6+
import {
7+
createSchema,
8+
getSchemaById,
9+
} from './controller/schema_controller';
1110
import {
1211
documentHashOnChain,
1312
getCredById,
1413
issueVC,
1514
revokeCred,
1615
updateCred,
1716
} from './controller/credential_controller';
18-
import cors from 'cors';
19-
const app = express();
20-
export const { PORT } = process.env;
21-
22-
app.use(bodyParser.json({ limit: '5mb' }));
23-
app.use(express.json());
24-
25-
26-
const allowedOrigins = [
27-
'http://localhost:3000',
28-
'http://localhost:5001',
29-
'http://localhost:5108',
30-
'https://studio.dhiway.com',
31-
'https://markdemo.dhiway.com',
32-
'https://studiodemo.dhiway.com',
33-
];
34-
35-
36-
const allowedDomains = [
37-
'localhost',
38-
'dhiway.com',
39-
'dway.io',
40-
'cord.network',
41-
'amplifyapp.com' /* For supporting quick hosting of UI */,
42-
];
43-
44-
app.use(
45-
cors({
46-
origin: function (origin, callback) {
47-
if (!origin) return callback(null, true);
48-
let tmpOrigin = origin;
49-
50-
if (origin.slice(-1) === '/') {
51-
tmpOrigin = origin.substring(0, origin.length - 1);
52-
}
53-
if (allowedOrigins.indexOf(tmpOrigin) === -1) {
54-
/* Check if we should allow star/asteric */
55-
const b = tmpOrigin.split('/')[2].split('.');
56-
const domain = `${b[b.length - 2]}.${b[b.length - 1]}`;
57-
if (allowedDomains.indexOf(domain) === -1) {
58-
console.log(tmpOrigin, domain);
59-
const msg = `The CORS policy for this site (${origin}) does not allow access from the specified Origin.`;
60-
return callback(new Error(msg), false);
61-
}
62-
}
63-
return callback(null, true);
64-
},
65-
optionsSuccessStatus: 200, // For legacy browser support
66-
credentials: true,
67-
preflightContinue: true,
68-
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'],
69-
allowedHeaders: [
70-
'Content-Type',
71-
'X-UserId',
72-
'Accept',
73-
'Authorization',
74-
'user-agent',
75-
'Host',
76-
'X-Forwarded-For',
77-
'Upgrade',
78-
'Connection',
79-
'X-Content-Type-Options',
80-
'Content-Security-Policy',
81-
'X-Frame-Options',
82-
'Strict-Transport-Security',
83-
],
84-
})
85-
);
17+
const {
18+
PORT
19+
} = process.env;
8620

8721
const credentialRouter = express.Router({ mergeParams: true });
8822
const schemaRouter = express.Router({ mergeParams: true });
@@ -111,9 +45,6 @@ schemaRouter.get('/:id', async (req, res) => {
11145
return await getSchemaById(req, res);
11246
});
11347

114-
const openApiDocumentation = YAML.load('./apis.yaml');
115-
116-
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocumentation));
11748
app.use('/api/v1/schema', schemaRouter);
11849
app.use('/api/v1/cred', credentialRouter);
11950

@@ -130,7 +61,6 @@ app.get('/*', async (req, res) => {
13061
async function main() {
13162
try {
13263
await createConnection(dbConfig);
133-
13464
await addDelegateAsRegistryDelegate();
13565
} catch (error) {
13666
console.log('error: ', error);

src/server.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import express from 'express';
2+
import bodyParser from 'body-parser';
3+
import cors from 'cors';
4+
import YAML from 'yamljs';
5+
import swaggerUi from 'swagger-ui-express';
6+
7+
const app = express();
8+
export const { PORT } = process.env;
9+
10+
app.use(bodyParser.json({ limit: '5mb' }));
11+
app.use(express.json());
12+
13+
const allowedOrigins = [
14+
'http://localhost:3000',
15+
'http://localhost:5001',
16+
'http://localhost:5108',
17+
'https://studio.dhiway.com',
18+
'https://markdemo.dhiway.com',
19+
'https://studiodemo.dhiway.com',
20+
];
21+
22+
const allowedDomains = [
23+
'localhost',
24+
'dhiway.com',
25+
'dway.io',
26+
'cord.network',
27+
'amplifyapp.com' /* For supporting quick hosting of UI */,
28+
];
29+
30+
app.use(
31+
cors({
32+
origin: function (origin, callback) {
33+
if (!origin) return callback(null, true);
34+
let tmpOrigin = origin;
35+
36+
if (origin.slice(-1) === '/') {
37+
tmpOrigin = origin.substring(0, origin.length - 1);
38+
}
39+
if (allowedOrigins.indexOf(tmpOrigin) === -1) {
40+
/* Check if we should allow star/asterisk */
41+
const b = tmpOrigin.split('/')[2].split('.');
42+
const domain = `${b[b.length - 2]}.${b[b.length - 1]}`;
43+
if (allowedDomains.indexOf(domain) === -1) {
44+
console.log(tmpOrigin, domain);
45+
const msg = `The CORS policy for this site (${origin}) does not allow access from the specified Origin.`;
46+
return callback(new Error(msg), false);
47+
}
48+
}
49+
return callback(null, true);
50+
},
51+
optionsSuccessStatus: 200, // For legacy browser support
52+
credentials: true,
53+
preflightContinue: true,
54+
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH'],
55+
allowedHeaders: [
56+
'Content-Type',
57+
'X-UserId',
58+
'Accept',
59+
'Authorization',
60+
'user-agent',
61+
'Host',
62+
'X-Forwarded-For',
63+
'Upgrade',
64+
'Connection',
65+
'X-Content-Type-Options',
66+
'Content-Security-Policy',
67+
'X-Frame-Options',
68+
'Strict-Transport-Security',
69+
],
70+
})
71+
);
72+
73+
const openApiDocumentation = YAML.load('./apis.yaml');
74+
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocumentation));
75+
76+
export default app;

0 commit comments

Comments
 (0)