-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodepost.js
153 lines (120 loc) · 5.15 KB
/
nodepost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const fs = require( "fs" );
const path = require( "path" );
const crypto = require('crypto');
const express = require( "express" );
const chp = require( "child_process" );
const pm = require( "./path-manager/pathManager" );
const co = require( "./path-manager/colorOrganizer" );
const rm = require( "./route-manager/routeManager" );
const dm = require( "./database-manager/databaseManager" );
const nodepost = express();
const PORT = 1400;
const log = console.log;
const rootPath = __dirname;
const UPDATE = co.colorizeLine( "yellow" )( "Update:" );
const READ = co.colorizeLine( "green" )( "Read: " );
const USER = "User: ";
const stat = dm.stat;
const routeDirs = dm.routeDirs;
const statAddress = dm.statAddress;
const postsJson = dm.postsJson;
// create an array of valid paths we have
const routeJson = rm.makeRoute( postsJson );
// create or delete new directories
pm.manageDir( routeJson, routeDirs, rootPath );
// always serve these
nodepost.use( "/build", express.static( rootPath + "/build" ) );
nodepost.use( "/vendor", express.static( rootPath + "/vendor" ) );
nodepost.use( "/react-js", express.static( rootPath + "/react-js" ) );
// cache each post after last modification
const cache = {};
// for deferring some functions
async function defer( fn, ...args ){
return F;
}
// Any GET request will be handled here
nodepost.get( "/*", function( request, response ){
const requestPath = request.path;
const actualPath = requestPath === "/" ? routeJson[ 0 ] : routeJson[ 0 ] + requestPath;
const absolutePath = rootPath + actualPath;
// it will be either one of:
// true thus the path was found
// false thus the path was not found
const validPath = routeJson.some( ( path ) => path === actualPath );
const xForwardedFor = request.headers[ "x-forwarded-for" ];
const userAgent = request.headers[ "user-agent" ];
// base on Valid Path
switch( validPath ){
case false:
if( statAddress !== "" && requestPath === statAddress )
response.send( stat );
else
response.send( pm.getContent( rootPath + "/error-page/404" ) );
break;
default:
log( xForwardedFor, requestPath );
log( USER, userAgent );
const mtime_path = fs.statSync( absolutePath ).mtime.toString();
const mtime_main_html = fs.statSync( absolutePath + "/main.html" ).mtime.toString();
const mtime = crypto.createHmac( "md5", mtime_path + mtime_main_html ).digest( "hex" );
if( cache[ absolutePath ] ){
if( cache[ absolutePath ].mtime === mtime ){
log( READ, "cache ..." );
// defer( pm.makeStat ).then( ms => ms( stat, request, rootPath ) );
pm.makeStat( stat, request, rootPath );
response.send( cache[ absolutePath ].html );
} else {
log( UPDATE, "cache ..." );
cache[ absolutePath ].mtime = mtime;
cache[ absolutePath ].html = pm.getContent( absolutePath, mtime_main_html );
// defer( pm.makeStat ).then( ms => ms( stat, request, rootPath ) );
pm.makeStat( stat, request, rootPath );
response.send( cache[ absolutePath ].html );
}
} else {
log( READ, "file system ..." );
cache[ absolutePath ] = { mtime: "", html: "" };
cache[ absolutePath ].mtime = mtime;
cache[ absolutePath ].html = pm.getContent( absolutePath, mtime_main_html );
// defer( pm.makeStat ).then( ms => ms( stat, request, rootPath ) );
pm.makeStat( stat, request, rootPath );
response.send( cache[ absolutePath ].html );
}
log( "end of response" );
}
});
nodepost.post( "/gitpush", function( request, response ){
log( "gitpush" );
request.on( "data", function( chunk ){
const secret = "this-will-be-the-secret-key";
const sig = "sha1=" + crypto.createHmac( "sha1", secret ).update( chunk.toString() ).digest( "hex" );
const x_hub_signature = request.headers['x-hub-signature'];
if( x_hub_signature === sig ){
log( "POST from github" );
const gitPull = chp.spawn( "git", [ "pull" ] );
gitPull.stdout.on( "data", function( data ){
log( "stdout: ", data.toString() );
});
gitPull.stderr.on( "data", function( data ){
log( "stderr: ", data.toString() );
});
gitPull.on( "close", function( code ){
log( "exit code:", code );
});
gitPull.on( "error", function( code ){
log( "error code:", code );
});
} else {
log( "unknown POST" );
}
});
response.status( 200 ).end();
});
nodepost.listen( PORT, function(){
log( `Server is running at http://localhost:${ PORT }/` );
});
process.on('uncaughtException', function ( exception ) {
log( "uncaughtException happened ..." );
log( "message:", exception.message );
process.exit( 0 );
});