File tree 7 files changed +30
-27
lines changed
7 files changed +30
-27
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const pg = require ( 'pg' ) ;
4
- const config = require ( './config.js' ) ;
5
4
6
- const pool = new pg . Pool ( config . db ) ;
7
-
8
- module . exports = ( table ) => ( {
5
+ const createDBCrud = ( pool ) => ( table ) => ( {
9
6
async query ( sql , args ) {
10
7
return await pool . query ( sql , args ) ;
11
8
} ,
@@ -52,3 +49,5 @@ module.exports = (table) => ({
52
49
return await pool . query ( sql , [ id ] ) ;
53
50
} ,
54
51
} ) ;
52
+
53
+ module . exports = ( poolOptions ) => createDBCrud ( new pg . Pool ( poolOptions ) ) ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const http = require ( 'node:http' ) ;
4
- const console = require ( './logger.js' ) ;
5
4
6
5
const answerNotFound = ( res , headers ) => {
7
6
res . writeHead ( 404 , headers ) ;
@@ -15,7 +14,7 @@ const receiveArgs = async (req) => {
15
14
return JSON . parse ( data ) ;
16
15
} ;
17
16
18
- module . exports = ( routing , port ) => {
17
+ module . exports = ( routing , port , console ) => {
19
18
http
20
19
. createServer ( async ( req , res ) => {
21
20
const headers = {
Original file line number Diff line number Diff line change 3
3
const fs = require ( 'node:fs' ) ;
4
4
const util = require ( 'node:util' ) ;
5
5
const path = require ( 'node:path' ) ;
6
- const config = require ( './config.js' ) ;
7
6
8
7
const COLORS = {
9
8
info : '\x1b[1;37m' ,
@@ -69,4 +68,4 @@ class Logger {
69
68
}
70
69
}
71
70
72
- module . exports = new Logger ( config . logPath ) ;
71
+ module . exports = Logger ;
Original file line number Diff line number Diff line change @@ -4,12 +4,15 @@ const fsp = require('node:fs').promises;
4
4
const path = require ( 'node:path' ) ;
5
5
const staticServer = require ( './static.js' ) ;
6
6
const load = require ( './load.js' ) ;
7
- const db = require ( './db.js' ) ;
7
+ const createDbQueryConstructor = require ( './db.js' ) ;
8
8
const hash = require ( './hash.js' ) ;
9
- const logger = require ( './logger.js' ) ;
9
+ const Logger = require ( './logger.js' ) ;
10
10
const config = require ( './config.js' ) ;
11
11
const server = require ( `./${ config . transport } .js` ) ;
12
12
13
+ const logger = new Logger ( config . logPath ) ;
14
+ const db = createDbQueryConstructor ( config . db ) ;
15
+
13
16
const sandbox = {
14
17
console : Object . freeze ( logger ) ,
15
18
db : Object . freeze ( db ) ,
@@ -27,6 +30,6 @@ const routing = {};
27
30
routing [ serviceName ] = await load ( filePath , sandbox ) ;
28
31
}
29
32
30
- staticServer ( config . staticPath , config . staticPort ) ;
31
- server ( routing , config . apiPort ) ;
33
+ staticServer ( config . staticPath , config . staticPort , logger ) ;
34
+ server ( routing , config . apiPort , logger ) ;
32
35
} ) ( ) ;
Original file line number Diff line number Diff line change 8
8
"engines" : {
9
9
"node" : " 14 || 16 || 18"
10
10
},
11
+ "scripts" : {
12
+ "start" : " node --experimental-vm-modules main.js"
13
+ },
11
14
"dependencies" : {
12
15
"pg" : " ^8.8.0" ,
13
16
"ws" : " ^8.12.0"
Original file line number Diff line number Diff line change 3
3
const http = require ( 'node:http' ) ;
4
4
const path = require ( 'node:path' ) ;
5
5
const fs = require ( 'node:fs' ) ;
6
- const console = require ( './logger.js' ) ;
7
6
8
- module . exports = ( root , port ) => {
9
- http . 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
- } ) . 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 ) ;
20
21
21
22
console . log ( `Static on port ${ port } ` ) ;
22
23
} ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const console = require ( './logger.js' ) ;
4
3
const { Server } = require ( 'ws' ) ;
5
4
6
- module . exports = ( routing , port ) => {
5
+ module . exports = ( routing , port , console ) => {
7
6
const ws = new Server ( { port } ) ;
8
7
9
8
ws . on ( 'connection' , ( connection , req ) => {
You can’t perform that action at this time.
0 commit comments