change approach again

This commit is contained in:
2018-05-09 14:31:22 +02:00
parent 54a933c83a
commit 0686e02b75
2252 changed files with 864743 additions and 270 deletions

19
src/config.ts Normal file
View File

@ -0,0 +1,19 @@
import * as fs from 'fs'
import * as cmdargs from 'command-line-args'
const OPTION_DEFINITIONS = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'config', alias: 'c', type: String, defaultValue: '~/homepage.conf' }
];
export let dict : any
export function readConfig() {
let options = cmdargs(OPTION_DEFINITIONS)
dict = JSON.parse(fs.readFileSync(options.config, "utf8"))
}
readConfig()

79
src/log.ts Normal file
View File

@ -0,0 +1,79 @@
import * as moment from 'moment'
import * as config from './config'
import * as nodemailer from 'nodemailer'
enum Level {
All,
NoDebug,
NoDebugNoInfo,
NoDebugNoInfoNoWarning
}
var level = Level.NoDebug
function timestamp(): string {
return moment().format('HH:mm:ss.SSS')
}
export function setLevel(value: string): void {
switch (value) {
case 'info': level = Level.NoDebug; break
case 'warn': level = Level.NoDebugNoInfo; break
case 'error': level = Level.NoDebugNoInfoNoWarning; break
default: level = Level.All
}
}
export function sendAlarmMail(subject : string, message : string): void {
let transport = nodemailer.createTransport({
host: config.dict.smtpHost,
port: config.dict.smtpPort,
secure: false,
tls: {
rejectUnauthorized: false
}
});
let mail : nodemailer.SendMailOptions = {
from: config.dict.smtpSender,
to: config.dict.smtpReceiver,
subject: subject,
text: message
};
transport.sendMail(mail)
.then((v : nodemailer.SentMessageInfo) => {
info(`Mail sent, ${subject}, ${message}, ${v.response}`)
})
.catch((reason : any) => {
error(`Failure when sending alarm mail: ${message}, ${reason}`)
})
}
export function info(message: string): void {
if (level < Level.NoDebugNoInfo) {
console.log(`${timestamp()} [ II ] ${message}`)
}
}
export function warn(message: string): void {
if (level < Level.NoDebugNoInfoNoWarning) {
console.log(`${timestamp()} [ WW ] ${message}`)
}
}
export function error(message: string): void {
console.log(`${timestamp()} [ EE ] ${message}`)
}
export function success(message: string): void {
console.log(`${timestamp()} [ OK ] ${message}`)
}
export function debug(message: string): void {
if (level < Level.NoDebug) {
console.log(`${timestamp()} [ DB ] ${message}`)
}
}

48
src/main.ts Normal file
View File

@ -0,0 +1,48 @@
import * as fs from 'fs'
import * as config from './config'
import * as logger from './log'
import * as express from 'express'
logger.info("Homepage starting")
let app = express()
let masterTmpl: string
app.engine('pag', (filePath: string, options: any, callback: any) => {
fs.readFile(filePath, (err, content) => {
if (err) {
return callback(new Error(err.message))
}
let renderedPhase1 = content.toString()
.replace('#bla#', 'blu')
let renderedPhase2 = masterTmpl
.replace('#maincontent#', renderedPhase1)
.replace('#title#', options.title)
return callback(null, renderedPhase2)
})
})
app.set('views', './docroot')
app.set('view engine', 'pag')
app.get('/index', (req, res) => {
res.render('index', {'title': 'Projects - just for fun'})
})
app.get('/', (req, res) => {
res.send('Hello world!')
})
masterTmpl = fs.readFileSync(config.dict.masterTmpl).toString()
app.listen(config.dict.httpPort, () => {
logger.info("Homepage is listening")
})
logger.info("Homepage running")