From 3f328e5316e53058178ccdd601cdb77bdccc0cf7 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Mon, 29 Apr 2024 21:26:04 +0200 Subject: [PATCH] Clean setup - nork v4, nothing else --- api/.gitignore | 3 ++ api/norkconfig.json | 1 + api/package.json | 61 +++++++++++++++++++++++++++++++++++ api/src/app.ts | 42 ++++++++++++++++++++++++ api/src/config/environment.ts | 4 +++ api/src/routes/index.ts | 15 +++++++++ api/src/server.ts | 26 +++++++++++++++ api/tsconfig.json | 15 +++++++++ 8 files changed, 167 insertions(+) create mode 100644 api/.gitignore create mode 100644 api/norkconfig.json create mode 100644 api/package.json create mode 100644 api/src/app.ts create mode 100644 api/src/config/environment.ts create mode 100644 api/src/routes/index.ts create mode 100644 api/src/server.ts create mode 100644 api/tsconfig.json diff --git a/api/.gitignore b/api/.gitignore new file mode 100644 index 0000000..1945131 --- /dev/null +++ b/api/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package-lock.json +.env diff --git a/api/norkconfig.json b/api/norkconfig.json new file mode 100644 index 0000000..b07785a --- /dev/null +++ b/api/norkconfig.json @@ -0,0 +1 @@ +{ "lang": "ts" } diff --git a/api/package.json b/api/package.json new file mode 100644 index 0000000..ec289fe --- /dev/null +++ b/api/package.json @@ -0,0 +1,61 @@ +{ + "name": "deguapp-api", + "version": "2.0.0", + "description": "DeguApp REST API", + "main": "dist/app.js", + "bin": "dist/app.js", + "types": "dist/app.d.ts", + "scripts": { + "start": "npm run start:prod", + "start:dev": "tsx watch src/server.ts", + "start:prod": "node dist/server.js", + "tsc": "tsc -p .", + "clean": "rimraf dist", + "copy-assets": "ts-node src/utils/copyAssets", + "build": "npm-run-all clean tsc copy-assets", + "test": "mocha --config .mocharc.json --watch src/**/*.test.ts", + "format": "npx prettier --write ." + }, + "author": "Filip Rojek", + "license": "MIT", + "dependencies": { + "colors": "1.4.0", + "dotenv": "^16.4.5", + "express": "^4.19.2", + "fs-extra": "^10.0.0", + "inquirer": "^8.1.2", + "morgan": "^1.10.0", + "pad": "^3.2.0", + "path": "^0.12.7" + }, + "devDependencies": { + "@types/chai": "^4.2.22", + "@types/express": "^4.17.21", + "@types/fs-extra": "^9.0.13", + "@types/inquirer": "^8.1.3", + "@types/mocha": "^9.0.0", + "@types/morgan": "^1.9.9", + "@types/shelljs": "^0.8.11", + "@typescript-eslint/eslint-plugin": "^5.5.0", + "@typescript-eslint/parser": "^5.5.0", + "chai": "^4.3.4", + "eslint": "^8.3.0", + "http": "^0.0.1-security", + "mocha": "^9.1.3", + "npm-run-all": "^4.1.5", + "prettier": "^2.7.1", + "rimraf": "^3.0.2", + "shelljs": "^0.8.5", + "ts-node": "^10.9.2", + "tsx": "^4.7.3", + "typescript": "^4.5.2" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/filiprojek/nork.git" + }, + "bugs": { + "url": "https://github.com/filiprojek/nork/issues" + }, + "homepage": "https://github.com/filiprojek/nork/blob/master/README.md" +} diff --git a/api/src/app.ts b/api/src/app.ts new file mode 100644 index 0000000..f14a697 --- /dev/null +++ b/api/src/app.ts @@ -0,0 +1,42 @@ +import express from "express"; +import morgan from "morgan"; +//import path from 'path' +//import cors from 'cors' +//import cookieParser from 'cookie-parser' +import { router as routes } from "./routes"; +//import { router as middlewares } from './middlewares' +//import env from './config/environment' + +//export let corsWhitelist: Array +//if (env.CORS_WHITELIST != 'undefined') { +// corsWhitelist = [...['http://localhost:8080', 'http://localhost:6040'], ...env.CORS_WHITELIST.split(';')] +//} else { +// corsWhitelist = ['http://localhost:8080', 'http://localhost:6040'] +//} +//const corsOptions = { +// origin: function (origin: any, callback: any) { +// if (!origin || corsWhitelist.indexOf(origin) !== -1) { +// callback(null, true) +// } else { +// callback(new Error('Not allowed by CORS')) +// } +// }, +// optionsSuccessStatus: 200, +// credentials: true +//} + +export const app = express(); + +// Middlewares +//app.use(middlewares) +//app.set('view engine', 'ejs') +//app.set('views', path.join(__dirname, 'views')) +//app.use(cors(corsOptions)) +app.use(morgan("dev")); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); +//app.use(express.static(path.join(__dirname, 'public'))) +//app.use(cookieParser()) + +// Routes +app.use(routes); diff --git a/api/src/config/environment.ts b/api/src/config/environment.ts new file mode 100644 index 0000000..07b2cac --- /dev/null +++ b/api/src/config/environment.ts @@ -0,0 +1,4 @@ +import path from "path"; +import { Env } from "nork"; +const env = Env.get(path.join(__dirname, "../.env")); +export default env; diff --git a/api/src/routes/index.ts b/api/src/routes/index.ts new file mode 100644 index 0000000..bf190c9 --- /dev/null +++ b/api/src/routes/index.ts @@ -0,0 +1,15 @@ +import { Request, Response, Router } from "express"; +import path from "path"; +//import authRoutes from "./authRoutes"; +export const router = Router(); + +//router.use("/api/auth", authRoutes); + +//router.get("*", (req: Request, res: Response) => { +// res.sendFile(path.join(__dirname, "../views/index.html")); +//}); + +// 404 +router.use((req: Request, res: Response) => { + res.status(404).send("Error 404\n"); +}); diff --git a/api/src/server.ts b/api/src/server.ts new file mode 100644 index 0000000..fbc1287 --- /dev/null +++ b/api/src/server.ts @@ -0,0 +1,26 @@ +import http from "http"; +import { app } from "./app"; +import env from "./config/environment"; +//const env = { +// APP_PORT: 8080, +// APP_HOSTNAME: "127.0.0.1", +//}; +import { Log } from "nork"; +//import database from './config/database' +const port: number = env.APP_PORT || 8080; +const hostname: string = env.APP_HOSTNAME || "localhost"; +export const server = http.createServer(app); + +// Server +export function runServer(): void { + server.listen(port, hostname, () => { + Log.info(200, `Server is listening on http://${hostname}:${port}`); + }); +} + +//if (!env.NORK.database) { +runServer(); +//} else { +// const db_connection = database() +// runServer() +//} diff --git a/api/tsconfig.json b/api/tsconfig.json new file mode 100644 index 0000000..3ec4ba1 --- /dev/null +++ b/api/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "es6" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + "module": "commonjs" /* Specify what module code is generated. */, + "outDir": "./dist" /* Specify an output folder for all emitted files. */, + "rootDir": "./src" /* Specify the root folder within your source files. */, + "strict": true /* Enable all strict type-checking options. */, + "baseUrl": "./" /* Base directory to resolve non-absolute module names. */, + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, + "skipLibCheck": true /* Skip type checking all .d.ts files. */, + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + "declaration": true + }, + "exclude": ["_old", "node_modules", "src/make-files", "src/skeletons", "src/tests", "src/interfaces", "src/utils"] +}