Skip to content
Open

1306 #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/LeftMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import SchoolOutlinedIcon from '@material-ui/icons/SchoolOutlined';
import SupervisorAccountOutlinedIcon from '@material-ui/icons/SupervisorAccountOutlined';
import CropFreeOutlinedIcon from '@material-ui/icons/CropFreeOutlined';
import { ERole } from '../types/ERole';
import { useSession } from 'next-auth/client';

const drawerWidth = 240;

Expand Down Expand Up @@ -218,6 +219,20 @@ export default function LeftMenu({
</Link>
</ListItem>

<ListItem button key={PAGES.testUpload.path}>
<CropFreeOutlinedIcon />
<Link
as={`${PAGES.testUpload.path}/${currentId}`}
href={`${PAGES.testUpload.path}/[test_upload]`}
>
<a className={classes.link}>
<ListItemText
primary={PAGES.testUpload.title}
/>
</a>
</Link>
</ListItem>

<ListItem button key={PAGES.testMongo.path}>
<CropFreeOutlinedIcon />
<Link href={PAGES.testMongo.path}>
Expand Down
5 changes: 5 additions & 0 deletions constants/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export const PAGES = {
path: '/test_mui',
title: 'Test Mongo & MUI Integration Page',
},
testUpload: {
id: 'testUpload',
path: '/test_upload',
title: 'Test Upload Page',
},
adminpage: {
id: 'adminpage',
path: '/admin',
Expand Down
30 changes: 24 additions & 6 deletions middleware/connectDB.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import mongoose from 'mongoose';

const fs = require('fs');
const path = require('path');

console.log('MONGO_URI!!!!', process.env.MONGODB_URI);
console.log('DB', process.env.DB);

export const connectDB = async () => {
if (!mongoose.connections[0]?.readyState) {
await mongoose.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
});
if (process.env.DB === 'AWS') {
mongoose
.connect(process.env.MONGODB_URI_AWS, {
useNewUrlParser: true,
ssl: true,
sslValidate: true,
sslCA: fs.readFileSync(
path.join(process.cwd(), 'rds-combined-ca-bundle.pem')
),
})
.then(() => console.log('Connection to DB successful'))
.catch((err) => console.error(err, 'Error'));
} else {
await mongoose.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
});
}
}
};
30 changes: 22 additions & 8 deletions middleware/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import mongoose from 'mongoose';
import { NextApiRequest, NextApiResponse } from 'next';
const fs = require('fs');
const path = require('path');

const connectDB = (handler: any) => async (
req: NextApiRequest,
Expand All @@ -9,14 +11,26 @@ const connectDB = (handler: any) => async (
// Use current db connection
return handler(req, res);
}
// Use new db connection
await mongoose.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
});
return handler(req, res);

if (process.env.DB === 'AWS') {
await mongoose.connect(process.env.MONGODB_URI_AWS, {
useNewUrlParser: true,
ssl: true,
sslValidate: true,
sslCA: fs.readFileSync(
path.join(process.cwd(), 'rds-combined-ca-bundle.pem')
),
});
return handler(req, res);
} else {
await mongoose.connect(process.env.MONGODB_URI, {
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
useNewUrlParser: true,
});
return handler(req, res);
}
};

export default connectDB;
16 changes: 14 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@ module.exports = (phase) => {
const isStaging =
phase === PHASE_PRODUCTION_BUILD && process.env.STAGING === '1';

//put ec2 instance link
const EC2AWS = 'http://ec2-3-66-89-198.eu-central-1.compute.amazonaws.com';

console.log(`isDev:${isDev} isProd:${isProd} isStaging:${isStaging}`);
console.log('DB', process.env.DB);

const env = {
RESTURL: (() => {
if (isDev) return 'http://localhost:3000';
if (isProd) {
if (isProd && process.env.DB !== 'AWS') {
return 'https://kr-web.klishevich.ru';
}
if (isProd && process.env.DB === 'AWS') {
return EC2AWS;
}
if (isStaging) return 'https://localhost:3000';
return 'RESTURL:not (isDev,isProd && !isStaging,isProd && isStaging)';
})(),
RESTURL_SESSIONS: (() => {
if (isDev) return 'http://localhost:3000/sessions';
if (isProd) return 'https://kr-web.klishevich.ru/sessions';
if (isProd && process.env.DB !== 'AWS') {
return 'https://kr-web.klishevich.ru/sessions';
}
if (isProd && process.env.DB === 'AWS') {
return `${EC2AWS}/sessions`;
}
if (isStaging) return 'http://localhost:3000';
return 'RESTURL_SESSIONS:not (isDev,isProd && !isStaging,isProd && isStaging)';
})(),
Expand Down
Loading