File tree Expand file tree Collapse file tree 9 files changed +56
-30
lines changed Expand file tree Collapse file tree 9 files changed +56
-30
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "singleQuote": true,
3+ "trailingComma": "all",
4+ "overrides": [
5+ {
6+ "files": ["**/.*rc", "**/*.json"],
7+ "options": { "parser": "json" }
8+ }
9+ ]
10+ }
Original file line number Diff line number Diff line change 1+ module . exports = {
2+ transport : "http" , // ws | http
3+ apiPort : 8001 ,
4+ staticPort : 8000 ,
5+ staticPath : "./static" ,
6+ logPath : "./log" ,
7+ db : {
8+ host : "127.0.0.1" ,
9+ port : 5432 ,
10+ database : "example" ,
11+ user : "marcus" ,
12+ password : "marcus" ,
13+ } ,
14+ } ;
Original file line number Diff line number Diff line change 22
33const pg = require ( 'pg' ) ;
44
5- const pool = new pg . Pool ( {
6- host : '127.0.0.1' ,
7- port : 5432 ,
8- database : 'example' ,
9- user : 'marcus' ,
10- password : 'marcus' ,
11- } ) ;
12-
13- module . exports = ( table ) => ( {
5+ const createDBCrud = ( pool ) => ( table ) => ( {
146 async query ( sql , args ) {
157 return await pool . query ( sql , args ) ;
168 } ,
@@ -57,3 +49,5 @@ module.exports = (table) => ({
5749 return await pool . query ( sql , [ id ] ) ;
5850 } ,
5951} ) ;
52+
53+ module . exports = ( poolOptions ) => createDBCrud ( new pg . Pool ( poolOptions ) ) ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ const receiveArgs = async (req) => {
99 return JSON . parse ( data ) ;
1010} ;
1111
12- module . exports = ( routing , port ) => {
12+ module . exports = ( routing , port , console ) => {
1313 http . createServer ( async ( req , res ) => {
1414 const { url, socket } = req ;
1515 const [ name , method , id ] = url . substring ( 1 ) . split ( '/' ) ;
Original file line number Diff line number Diff line change @@ -68,4 +68,4 @@ class Logger {
6868 }
6969}
7070
71- module . exports = new Logger ( './log' ) ;
71+ module . exports = Logger ;
Original file line number Diff line number Diff line change 22
33const fsp = require ( 'node:fs' ) . promises ;
44const path = require ( 'node:path' ) ;
5- const server = require ( './ws.js' ) ;
65const staticServer = require ( './static.js' ) ;
76const load = require ( './load.js' ) ;
8- const db = require ( './db.js' ) ;
7+ const createDbQueryConstructor = require ( './db.js' ) ;
98const hash = require ( './hash.js' ) ;
10- const logger = require ( './logger.js' ) ;
9+ const Logger = require ( './logger.js' ) ;
10+ const config = require ( './config.js' ) ;
11+ const server = require ( `./${ config . transport } .js` ) ;
12+
13+ const logger = new Logger ( config . logPath ) ;
14+ const db = createDbQueryConstructor ( config . db ) ;
1115
1216const sandbox = {
1317 console : Object . freeze ( logger ) ,
@@ -26,6 +30,6 @@ const routing = {};
2630 routing [ serviceName ] = await load ( filePath , sandbox ) ;
2731 }
2832
29- staticServer ( './static' , 8000 ) ;
30- server ( routing , 8001 ) ;
33+ staticServer ( config . staticPath , config . staticPort , logger ) ;
34+ server ( routing , config . apiPort , logger ) ;
3135} ) ( ) ;
Original file line number Diff line number Diff line change 88 "engines" : {
99 "node" : " 14 || 16 || 18"
1010 },
11+ "scripts" : {
12+ "start" : " node --experimental-vm-modules main.js"
13+ },
1114 "dependencies" : {
1215 "pg" : " ^8.8.0" ,
1316 "ws" : " ^8.12.0"
Original file line number Diff line number Diff line change @@ -4,18 +4,20 @@ const http = require('node:http');
44const path = require ( 'node:path' ) ;
55const fs = require ( 'node:fs' ) ;
66
7- module . exports = ( root , port ) => {
8- http . createServer ( async ( req , res ) => {
9- const url = req . url === '/' ? '/index.html' : req . url ;
10- const filePath = path . join ( root , url ) ;
11- try {
12- const data = await fs . promises . readFile ( filePath ) ;
13- res . end ( data ) ;
14- } catch ( err ) {
15- res . statusCode = 404 ;
16- res . end ( '"File is not found"' ) ;
17- }
18- } ) . listen ( port ) ;
7+ module . exports = ( root , port , console ) => {
8+ http
9+ . createServer ( async ( req , res ) => {
10+ const url = req . url === '/' ? '/index.html' : req . url ;
11+ const filePath = path . join ( root , url ) ;
12+ try {
13+ const data = await fs . promises . readFile ( filePath ) ;
14+ res . end ( data ) ;
15+ } catch ( err ) {
16+ res . statusCode = 404 ;
17+ res . end ( '"File is not found"' ) ;
18+ }
19+ } )
20+ . listen ( port ) ;
1921
2022 console . log ( `Static on port ${ port } ` ) ;
2123} ;
Original file line number Diff line number Diff line change 11'use strict' ;
22
3- const console = require ( './logger.js' ) ;
43const { Server } = require ( 'ws' ) ;
54
6- module . exports = ( routing , port ) => {
5+ module . exports = ( routing , port , console ) => {
76 const ws = new Server ( { port } ) ;
87
98 ws . on ( 'connection' , ( connection , req ) => {
You can’t perform that action at this time.
0 commit comments