blob: 4e7317473cfce61b781a24fd575d90b1a1def433 [file] [log] [blame]
TamasBakai9b780332019-02-15 08:38:16 +00001var http = require('http');
2var https = require('https');
3var ArgumentParser = require('argparse').ArgumentParser;
4var express = require('express');
5const stream = require('stream');
6var app = express();
maximessonb8b94ae2019-03-25 16:03:08 +00007var fs = require('fs');
TamasBakai9b780332019-02-15 08:38:16 +00008var path = require('path');
9var privateKey = fs.readFileSync('cert/private.key', 'utf8');
10var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
11var credentials = {key: privateKey, cert: certificate};
maximessonb8b94ae2019-03-25 16:03:08 +000012const allPublished = "allPublished";
13const nonePublished = "nonePublished";
TamasBakai9b780332019-02-15 08:38:16 +000014
15var parser = new ArgumentParser({
16 version: '0.0.1',
17 addHelp:true,
18 description: 'Datarouter simulator'
19 });
20
21parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
22parser.addArgument('--printtc' ,
23 {
24 help: 'Print complete usage help',
25 action: 'storeTrue'
26 }
27 );
28
29var args = parser.parseArgs();
30
maximessonb8b94ae2019-03-25 16:03:08 +000031if (args.tc=="nonePublished") {
32 console.log("TC: nonePublished")
TamasBakai9b780332019-02-15 08:38:16 +000033}
maximessonb8b94ae2019-03-25 16:03:08 +000034if (args.tc=="allPublished") {
35 console.log("TC: allPublished")
TamasBakai9b780332019-02-15 08:38:16 +000036 //preparations
37}
38
39if (args.printtc) {
maximessonb8b94ae2019-03-25 16:03:08 +000040 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");
TamasBakai9b780332019-02-15 08:38:16 +000043 process.exit(0);
44}
45
46var bodyParser = require('body-parser')
47app.use(bodyParser.urlencoded({ extended: false }))
48
49// parse application/json
50app.use(bodyParser.json())
51
52// parse application/vnd.api+json as json
53app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
54
55// parse some custom thing into a Buffer
56app.use(bodyParser.raw({limit:1024*1024*20, type: 'application/octet-stream' }))
57// parse an HTML body into a string
58app.use(bodyParser.text({ type: 'text/html' }))
59app.get("/",function(req, res){
60 res.send("ok");
61})
maximessonb8b94ae2019-03-25 16:03:08 +000062
63
64var published = [];
65app.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 }
TamasBakai9b780332019-02-15 08:38:16 +000078})
maximessonb8b94ae2019-03-25 16:03:08 +000079
80
81app.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
TamasBakai9b780332019-02-15 08:38:16 +0000105var httpServer = http.createServer(app);
106var httpsServer = https.createServer(credentials, app);
107
maximessonb8b94ae2019-03-25 16:03:08 +0000108var httpPort=3906;
109var httpsPort=3907;
TamasBakai9b780332019-02-15 08:38:16 +0000110httpServer.listen(httpPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000111console.log("DR-simulator listening (http) at "+httpPort);
TamasBakai9b780332019-02-15 08:38:16 +0000112httpsServer.listen(httpsPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000113console.log("DR-simulator listening (https) at "+httpsPort);