--
This commit is contained in:
parent
8fcc733093
commit
231adbe93f
123 changed files with 16683 additions and 62496 deletions
15
backend/.dockerignore
Normal file
15
backend/.dockerignore
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
node_modules
|
||||
npm-debug.log
|
||||
|
||||
.idea/
|
||||
.DS_Store
|
||||
.vscode
|
||||
|
||||
dist/
|
||||
debug.log
|
||||
|
||||
.env
|
||||
|
||||
.dockerignore
|
||||
.gitignore
|
||||
Dockerfile
|
||||
3
backend/.gitignore
vendored
Normal file
3
backend/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
dist
|
||||
.env
|
||||
27
backend/Dockerfile
Normal file
27
backend/Dockerfile
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
FROM node:14 AS builder
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm run build-ts
|
||||
|
||||
FROM node:14-alpine AS server
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
RUN chown -R node:node /usr/src/app
|
||||
|
||||
USER node
|
||||
EXPOSE 5000
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm install --production
|
||||
|
||||
COPY --from=0 /usr/src/app/dist dist
|
||||
|
||||
ENV NODE_ENV=production
|
||||
|
||||
CMD ["node", "dist/server.js"]
|
||||
1723
backend/package-lock.json
generated
Normal file
1723
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
50
backend/package.json
Normal file
50
backend/package.json
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
{
|
||||
"name": "geolosys-configurator-backend",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "npm run serve",
|
||||
"build": "npm run build-ts",
|
||||
"serve": "node dist/server.js",
|
||||
"watch-node": "nodemon dist/server.js",
|
||||
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"",
|
||||
"build-ts": "tsc",
|
||||
"watch-ts": "tsc -w",
|
||||
"serve-debug": "nodemon --inspect dist/server.js",
|
||||
"debug": "npm run build && npm run watch-debug",
|
||||
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve-debug\""
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/oitsjustjose/Blog.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/oitsjustjose/Blog/issues"
|
||||
},
|
||||
"homepage": "https://github.com/oitsjustjose/Blog#readme",
|
||||
"dependencies": {
|
||||
"@types/bcrypt": "^3.0.0",
|
||||
"@types/body-parser": "^1.19.0",
|
||||
"@types/cors": "^2.8.8",
|
||||
"@types/express": "^4.17.6",
|
||||
"@types/jsonwebtoken": "^8.5.0",
|
||||
"@types/mongoose": "^5.7.23",
|
||||
"@types/shortid": "0.0.29",
|
||||
"bcrypt": "^5.0.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"concurrently": "^5.3.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"moment": "^2.29.1",
|
||||
"mongoose": "^5.9.18",
|
||||
"shortid": "^2.2.15"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^3.9.5"
|
||||
}
|
||||
}
|
||||
35
backend/src/app.ts
Normal file
35
backend/src/app.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import * as bodyParser from 'body-parser'
|
||||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import { loginPostHandler } from './app/controllers/loginPostHandler'
|
||||
import { registerPostHandler } from './app/controllers/registerPostHandler'
|
||||
import { configGetHandler, configGetSpecificHandler } from './app/controllers/configGetHandler'
|
||||
import { configPutHandler } from './app/controllers/configPutHandler'
|
||||
import { UserModel } from './app/db/user.schema'
|
||||
import { auth } from './app/middleware'
|
||||
|
||||
interface AuthReq {
|
||||
user: UserModel | null
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request extends AuthReq { }
|
||||
}
|
||||
}
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(cors())
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}))
|
||||
|
||||
app.get('/api/configs', auth, configGetHandler)
|
||||
app.put('/api/configs', auth, configPutHandler)
|
||||
app.get('/api/configs/:id', configGetSpecificHandler)
|
||||
app.post('/api/users/login', loginPostHandler)
|
||||
app.post('/api/users/register', registerPostHandler)
|
||||
|
||||
export default app
|
||||
20
backend/src/app/controllers/configGetHandler.ts
Normal file
20
backend/src/app/controllers/configGetHandler.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { Request, Response } from 'express'
|
||||
import ConfigSchema from '../db/config.schema'
|
||||
|
||||
export const configGetHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
return res.json(req.user?.configs)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
|
||||
export const configGetSpecificHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
return res.json(await ConfigSchema.findById(req.params.id))
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
18
backend/src/app/controllers/configPutHandler.ts
Normal file
18
backend/src/app/controllers/configPutHandler.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { Request, Response } from 'express'
|
||||
import ConfigSchema from '../db/config.schema'
|
||||
|
||||
export const configPutHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const config = new ConfigSchema()
|
||||
config.json = req.body.json
|
||||
await config.save()
|
||||
|
||||
req.user?.configs.push(config._id)
|
||||
await req.user.save()
|
||||
|
||||
return res.status(200).send(config)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
31
backend/src/app/controllers/loginPostHandler.ts
Normal file
31
backend/src/app/controllers/loginPostHandler.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Request, Response } from 'express'
|
||||
import * as jwt from 'jsonwebtoken'
|
||||
import UserSchema, { UserModel } from '../db/user.schema'
|
||||
import moment from 'moment'
|
||||
|
||||
export const loginPostHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const user: UserModel | null = await UserSchema.findOne({
|
||||
username: req.body.username
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).send("User not found")
|
||||
}
|
||||
|
||||
if (!user.validPassword(req.body.password)) {
|
||||
return res.status(404).send("Password for User was Incorrect")
|
||||
}
|
||||
|
||||
const token = jwt.sign({
|
||||
_id: user._id,
|
||||
username: user.username,
|
||||
expiresAt: moment().add(1, 'day').toISOString()
|
||||
}, process.env.JWT_SECRET as string)
|
||||
|
||||
return res.status(200).send(token)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
34
backend/src/app/controllers/registerPostHandler.ts
Normal file
34
backend/src/app/controllers/registerPostHandler.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { Request, Response } from 'express'
|
||||
import UserSchema from '../db/user.schema'
|
||||
import * as jwt from 'jsonwebtoken'
|
||||
import moment from 'moment'
|
||||
|
||||
export const registerPostHandler = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const exUser = await UserSchema.findOne({
|
||||
'username': req.body.username
|
||||
})
|
||||
|
||||
if (exUser) {
|
||||
return res.status(409).send("USERNAME ALREADY EXISTS")
|
||||
}
|
||||
|
||||
const user = new UserSchema()
|
||||
user.username = req.body.username
|
||||
user.password = user.generateHash(req.body.password)
|
||||
user.configs = Array()
|
||||
await user.save()
|
||||
|
||||
// Log the user in
|
||||
const token = jwt.sign({
|
||||
_id: user._id,
|
||||
username: user.username,
|
||||
expiresAt: moment().add(1, 'day').toISOString()
|
||||
}, process.env.JWT_SECRET as string)
|
||||
|
||||
return res.status(200).send(token)
|
||||
} catch (ex) {
|
||||
console.error(ex)
|
||||
return res.status(500).send({ error: ex })
|
||||
}
|
||||
}
|
||||
17
backend/src/app/db/config.schema.ts
Normal file
17
backend/src/app/db/config.schema.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { Document, model, Schema, Types } from 'mongoose'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const ConfigSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
default: shortid.generate
|
||||
},
|
||||
json: Object
|
||||
})
|
||||
|
||||
export type ConfigModel = Document & {
|
||||
_id: string,
|
||||
json: object,
|
||||
}
|
||||
|
||||
export default model<ConfigModel>('configs', ConfigSchema, 'configs')
|
||||
35
backend/src/app/db/user.schema.ts
Normal file
35
backend/src/app/db/user.schema.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import bcrypt from 'bcrypt'
|
||||
import { Document, model, Schema, Types } from 'mongoose'
|
||||
|
||||
const UserSchema = new Schema({
|
||||
username: String,
|
||||
password: String,
|
||||
configs: [{
|
||||
type: String
|
||||
}]
|
||||
})
|
||||
|
||||
UserSchema.methods.generateHash = function (password: string) {
|
||||
return bcrypt.hashSync(password, bcrypt.genSaltSync(8))
|
||||
}
|
||||
|
||||
UserSchema.methods.validPassword = function (password: string) {
|
||||
return bcrypt.compareSync(password, String(this.password))
|
||||
}
|
||||
|
||||
UserSchema.methods.changePassword = function (password: string, newPassword: string) {
|
||||
if (bcrypt.compareSync(password, this.password)) {
|
||||
this.password = newPassword
|
||||
}
|
||||
}
|
||||
|
||||
export type UserModel = Document & {
|
||||
username: string,
|
||||
password: string,
|
||||
configs: string[],
|
||||
generateHash: (password: string) => string,
|
||||
validPassword: (password: string) => string,
|
||||
changePassword: (password: string, newPassword: string) => void
|
||||
}
|
||||
|
||||
export default model<UserModel>('users', UserSchema, 'users')
|
||||
32
backend/src/app/middleware.ts
Normal file
32
backend/src/app/middleware.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { NextFunction, Request, Response } from 'express'
|
||||
import * as jwt from 'jsonwebtoken'
|
||||
import UserSchema from './db/user.schema'
|
||||
import moment from 'moment'
|
||||
|
||||
export const auth = async (req: Request, res: Response, next: NextFunction) => {
|
||||
const token = req.header('auth-token')
|
||||
|
||||
if (!token) {
|
||||
return res.status(403).send('Access Denied')
|
||||
}
|
||||
|
||||
try {
|
||||
const { _id, expiresAt } = (jwt.verify(token, process.env.JWT_SECRET as string) as { _id: string, expiresAt: string })
|
||||
|
||||
if (moment(moment()).isAfter(expiresAt)) {
|
||||
return res.status(400).send('Token expired')
|
||||
}
|
||||
|
||||
const user = await UserSchema.findById(_id)
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).send("User not found")
|
||||
} else {
|
||||
req.user = user
|
||||
}
|
||||
|
||||
next()
|
||||
} catch (err) {
|
||||
return res.status(400).send(`Invalid Token: ${err}`)
|
||||
}
|
||||
}
|
||||
8
backend/src/db.ts
Normal file
8
backend/src/db.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import mongoose from 'mongoose'
|
||||
|
||||
export default async () => {
|
||||
await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Blog`, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true
|
||||
})
|
||||
}
|
||||
13
backend/src/server.ts
Normal file
13
backend/src/server.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { config as LoadDotenv } from 'dotenv'
|
||||
import app from './app'
|
||||
import connectDB from './db'
|
||||
|
||||
if(process.env.NODE_ENV !== "production") {
|
||||
LoadDotenv()
|
||||
}
|
||||
|
||||
connectDB()
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.listen(port, () => console.log(`Listening at http://127.0.0.1:${port}`))
|
||||
29
backend/tsconfig.json
Normal file
29
backend/tsconfig.json
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "es2017",
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"*": [
|
||||
"node_modules/*"
|
||||
]
|
||||
},
|
||||
"lib": [
|
||||
"es2019"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"src/types.d.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"test/**/*"
|
||||
]
|
||||
}
|
||||
Loading…
Reference in a new issue