mirror of
				https://github.com/okiba-org/backend.git
				synced 2025-10-31 16:57:14 +00:00 
			
		
		
		
	[chore] remove chalk for coloring text & minor changes
Signed-off-by: alok8bb <alok8bb@gmail.com>
This commit is contained in:
		| @@ -1,20 +1,27 @@ | |||||||
| # Okiba | # Okiba | ||||||
| A simple paste bin written in nodejs with postgresql.   |  | ||||||
|  | A simple and minimal pastebin written in Node.JS. | ||||||
|  |  | ||||||
| ## Usage | ## Usage | ||||||
|  |  | ||||||
| Before starting the server, we populate the database by creating a table for endpoints and providing the words to be used. The server will be populated from `words.txt` file from project root. | Before starting the server, we populate the database by creating a table for endpoints and providing the words to be used. The server will be populated from `words.txt` file from project root. | ||||||
|  | https://github.com/dwyl/english-words/blob/master/words_alpha.txt | ||||||
|  |  | ||||||
| ### Environment Variables | ### Environment Variables | ||||||
|  |  | ||||||
| Create a `.env` file at the project root and add the following variables according to the database. | Create a `.env` file at the project root and add the following variables according to the database. | ||||||
|  |  | ||||||
| ```shell | ```shell | ||||||
| PORT = 8080 | PORT = 8080 | ||||||
| PGUSER = alok-pg | PGUSER = alok-pg | ||||||
| PGHOST = localhost | PGHOST = localhost | ||||||
| PGDATABASE = okiba | PGDATABASE = okiba | ||||||
| PGPORT = 5432 | PGPORT = 5432 | ||||||
|  | PGPASSWORD = postgres_user_password | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| ### Start scripts | ### Start scripts | ||||||
|  |  | ||||||
| ```shell | ```shell | ||||||
| // Development Server | // Development Server | ||||||
| $ pnpm run dev | $ pnpm run dev | ||||||
|   | |||||||
| @@ -20,7 +20,6 @@ | |||||||
|         "typescript": "^4.7.4" |         "typescript": "^4.7.4" | ||||||
|     }, |     }, | ||||||
|     "dependencies": { |     "dependencies": { | ||||||
|         "chalk": "^4.1.2", |  | ||||||
|         "cors": "^2.8.5", |         "cors": "^2.8.5", | ||||||
|         "dotenv": "^16.0.1", |         "dotenv": "^16.0.1", | ||||||
|         "express": "^4.18.1", |         "express": "^4.18.1", | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										40
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							| @@ -5,7 +5,6 @@ specifiers: | |||||||
|   '@types/express': ^4.17.13 |   '@types/express': ^4.17.13 | ||||||
|   '@types/node': ^18.0.3 |   '@types/node': ^18.0.3 | ||||||
|   '@types/pg': ^8.6.5 |   '@types/pg': ^8.6.5 | ||||||
|   chalk: ^4.1.2 |  | ||||||
|   cors: ^2.8.5 |   cors: ^2.8.5 | ||||||
|   dotenv: ^16.0.1 |   dotenv: ^16.0.1 | ||||||
|   express: ^4.18.1 |   express: ^4.18.1 | ||||||
| @@ -14,7 +13,6 @@ specifiers: | |||||||
|   typescript: ^4.7.4 |   typescript: ^4.7.4 | ||||||
|  |  | ||||||
| dependencies: | dependencies: | ||||||
|   chalk: 4.1.2 |  | ||||||
|   cors: 2.8.5 |   cors: 2.8.5 | ||||||
|   dotenv: 16.0.1 |   dotenv: 16.0.1 | ||||||
|   express: 4.18.1 |   express: 4.18.1 | ||||||
| @@ -161,13 +159,6 @@ packages: | |||||||
|     hasBin: true |     hasBin: true | ||||||
|     dev: true |     dev: true | ||||||
|  |  | ||||||
|   /ansi-styles/4.3.0: |  | ||||||
|     resolution: {integrity: sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==} |  | ||||||
|     engines: {node: '>=8'} |  | ||||||
|     dependencies: |  | ||||||
|       color-convert: 2.0.1 |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /anymatch/3.1.2: |   /anymatch/3.1.2: | ||||||
|     resolution: {integrity: sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==} |     resolution: {integrity: sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==} | ||||||
|     engines: {node: '>= 8'} |     engines: {node: '>= 8'} | ||||||
| @@ -248,14 +239,6 @@ packages: | |||||||
|       get-intrinsic: 1.1.2 |       get-intrinsic: 1.1.2 | ||||||
|     dev: false |     dev: false | ||||||
|  |  | ||||||
|   /chalk/4.1.2: |  | ||||||
|     resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} |  | ||||||
|     engines: {node: '>=10'} |  | ||||||
|     dependencies: |  | ||||||
|       ansi-styles: 4.3.0 |  | ||||||
|       supports-color: 7.2.0 |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /chokidar/3.5.3: |   /chokidar/3.5.3: | ||||||
|     resolution: {integrity: sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==} |     resolution: {integrity: sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==} | ||||||
|     engines: {node: '>= 8.10.0'} |     engines: {node: '>= 8.10.0'} | ||||||
| @@ -271,17 +254,6 @@ packages: | |||||||
|       fsevents: 2.3.2 |       fsevents: 2.3.2 | ||||||
|     dev: true |     dev: true | ||||||
|  |  | ||||||
|   /color-convert/2.0.1: |  | ||||||
|     resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==} |  | ||||||
|     engines: {node: '>=7.0.0'} |  | ||||||
|     dependencies: |  | ||||||
|       color-name: 1.1.4 |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /color-name/1.1.4: |  | ||||||
|     resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /concat-map/0.0.1: |   /concat-map/0.0.1: | ||||||
|     resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} |     resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} | ||||||
|     dev: true |     dev: true | ||||||
| @@ -486,11 +458,6 @@ packages: | |||||||
|       path-is-absolute: 1.0.1 |       path-is-absolute: 1.0.1 | ||||||
|     dev: true |     dev: true | ||||||
|  |  | ||||||
|   /has-flag/4.0.0: |  | ||||||
|     resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} |  | ||||||
|     engines: {node: '>=8'} |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /has-symbols/1.0.3: |   /has-symbols/1.0.3: | ||||||
|     resolution: {integrity: sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==} |     resolution: {integrity: sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==} | ||||||
|     engines: {node: '>= 0.4'} |     engines: {node: '>= 0.4'} | ||||||
| @@ -893,13 +860,6 @@ packages: | |||||||
|     engines: {node: '>=0.10.0'} |     engines: {node: '>=0.10.0'} | ||||||
|     dev: true |     dev: true | ||||||
|  |  | ||||||
|   /supports-color/7.2.0: |  | ||||||
|     resolution: {integrity: sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==} |  | ||||||
|     engines: {node: '>=8'} |  | ||||||
|     dependencies: |  | ||||||
|       has-flag: 4.0.0 |  | ||||||
|     dev: false |  | ||||||
|  |  | ||||||
|   /supports-preserve-symlinks-flag/1.0.0: |   /supports-preserve-symlinks-flag/1.0.0: | ||||||
|     resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==} |     resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==} | ||||||
|     engines: {node: '>= 0.4'} |     engines: {node: '>= 0.4'} | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import { logError, logSuccess, projectRoot } from "../utils"; | import { logError, logInfo, projectRoot } from "../utils"; | ||||||
| import fs from "fs"; | import fs from "fs"; | ||||||
| import path from "path"; | import path from "path"; | ||||||
| import { Client } from "pg"; | import { Client } from "pg"; | ||||||
| @@ -65,5 +65,5 @@ export const populateDB = async (db: Client) => { | |||||||
|         await db.query(queryStr); |         await db.query(queryStr); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     logSuccess("Successfully populated the database!"); |     logInfo("Successfully populated the database!"); | ||||||
| }; | }; | ||||||
|   | |||||||
							
								
								
									
										17
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.ts
									
									
									
									
									
								
							| @@ -2,7 +2,7 @@ import express, { Response, Request, Express } from "express"; | |||||||
| import dotenv from "dotenv"; | import dotenv from "dotenv"; | ||||||
| import BinRouter from "./routes/bin"; | import BinRouter from "./routes/bin"; | ||||||
| import { Client, Pool } from "pg"; | import { Client, Pool } from "pg"; | ||||||
| import { createDataDir, errorHandler, logError, logSuccess } from "./utils"; | import { createDataDir, errorHandler, logError, logInfo } from "./utils"; | ||||||
| import { tableExists, populateDB } from "./db"; | import { tableExists, populateDB } from "./db"; | ||||||
| import cors from "cors"; | import cors from "cors"; | ||||||
|  |  | ||||||
| @@ -12,15 +12,20 @@ const main = async () => { | |||||||
|  |  | ||||||
|     // init db |     // init db | ||||||
|     const pool = new Pool(); |     const pool = new Pool(); | ||||||
|     const client = new Client(); |     const client = new Client({ | ||||||
|  |         user: process.env.PGUSER, | ||||||
|  |         password: process.env.PGPASSWORD, | ||||||
|  |         port: Number(process.env.PGPORT), | ||||||
|  |         database: process.env.PGDATABASE, | ||||||
|  |     }); | ||||||
|  |  | ||||||
|     client |     client | ||||||
|         .connect() |         .connect() | ||||||
|         .then(() => { |         .then(() => { | ||||||
|             logSuccess("Connected to database!"); |             logInfo("Connected to database!"); | ||||||
|         }) |         }) | ||||||
|         .catch((err: Error) => { |         .catch((err: Error) => { | ||||||
|             logError(`Could not connect to database! \n\n${err.message}`); |             logError(`Could not connect to database!\n${err.message}`); | ||||||
|             throw err; |             throw err; | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
| @@ -40,14 +45,14 @@ const main = async () => { | |||||||
|     // check more on this later |     // check more on this later | ||||||
|     app.use(cors()); |     app.use(cors()); | ||||||
|  |  | ||||||
|     app.get("/", (req: Request, res: Response) => { |     app.get("/", (_: Request, res: Response) => { | ||||||
|         res.send("Hello, World!"); |         res.send("Hello, World!"); | ||||||
|     }); |     }); | ||||||
|     app.use("/bin/", BinRouter(pool)); |     app.use("/bin/", BinRouter(pool)); | ||||||
|  |  | ||||||
|     app.use(errorHandler); |     app.use(errorHandler); | ||||||
|     app.listen(port, () => { |     app.listen(port, () => { | ||||||
|         logSuccess(`Server started on port ${port}`); |         logInfo(`Server started on port ${port}`); | ||||||
|     }); |     }); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,13 +1,12 @@ | |||||||
| import chalk from "chalk"; |  | ||||||
| import { ErrorRequestHandler } from "express"; | import { ErrorRequestHandler } from "express"; | ||||||
| import path from "path"; | import path from "path"; | ||||||
| import fs from "fs"; | import fs from "fs"; | ||||||
| import fsp from "fs/promises"; | import fsp from "fs/promises"; | ||||||
|  |  | ||||||
| // logs | // logs | ||||||
| export const logError = (msg: string) => console.error(chalk.bold.red(msg)); | export const logInfo = (msg: any) => console.info(`\x1b[36m%s\x1b[0m`, `[INFO] ${msg}`); | ||||||
| export const logSuccess = (msg: string) => console.log(chalk.bold.green(msg)); | export const logError = (msg: any) => console.error(`\x1b[31m`, `[ERR] ${msg}`); | ||||||
| export const logWarning = (msg: string) => console.log(chalk.bold.yellow(msg)); | export const logWarn = (msg: any) => console.warn(`\x1b[33m`, `[WARN] ${msg}`); | ||||||
|  |  | ||||||
| export const projectRoot = path.join(__dirname, "..", ".."); | export const projectRoot = path.join(__dirname, "..", ".."); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user