TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 1 | var http = require('http'); |
| 2 | var https = require('https'); |
| 3 | var ArgumentParser = require('argparse').ArgumentParser; |
| 4 | var express = require('express'); |
| 5 | const stream = require('stream'); |
| 6 | var app = express(); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 7 | var fs = require('fs'); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 8 | var path = require('path'); |
| 9 | var privateKey = fs.readFileSync('cert/private.key', 'utf8'); |
| 10 | var certificate = fs.readFileSync('cert/certificate.crt', 'utf8'); |
| 11 | var credentials = {key: privateKey, cert: certificate}; |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 12 | const allPublished = "allPublished"; |
| 13 | const nonePublished = "nonePublished"; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 14 | |
| 15 | var parser = new ArgumentParser({ |
| 16 | version: '0.0.1', |
| 17 | addHelp:true, |
| 18 | description: 'Datarouter simulator' |
| 19 | }); |
| 20 | |
| 21 | parser.addArgument('--tc' , { help: 'TC $NoOfTc' } ); |
| 22 | parser.addArgument('--printtc' , |
| 23 | { |
| 24 | help: 'Print complete usage help', |
| 25 | action: 'storeTrue' |
| 26 | } |
| 27 | ); |
| 28 | |
| 29 | var args = parser.parseArgs(); |
| 30 | |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 31 | if (args.tc=="nonePublished") { |
| 32 | console.log("TC: nonePublished") |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 33 | } |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 34 | if (args.tc=="allPublished") { |
| 35 | console.log("TC: allPublished") |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 36 | //preparations |
| 37 | } |
| 38 | |
| 39 | if (args.printtc) { |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 40 | console.log("TC nonePublished: no file has already been published."); |
| 41 | console.log("TC allPublished: whatever is the request, this file is considered as published."); |
| 42 | console.log("No argument passed: normal behaviour, that is publish if not already published"); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 43 | process.exit(0); |
| 44 | } |
| 45 | |
| 46 | var bodyParser = require('body-parser') |
| 47 | app.use(bodyParser.urlencoded({ extended: false })) |
| 48 | |
| 49 | // parse application/json |
| 50 | app.use(bodyParser.json()) |
| 51 | |
| 52 | // parse application/vnd.api+json as json |
| 53 | app.use(bodyParser.json({ type: 'application/vnd.api+json' })) |
| 54 | |
| 55 | // parse some custom thing into a Buffer |
| 56 | app.use(bodyParser.raw({limit:1024*1024*20, type: 'application/octet-stream' })) |
| 57 | // parse an HTML body into a string |
| 58 | app.use(bodyParser.text({ type: 'text/html' })) |
| 59 | app.get("/",function(req, res){ |
| 60 | res.send("ok"); |
| 61 | }) |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 62 | |
| 63 | |
| 64 | var published = []; |
| 65 | app.get('/feedlog/1/',function(req, res){ |
| 66 | var filename = req.query.filename; |
| 67 | if(args.tc == allPublished){ |
| 68 | res.send("[" + filename + "]"); |
| 69 | } else if(args.tc == nonePublished){ |
| 70 | res.send("[]"); |
| 71 | } else { |
| 72 | if (published.includes(filename)) { |
| 73 | res.send("[" + filename + "]"); |
| 74 | } else { |
| 75 | res.send("[]"); |
| 76 | } |
| 77 | } |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 78 | }) |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 79 | |
| 80 | |
| 81 | app.put('/publish/1/', function (req, res) { |
| 82 | var filename = req.query.filename; |
| 83 | var type = req.query.type; |
| 84 | if(typeof(filename) == 'undefined'){ |
| 85 | res.status(400).send({error: 'No filename provided.'}); |
| 86 | } else if(typeof(type) == 'undefined'){ |
| 87 | res.status(400).send({error: 'No type provided.'}); |
| 88 | } else { |
| 89 | if(args.tc == allPublished){ |
| 90 | res.send("[" + filename + "]"); |
| 91 | } else if(args.tc == nonePublished){ |
| 92 | res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename); |
| 93 | } else { |
| 94 | if (!published.includes(filename)) { |
| 95 | published.push(filename); |
| 96 | res.redirect(301, 'http://127.0.0.1:3908/publish/1/'+filename); |
| 97 | } else { |
| 98 | res.send("ok"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | }) |
| 103 | |
| 104 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 105 | var httpServer = http.createServer(app); |
| 106 | var httpsServer = https.createServer(credentials, app); |
| 107 | |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 108 | var httpPort=3906; |
| 109 | var httpsPort=3907; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 110 | httpServer.listen(httpPort); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 111 | console.log("DR-simulator listening (http) at "+httpPort); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 112 | httpsServer.listen(httpsPort); |
maximesson | b8b94ae | 2019-03-25 16:03:08 +0000 | [diff] [blame^] | 113 | console.log("DR-simulator listening (https) at "+httpsPort); |