Skip to content

Commit b835b4b

Browse files
committed
Basic Application setup
1 parent 74a14b4 commit b835b4b

File tree

9 files changed

+1826
-0
lines changed

9 files changed

+1826
-0
lines changed

application/nodemon.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"watch": ["src"],
3+
"ext": ".ts,.js",
4+
"exec": "ts-node ./src/index.ts"
5+
}

application/package-lock.json

Lines changed: 1605 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

application/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "codeberg-application",
3+
"version": "1.0.0",
4+
"description": "Server for managing services for codeberg",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "cd src && npx tsc",
9+
"start": "node dist/index.js",
10+
"dev": "nodemon"
11+
},
12+
"keywords": [
13+
"codeberg",
14+
"coding"
15+
],
16+
"author": "Saket Aryan",
17+
"license": "ISC",
18+
"devDependencies": {
19+
"@types/cors": "^2.8.13",
20+
"@types/express": "^4.17.17",
21+
"@types/jsonwebtoken": "^9.0.2",
22+
"@types/lodash": "^4.14.196",
23+
"nodemon": "^3.0.1",
24+
"typescript": "^5.1.6"
25+
},
26+
"dependencies": {
27+
"cors": "^2.8.5",
28+
"dotenv": "^16.3.1",
29+
"express": "^4.18.2",
30+
"jsonwebtoken": "^9.0.1",
31+
"lodash": "^4.17.21",
32+
"mongoose": "^7.4.2",
33+
"ts-node": "^10.9.1"
34+
}
35+
}

application/src/database.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import dotenv from 'dotenv';
2+
import mongoose from 'mongoose';
3+
4+
dotenv.config();
5+
6+
const MONGO_URI = process.env.MONGO as string;
7+
8+
const connectDB = async () => {
9+
try {
10+
await mongoose.connect(MONGO_URI);
11+
console.log('MongoDB connection SUCCESS')
12+
} catch (error) {
13+
console.error('MongoDB connection FAIL')
14+
process.exit(1)
15+
}
16+
}
17+
18+
export default connectDB;

application/src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import express, { Express, Request, Response } from 'express';
2+
import dotenv from 'dotenv';
3+
dotenv.config();
4+
5+
import router from './routes';
6+
7+
import cors from 'cors';
8+
import connectDB from './database';
9+
10+
connectDB();
11+
12+
const app: Express = express();
13+
const port = process.env.PORT || 9001;
14+
15+
app.use(cors());
16+
app.use(express.json());
17+
18+
app.use('/', router());
19+
20+
app.listen(port, () => {
21+
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
22+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Request, Response, NextFunction } from 'express';
2+
import dotenv from 'dotenv';
3+
dotenv.config();
4+
import jwt from 'jsonwebtoken';
5+
import { merge } from 'lodash';
6+
7+
const JWT_SECRET = process.env.JWT_SECRET as string;
8+
9+
interface UserPayload {
10+
user: string;
11+
}
12+
13+
const fetchuser = (req: Request, res: Response, next: NextFunction)=>{
14+
15+
const token:any = req.header('auth-token');
16+
if(!token){
17+
res.status(401).send({error: "Token Validation Error!"})
18+
}
19+
20+
try{
21+
22+
const data = jwt.verify(token, JWT_SECRET) as UserPayload;
23+
merge(req, {user: data.user});
24+
25+
return next();
26+
}catch(error){
27+
res.status(401).send({error: "Token Validation Error!"})
28+
}
29+
}
30+
31+
export default fetchuser;

application/src/models/User.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Schema, model } from "mongoose";
2+
3+
const userSchema = new Schema({
4+
name: {
5+
type: String,
6+
required: true,
7+
trim: true,
8+
maxlength: 32,
9+
minlength: 3,
10+
},
11+
email: {
12+
type: String,
13+
required: true,
14+
trim: true,
15+
unique: true,
16+
lowercase: true,
17+
},
18+
avatar: String,
19+
points: { type: Number, default: 0 },
20+
createdAt: { type: Date, default: Date.now },
21+
batch: {
22+
type: String,
23+
required: true,
24+
trim: true,
25+
maxlength: 4,
26+
minlength: 4,
27+
},
28+
program: {
29+
type: String,
30+
required: true,
31+
trim: true,
32+
maxlength: 50,
33+
minlength: 3,
34+
},
35+
branch: {
36+
type: String,
37+
required: true,
38+
trim: true,
39+
maxlength: 50,
40+
minlength: 3,
41+
},
42+
gender: {
43+
type: String,
44+
required: true,
45+
trim: true,
46+
maxlength: 6,
47+
minlength: 1,
48+
},
49+
sectionCode: {
50+
type: String,
51+
required: true,
52+
trim: true,
53+
maxlength: 10,
54+
minlength: 1,
55+
},
56+
registrationNumber: {
57+
type: String,
58+
required: true,
59+
trim: true,
60+
maxlength: 12,
61+
minlength: 8,
62+
},
63+
dob: {
64+
type: String,
65+
required: true,
66+
trim: true,
67+
maxlength: 10,
68+
minlength: 10,
69+
},
70+
phone: {
71+
type: String,
72+
required: true,
73+
trim: true,
74+
maxlength: 10,
75+
minlength: 10,
76+
},
77+
password: {
78+
type: String,
79+
required: true,
80+
trim: true,
81+
maxlength: 255,
82+
minlength: 6,
83+
},
84+
});
85+
86+
const UserModel = model("User", userSchema);
87+
88+
export default UserModel;
89+
90+
export const findUserByEmail = (email: string) => UserModel.findOne({ email });
91+
export const findUserByReg = (registrationNumber: string) => UserModel.findOne({ registrationNumber });

application/src/routes/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Router } from "express";
2+
3+
const router = Router();
4+
5+
export default (): Router => {
6+
7+
return router;
8+
};

application/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"esModuleInterop": true,
5+
"target": "es6",
6+
"moduleResolution": "node",
7+
"sourceMap": true,
8+
"outDir": "dist"
9+
},
10+
"lib": ["es2015"]
11+
}

0 commit comments

Comments
 (0)