2023-02-25 05:24:07 +00:00
|
|
|
import * as WebSocket from 'ws'
|
2023-02-25 07:08:05 +00:00
|
|
|
import express, { Express, Request, Response, Application } from 'express'
|
2023-02-25 05:24:07 +00:00
|
|
|
import { readFile } from 'fs/promises'
|
|
|
|
import Log = require('log');
|
|
|
|
|
|
|
|
interface ServerData {
|
|
|
|
[key: string]: string;
|
|
|
|
PORT? : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ServerTemplate {
|
|
|
|
name : string;
|
|
|
|
path : string;
|
|
|
|
data? : string
|
|
|
|
}
|
|
|
|
|
|
|
|
class Server {
|
|
|
|
private id : string = 'server'
|
|
|
|
public isActive : boolean = false
|
|
|
|
private log : any
|
|
|
|
private templates : ServerTemplate[] = [
|
|
|
|
{
|
|
|
|
name :'index',
|
|
|
|
path : 'server.html'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name : 'script',
|
|
|
|
path : 'lib/client/index.js'
|
|
|
|
}
|
|
|
|
]
|
2023-02-25 07:08:05 +00:00
|
|
|
private http : Application
|
|
|
|
private httpd : Server
|
2023-02-25 05:24:07 +00:00
|
|
|
private port : number = 9900
|
|
|
|
private wsPort : number = 9901
|
2023-02-25 07:08:05 +00:00
|
|
|
|
2023-02-25 05:24:07 +00:00
|
|
|
constructor () {
|
|
|
|
this.init()
|
2023-02-25 07:08:05 +00:00
|
|
|
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init () {
|
|
|
|
this.log = await Log({ label : this.id });
|
2023-02-25 07:08:05 +00:00
|
|
|
await this.load()
|
|
|
|
await this.start()
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load () {
|
2023-02-25 07:08:05 +00:00
|
|
|
this.http = express()
|
2023-02-25 05:24:07 +00:00
|
|
|
for (let tmpl of this.templates) {
|
|
|
|
tmpl.data = await readFile(tmpl.path, 'utf8')
|
|
|
|
}
|
2023-02-25 07:08:05 +00:00
|
|
|
|
|
|
|
this.http.get('/', this.index.bind(this))
|
|
|
|
this.http.get('/client.js', this.script.bind(this))
|
|
|
|
|
2023-02-25 05:24:07 +00:00
|
|
|
this.log.info("Server assets loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
template (name: string, data : ServerData) {
|
|
|
|
let html : string = this.templates.find(el => el.name === name).data
|
|
|
|
for (let key of Object.keys(data)) {
|
|
|
|
html = html.replace(`{{${key}}}`, data[key])
|
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2023-02-25 07:08:05 +00:00
|
|
|
async start () {
|
|
|
|
return new Promise(function (resolve : Function, reject : Function) {
|
|
|
|
this.httpd = this.http.listen(this.port, function () {
|
|
|
|
this.isActive = true
|
|
|
|
this.log.info(`Server started!`)
|
|
|
|
this.log.info(`URL [ http://localhost:${this.port} ]`)
|
|
|
|
return resolve(true)
|
|
|
|
}.bind(this))
|
2023-02-25 05:24:07 +00:00
|
|
|
}.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2023-02-25 07:08:05 +00:00
|
|
|
return new Promise(function (resolve : Function, reject : Function) {
|
|
|
|
return this.httpd.close(function () {
|
|
|
|
this.isActive = false
|
|
|
|
this.log.info(`Server stopped :(`)
|
|
|
|
}.bind(this))
|
|
|
|
}.bind(this))
|
|
|
|
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
index (req : Request, res : Response, next : Function) {
|
|
|
|
const html : string = this.template('index', { PORT : `${this.port}` })
|
2023-02-25 07:08:05 +00:00
|
|
|
this.log.info('GET /')
|
2023-02-25 05:24:07 +00:00
|
|
|
return res.send(html)
|
|
|
|
}
|
|
|
|
script (req : Request, res : Response, next : Function) {
|
|
|
|
const js : string = this.template('script', { PORT : `${this.wsPort}` })
|
2023-02-25 07:08:05 +00:00
|
|
|
res.contentType('text/javascript')
|
|
|
|
this.log.info('GET /script.js')
|
|
|
|
return res.send(js)
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
2023-02-25 07:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
return new Server()
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|