Skip to content

Commit 372c1c2

Browse files
committed
moved to src, prepare for unit testing
1 parent b189aaf commit 372c1c2

File tree

15 files changed

+121
-110
lines changed

15 files changed

+121
-110
lines changed

environment.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ignore": [".git", "node_modules/", "dist/"],
3-
"watch": ["server/"],
3+
"watch": ["src/"],
44
"execMap": {
55
"ts": "npx ts-node --esm"
66
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"build": "npx tsc",
8-
"start": "nodemon --config nodemon.json server/server.ts"
8+
"start": "nodemon --config nodemon.json src/server.ts"
99
},
1010
"dependencies": {
1111
"bcrypt": "^5.0.1",

server/server.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/app.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import express from 'express';
2+
import cors from 'cors';
3+
import connectRedis from 'connect-redis';
4+
import {createClient} from 'redis';
5+
import session from 'express-session';
6+
import env from './utils/env.js';
7+
import cookieParser from 'cookie-parser';
8+
import fileupload from 'express-fileupload';
9+
import auth from './endpoints/auth.js';
10+
import video from './endpoints/video.js';
11+
import videos from './endpoints/videos.js';
12+
import image from './endpoints/image.js';
13+
import requireAuth from './middleware/requireAuth.js';
14+
15+
const app = express();
16+
17+
const allowedOrigins = ['http://localhost', 'http://localhost:8080'];
18+
const options: cors.CorsOptions = {
19+
origin: allowedOrigins,
20+
credentials: true,
21+
};
22+
23+
const RedisStore = connectRedis(session);
24+
const redisClient = createClient({legacyMode: true});
25+
26+
redisClient.connect().then(() => {
27+
console.log('Successfully connected to redis.');
28+
}).catch((_error:any) => {
29+
console.log('Failed to connect to redis, server not running...');
30+
process.exitCode = 1;
31+
process.exit();
32+
});
33+
34+
app.use(cors(options));
35+
app.use(cookieParser());
36+
app.use(session({
37+
store: new RedisStore({client: redisClient}),
38+
secret: env.SESSION_SECRET,
39+
resave: false,
40+
saveUninitialized: false,
41+
cookie: {
42+
secure: false, // Change in production
43+
httpOnly: true,
44+
},
45+
}));
46+
app.use(requireAuth);
47+
app.use(express.json());
48+
app.use(fileupload({
49+
useTempFiles: true,
50+
tempFileDir: '/tmp/',
51+
}));
52+
53+
app.use('/api/auth', auth);
54+
app.use('/api/video', video);
55+
app.use('/api/videos', videos);
56+
app.use('/api/image', image);
57+
58+
export default app;
File renamed without changes.

server/endpoints/auth.ts renamed to src/endpoints/auth.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ import {getDb} from '../db/mongo.js';
77
const auth = express.Router();
88
// const saltRounds = Number(process.env.SALT_ROUNDS);
99

10-
declare module 'express-session' {
11-
// eslint-disable-next-line no-unused-vars
12-
interface SessionData {
13-
userId: string;
14-
user: string;
15-
profile: number;
16-
}
17-
}
18-
1910
auth.post('/', function(req, res) {
2011
const {user, password} = req.body;
2112

@@ -36,7 +27,10 @@ auth.post('/', function(req, res) {
3627
dbResult.password,
3728
function(_err, authResult) {
3829
if (authResult) {
39-
req.session.userId = dbResult._id;
30+
// console.log(dbResult);
31+
// console.log(req.session);
32+
33+
req.session.userId = dbResult._id.toString();
4034
req.session.user = dbResult.username;
4135
req.session.profile = dbResult.profile;
4236
res.json(
File renamed without changes.

server/endpoints/video.ts renamed to src/endpoints/video.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ video.post('/upload', function(req, res) {
9292

9393
const relPath = `/${year}/${month}/${day}`;
9494

95-
/** Insert video in database */
9695
const db = getDb();
9796

9897
video.mv(tempDir + '/' + video.name).then(() => {
File renamed without changes.

server/middleware/requireAuth.ts renamed to src/middleware/requireAuth.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import {Request, Response, NextFunction} from 'express';
77
* @param {NextFunction} next NextFunction
88
*/
99
function requireAuth(req: Request, res: Response, next: NextFunction) {
10-
// console.log(req.path);
11-
// console.log(req.session);
12-
1310
if (req.path !== '/api/auth') {
14-
if (req.session.userId === undefined) {
11+
if (req.session === undefined || req.session.userId === undefined) {
1512
console.log('Bloqued request!');
1613
res.status(400).send('Invalid user');
1714
return;

src/server.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'fs';
2+
import app from './app.js';
3+
import env from './utils/env.js';
4+
import {connectToServer} from './db/mongo.js';
5+
6+
fs.stat(env.STORAGE, (err, stats) => {
7+
if (err) {
8+
// console.log(err.message);
9+
console.log('storage not found, server not running.');
10+
process.exitCode = 1;
11+
process.exit();
12+
}
13+
14+
if (!stats.isDirectory()) {
15+
console.log('storage is not a directory, server not running.');
16+
process.exitCode = 1;
17+
process.exit();
18+
}
19+
20+
connectToServer(function(error: any) {
21+
if (error) {
22+
console.log('Couldn\'t connect to database, server not running...');
23+
process.exitCode = 1;
24+
process.exit();
25+
}
26+
27+
app.listen(env.PORT, function() {
28+
console.log(`[server]: Server is running on port ${env.PORT}`);
29+
});
30+
});
31+
});

src/types/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import {SessionData} from 'express-session';
3+
4+
declare module 'express-session' {
5+
export interface SessionData {
6+
userId?: string;
7+
user?: string;
8+
profile?: number;
9+
}
10+
}

src/utils/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'dotenv/config';
2+
3+
export default {
4+
PORT: process.env.PORT || 8000,
5+
SESSION_SECRET: process.env.SESSION_SECRET || 'must change this!!!',
6+
FFMPEG: process.env.FFMPEG || '/usr/bin/ffmpeg',
7+
FFPROBE: process.env.FFPROBE || '/usr/bin/ffprobe',
8+
STORAGE: process.env.STORAGE || '/mnt/storage',
9+
};

tsconfig.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2+
"ts-node": {
3+
"files": true
4+
},
25
"compilerOptions": {
3-
"target": "ES2020",
6+
"target": "ES2022",
47
"module": "ES2022",
58
"moduleResolution": "node",
69
"outDir": "./dist",
@@ -11,6 +14,6 @@
1114
"allowJs": false,
1215
"noImplicitAny": true,
1316
},
14-
"include": [ "server/**/*" ],
15-
"exclude": ["node_modules", "**/node_modules/*"],
17+
"include": [ "./src/**/*" ],
18+
"exclude": ["./node_modules", "**/node_modules/*"],
1619
}

0 commit comments

Comments
 (0)