File tree 9 files changed +56
-30
lines changed
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 2
2
3
3
const pg = require ( 'pg' ) ;
4
4
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 ) => ( {
14
6
async query ( sql , args ) {
15
7
return await pool . query ( sql , args ) ;
16
8
} ,
@@ -57,3 +49,5 @@ module.exports = (table) => ({
57
49
return await pool . query ( sql , [ id ] ) ;
58
50
} ,
59
51
} ) ;
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) => {
9
9
return JSON . parse ( data ) ;
10
10
} ;
11
11
12
- module . exports = ( routing , port ) => {
12
+ module . exports = ( routing , port , console ) => {
13
13
http . createServer ( async ( req , res ) => {
14
14
const { url, socket } = req ;
15
15
const [ name , method , id ] = url . substring ( 1 ) . split ( '/' ) ;
Original file line number Diff line number Diff line change @@ -68,4 +68,4 @@ class Logger {
68
68
}
69
69
}
70
70
71
- module . exports = new Logger ( './log' ) ;
71
+ module . exports = Logger ;
Original file line number Diff line number Diff line change 2
2
3
3
const fsp = require ( 'node:fs' ) . promises ;
4
4
const path = require ( 'node:path' ) ;
5
- const server = require ( './ws.js' ) ;
6
5
const staticServer = require ( './static.js' ) ;
7
6
const load = require ( './load.js' ) ;
8
- const db = require ( './db.js' ) ;
7
+ const createDbQueryConstructor = require ( './db.js' ) ;
9
8
const 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 ) ;
11
15
12
16
const sandbox = {
13
17
console : Object . freeze ( logger ) ,
@@ -26,6 +30,6 @@ const routing = {};
26
30
routing [ serviceName ] = await load ( filePath , sandbox ) ;
27
31
}
28
32
29
- staticServer ( './static' , 8000 ) ;
30
- server ( routing , 8001 ) ;
33
+ staticServer ( config . staticPath , config . staticPort , logger ) ;
34
+ server ( routing , config . apiPort , logger ) ;
31
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 @@ -4,18 +4,20 @@ const http = require('node:http');
4
4
const path = require ( 'node:path' ) ;
5
5
const fs = require ( 'node:fs' ) ;
6
6
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 ) ;
19
21
20
22
console . log ( `Static on port ${ port } ` ) ;
21
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