mirror of
https://github.com/filiprojek/nork.git
synced 2024-11-22 16:51:03 +01:00
nork 3.0.4
This commit is contained in:
parent
100eb59f17
commit
95e60bc04e
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nork",
|
||||
"version": "3.0.3",
|
||||
"version": "3.0.4",
|
||||
"description": "The best node.js 'framework' :)",
|
||||
"main": "dist/app.js",
|
||||
"bin": "dist/app.js",
|
||||
@ -25,7 +25,7 @@
|
||||
"author": "Filip Rojek",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"colors":"1.4.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
"inquirer": "^8.1.2",
|
||||
"pad": "^3.2.0"
|
||||
|
20
src/skeletons/express-ts/.prettierrc
Normal file
20
src/skeletons/express-ts/.prettierrc
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"arrowParens": "avoid",
|
||||
"bracketSpacing": true,
|
||||
"endOfLine": "lf",
|
||||
"htmlWhitespaceSensitivity": "css",
|
||||
"insertPragma": false,
|
||||
"jsxBracketSameLine": true,
|
||||
"jsxSingleQuote": true,
|
||||
"printWidth": 200,
|
||||
"proseWrap": "preserve",
|
||||
"quoteProps": "as-needed",
|
||||
"requirePragma": false,
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 4,
|
||||
"trailingComma": "all",
|
||||
"useTabs": true,
|
||||
"vueIndentScriptAndStyle": true,
|
||||
"parser": "typescript"
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
{
|
||||
"lang": "ts"
|
||||
"lang": "ts",
|
||||
"db": ""
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node -r tsconfig-paths/register -r ts-node/register dist/server.js",
|
||||
"start:dev": "npx nodemon src/server.ts",
|
||||
"dev": "nodemon src/server.ts",
|
||||
"test": "jest",
|
||||
"clean": "rimraf dist/*",
|
||||
@ -16,15 +17,20 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"colors": "1.4.0",
|
||||
"cookie-parser": "^1.4.5",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"ejs": "^3.1.6",
|
||||
"express": "^4.17.1",
|
||||
"express-validator": "^6.14.0",
|
||||
"fs-extra": "^10.0.0",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"mongoose": "^5.12.3",
|
||||
"morgan": "^1.10.0"
|
||||
"morgan": "^1.10.0",
|
||||
"pg": "^8.7.1",
|
||||
"pg-hstore": "^2.3.4",
|
||||
"sequelize": "^6.15.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/cookie-parser": "^1.4.2",
|
||||
@ -33,6 +39,7 @@
|
||||
"@types/express": "^4.17.11",
|
||||
"@types/fs-extra": "^9.0.12",
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/jsonwebtoken": "^8.5.8",
|
||||
"@types/mongoose": "^5.10.5",
|
||||
"@types/morgan": "^1.9.2",
|
||||
"@types/node": "^14.14.41",
|
||||
@ -59,4 +66,4 @@
|
||||
"exec": "node -r tsconfig-paths/register -r ts-node/register ./src/server.ts",
|
||||
"ext": "ts, js"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,17 @@
|
||||
APP_PORT = 8080
|
||||
# General
|
||||
APP_PORT = 6060
|
||||
APP_HOSTNAME = 'localhost'
|
||||
APP_HOST = 'http://localhost:8080' # frontend url
|
||||
CORS_WHITELIST = http://172.15.46.21:8080;http://192.168.0.1:8080
|
||||
|
||||
JWT_SECRET = ''
|
||||
|
||||
# MongoDB
|
||||
DB_URI = 'mongodb://username:password@localhost:27017/database?authSource=admin'
|
||||
|
||||
# PostgreSQL
|
||||
DB_PORT = 5432
|
||||
DB_HOST = '127.0.0.1'
|
||||
DB_USERNAME = ''
|
||||
DB_PASSWORD = ''
|
||||
DB_DATABASE = ''
|
49
src/skeletons/express-ts/src/config/database.ts
Normal file
49
src/skeletons/express-ts/src/config/database.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import mongoose from 'mongoose'
|
||||
import config from '@/utils/environment'
|
||||
import { Err, Succ } from '@/services/globalService'
|
||||
import db from '@/config/postgres.config'
|
||||
|
||||
// MongoDB
|
||||
const dbURI: string = config.DB_URI
|
||||
function connect() {
|
||||
if (!config.NORK.db) {
|
||||
new Err(500, 'no database is in norkcfg.json')
|
||||
return false
|
||||
}
|
||||
|
||||
if (config.NORK.db == 'mongodb') {
|
||||
mongoose
|
||||
.connect(dbURI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useCreateIndex: true,
|
||||
})
|
||||
.then(() => {
|
||||
new Succ(200, 'connected to db')
|
||||
return true
|
||||
})
|
||||
.catch((err: any) => {
|
||||
new Err(500, err)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
if (config.NORK.db == 'postgresql') {
|
||||
db.sync()
|
||||
.then(() => {
|
||||
new Succ(200, 'connected to db')
|
||||
return true
|
||||
})
|
||||
.catch((err: any) => {
|
||||
new Err(500, `Can't connect to db\n${err}`)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
if (config.NORK.db.length > 0) {
|
||||
new Err(500, `unsupported database ${config.NORK.db}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export default connect
|
10
src/skeletons/express-ts/src/config/postgres.config.ts
Normal file
10
src/skeletons/express-ts/src/config/postgres.config.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Sequelize } from 'sequelize'
|
||||
import config from '@/utils/environment'
|
||||
|
||||
const db = new Sequelize(config.DB_DATABASE, config.DB_USERNAME, config.DB_PASSWORD, {
|
||||
host: config.DB_HOST,
|
||||
dialect: 'postgres',
|
||||
logging: false,
|
||||
})
|
||||
|
||||
export default db
|
32
src/skeletons/express-ts/src/middlewares/authMiddleware.ts
Normal file
32
src/skeletons/express-ts/src/middlewares/authMiddleware.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { Request, Response, NextFunction } from 'express'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import env from '@/utils/environment'
|
||||
import { Err, Succ } from '@/services/globalService'
|
||||
// import User from '@/models/User' // uncomment this
|
||||
|
||||
export const requireAuth = (req: Request, res: Response, next: NextFunction) => {
|
||||
const token = req.cookies.jwt
|
||||
new Err(500, 'uncomment code in authMiddleware before using!')
|
||||
/* if (token) {
|
||||
jwt.verify(token, env.JWT_SECRET, async (err: any, decodedToken: any) => {
|
||||
if (err) {
|
||||
// console.error(err.message)
|
||||
res.status(401).send(new Err(401, 'user is not authenticated'))
|
||||
}
|
||||
if (!err) {
|
||||
const user = await User.findByPk(decodedToken.id)
|
||||
if (user === null) {
|
||||
res.status(401).send(new Err(401, 'user is not authenticated'))
|
||||
return
|
||||
}
|
||||
res.locals.user = user
|
||||
new Succ(100, 'user is authenticated')
|
||||
next()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
res.status(401).send(new Err(401, 'user is not authenticated'))
|
||||
} */
|
||||
}
|
13
src/skeletons/express-ts/src/middlewares/handleValidation.ts
Normal file
13
src/skeletons/express-ts/src/middlewares/handleValidation.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Request, Response, NextFunction } from 'express'
|
||||
import { validationResult } from 'express-validator'
|
||||
|
||||
class Middleware {
|
||||
handleValidationError(req: Request, res: Response, next: NextFunction) {
|
||||
const error = validationResult(req)
|
||||
if (!error.isEmpty()) {
|
||||
return res.status(400).json(error.array()[0])
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
export default new Middleware()
|
@ -1,6 +1,9 @@
|
||||
import { Router } from 'express'
|
||||
import * as rootController from '@/controllers/rootController'
|
||||
import rootValidator from '@/validators/rootValidator'
|
||||
import handleValidation from '@/middlewares/handleValidation'
|
||||
|
||||
export const router = Router()
|
||||
const mws = [handleValidation.handleValidationError]
|
||||
|
||||
router.get('/', rootController.root_get)
|
||||
router.get('/', rootValidator.checkRootGet(), mws, rootController.root_get)
|
||||
|
@ -1,24 +1,24 @@
|
||||
import mongoose from 'mongoose'
|
||||
import http from 'http'
|
||||
import { app } from '@/app'
|
||||
import config from '@/utils/environment'
|
||||
import { Err, Succ } from '@/services/globalService'
|
||||
import { Succ } from '@/services/globalService'
|
||||
import database from '@/config/database'
|
||||
const port: number = config.APP_PORT || 8080
|
||||
const hostname: string = config.APP_HOSTNAME || 'localhost'
|
||||
const server = http.createServer(app)
|
||||
|
||||
const port: Number = config.APP_PORT || 8080
|
||||
// Server
|
||||
export function runServer(): void {
|
||||
server.listen(port, hostname, () => {
|
||||
new Succ(200, `Server is listening on http://localhost:${port}`)
|
||||
})
|
||||
}
|
||||
|
||||
// MongoDB
|
||||
const dbURI: string = config.DB_URI
|
||||
mongoose
|
||||
.connect(dbURI, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
useCreateIndex: true,
|
||||
})
|
||||
.then(() => {
|
||||
new Succ(200, 'connected to db')
|
||||
app.listen(port, () => {
|
||||
new Succ(200, `Server is listening on http://localhost:${port}`)
|
||||
})
|
||||
})
|
||||
.catch((err: any) => {
|
||||
new Err(500, err)
|
||||
})
|
||||
if (!config.NORK.db) {
|
||||
runServer()
|
||||
} else {
|
||||
const db_connection = database()
|
||||
if (db_connection) {
|
||||
runServer()
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,40 @@
|
||||
import path from 'path'
|
||||
require('dotenv').config({ path: path.join(__dirname, '../.env') })
|
||||
import fs from 'fs-extra'
|
||||
import { Err } from '@/services/globalService'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
dotenv.config({ path: path.join(__dirname, '../.env') })
|
||||
const norkcfg = fs.readJSONSync(path.join(__dirname, '../../norkconfig.json'))
|
||||
|
||||
if (norkcfg.db) {
|
||||
if (norkcfg.db == 'postgresql') {
|
||||
if (!process.env.DB_PORT) {
|
||||
process.env.DB_PORT = '5432'
|
||||
}
|
||||
if (!process.env.DB_HOST) {
|
||||
process.env.DB_HOST = '127.0.0.1'
|
||||
}
|
||||
if (!process.env.DB_USERNAME || !process.env.DB_PASSWORD || !process.env.DB_DATABASE) {
|
||||
new Err(500, 'missing DB parameters in .env file')
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
// General
|
||||
APP_PORT: Number(process.env.APP_PORT),
|
||||
APP_HOST: String(process.env.APP_HOST),
|
||||
APP_HOSTNAME: process.env.APP_HOSTNAME !== undefined ? String(process.env.APP_HOSTNAME) : null,
|
||||
CORS_WHITELIST: String(process.env.CORS_WHITELIST),
|
||||
JWT_SECRET: String(process.env.JWT_SECRET),
|
||||
// MongoDB
|
||||
DB_URI: String(process.env.DB_URI),
|
||||
// PostgreSQL
|
||||
DB_PORT: Number(process.env.DB_PORT),
|
||||
DB_HOST: String(process.env.DB_HOST),
|
||||
DB_USERNAME: String(process.env.DB_USERNAME),
|
||||
DB_PASSWORD: String(process.env.DB_PASSWORD),
|
||||
DB_DATABASE: String(process.env.DB_DATABASE),
|
||||
NORK: norkcfg,
|
||||
}
|
||||
|
9
src/skeletons/express-ts/src/validators/rootValidator.ts
Normal file
9
src/skeletons/express-ts/src/validators/rootValidator.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { body, param, query } from 'express-validator'
|
||||
|
||||
class rootValidator {
|
||||
checkRootGet() {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export default new rootValidator()
|
Loading…
Reference in New Issue
Block a user