-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (151 loc) · 4.89 KB
/
index.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
154
155
156
157
158
159
160
//SETUP
var fs = require('fs')
var express = require('express')
var bodyParser = require('body-parser')
var config = require('./server/config')
var secure = require('./secure')
var arr = process.argv[1].split('/')
var path = ""
for (var i = 0; i < arr.length-1; i++) {
path += arr[i]
path += '/'
}
config.path = {
website : path+'website/',
index : path,
api : path+'server/api/',
db : path+'server/db/',
mail : path+'server/mailtemplates/'
}
secure.path = path
if (!secure.setUp()) {
console.log('secure setup failed')
return false;
} else { console.log('secure setup successful') }
var auth = require('./server/auth')
var request_handler = require('./server/handler')
var mailer = require('./server/mailer')
var exapp = express()
//WEBSITE
exapp.use(express.static('website'))
exapp.use(bodyParser.json())
exapp.use(function(req,res,next){
//called for all requests
next()
})
//API
exapp.get('/auth', function(request,response){
request_handler.auth(request,response)
})
exapp.get('/cache', function(request,response){
response.status(200).send(require('./server/cache').get()).end()
})
exapp.get('/login*', function(request,response){
request_handler.login(request,response)
})
exapp.post('/register*', function(request,response){
request_handler.register(request,response)
})
exapp.get('/register*', function(request,response){
request_handler.validate(request,response)
})
exapp.get('/api*', function(request,response){
request_handler.get(request,response)
})
exapp.post('/api*', function(request,response){
request_handler.post(request,response)
})
var commands = {
err: function(msg) {
///DESC Throw a new error and crash///DESC
console.info('exit');
throw new Error('Forced error');
return true;
},
cmd: function() {
///DESC Prints all available commands///DESC
console.info(' --- Commands:')
for (var key in commands) {
console.info(key+" --"+commands[key].toString().split('///DESC')[1])
}
return true;
},
log: function() {
///DESC Saves current log to a new file and begins a new logfile///DESC
saveLog()
newLog()
}
}
var newLog = function(){
var str = '<<< BEGIN LOG '
str += '|| date: '+new Date(Date.now()).toString()+" "
str += '|| host: '+config.host.name+':'+config.host.port
str += ' >>>\n\n'
fs.writeFileSync(config.path.index+'log/__log.txt',str)
}
var saveLog = function(){
var log = fs.readFileSync(config.path.index+'server/log/__log.txt','UTF-8')
var now = new Date(Date.now())
var year = now.getFullYear()
var month = now.getMonth() + 1
var day = now.getDate()
var path = config.path.index+'log/logfile_'+day+month+year+'_'+now+'.txt'
fs.writeFileSync(path.replace(/ /g,''),log)
}
console.log = function(data) {
var now = new Date(Date.now())
var time = now.getHours()+":"+now.getMinutes()+":"
var seconds = now.getSeconds()
if (seconds < 10) { seconds = "0"+seconds }
var mseconds = now.getMilliseconds()
if (mseconds < 10) { mseconds = "00"+mseconds }
else if (mseconds < 100) { mseconds = "0"+mseconds }
time += seconds+"::"+mseconds+" "
var entry = time+data.toString()+"\n"
fs.appendFileSync(config.path.index+'log/__log.txt',entry)
}
console.error = function(data) {
var now = new Date(Date.now())
var time = now.getHours()+":"+now.getMinutes()+":"
var seconds = now.getSeconds()
if (seconds < 10) { seconds = "0"+seconds }
var mseconds = now.getMilliseconds()
if (mseconds < 10) { mseconds = "00"+mseconds }
else if (mseconds < 100) { mseconds = "0"+mseconds }
time += seconds+"::"+mseconds+" "
var entry = time+data.toString()+"\n"
fs.appendFileSync(config.path.index+'log/__err.txt',entry)
}
try {
var log = fs.readFileSync(config.path.index+'log/__log.txt','UTF-8')
var str = '\n<<< BEGIN LOG '
str += '|| date: '+new Date(Date.now()).toString()+" "
str += '|| host: '+config.host.name+':'+config.host.port
str += ' >>>\n\n'
fs.appendFileSync(config.path.index+'log/__log.txt',str)
} catch (e) {
console.info('No log found: starting new logfile')
newLog()
}
//SERVER
exapp.listen(config.host.port, function () {
console.info('HOSTNAME:'+config.host.name+', listening on PORT:'+config.host.port)
console.info('Server Running')
commands.cmd()
process.stdin.setEncoding('UTF-8')
var prompt = "% <"+config.host.name+":"+config.host.port+"> "
process.stdout.write(prompt)
process.stdin.on('data', function(data){
console.log(data.toString().trim())
try {
var params = (data.trim()+"").replace(/\n/g,'').split(' ')
var command = params[0]
params.splice(0,1)
commands[command].apply(null,params)
} catch (e) {
if (e.toString() == 'Error: Forced error') throw e;
console.info('Sorry, '+(data.trim()+"").replace(/\n/g,'')+' is not a valid command. Use [cmd] to print all available commands.')
}
process.stdout.write(prompt)
})
})