diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-09-13 11:43:06 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-09-13 11:44:11 +0200 |
commit | f00ba92ed2cc1a9c24ad783e83525d1b5a85b857 (patch) | |
tree | 4d1c00a47c7f3842bcf3dece83d3c00ed3ae459f /webserver.js |
Initial
Diffstat (limited to 'webserver.js')
-rwxr-xr-x | webserver.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/webserver.js b/webserver.js new file mode 100755 index 0000000..e4b51b9 --- /dev/null +++ b/webserver.js @@ -0,0 +1,120 @@ +#!/usr/bin/env node +var cmn=require("./modules/$common.js"); +var app=require("express")(), + http=require("http"), + httpServer=http.Server(app), + io=require("socket.io")(httpServer), + url=require("url"), + fs=require("fs"), + util=require("util"), + bodyParser=require("body-parser"), + basicAuth=require("basic-auth"); + + +if(process.argv.length>3){ + console.log("Pass optional port as first argument"); + process.exit(1); +} +var PORT=process.argv.length==3?+process.argv[2]:80; + + +var whatpulse={"keys":"<??>","clicks":"<??>"}; +function refreshWhatpulse(){ + http.get("http://api.whatpulse.org/user.php?user=tomsmeding&format=json&formatted=yes",function(res){ + var body=""; + res.on("data",function(data){body+=data;}); + res.on("end",function(){ + try{body=JSON.parse(body);} + catch(e){return;} + whatpulse.keys=body.Keys/*.replace(/,/g," ")*/; + whatpulse.clicks=body.Clicks/*.replace(/,/g," ")*/; + }); + }); +} +setInterval(refreshWhatpulse,6*3600*1000); //every 6 hours +refreshWhatpulse(); + +/*app.use(function (req, res, next) { + console.log(req.subdomains); + next(); +});*/ + +app.use(bodyParser.text()); + + +var module_list=fs.readdirSync("modules").filter(function(f){ + return ["$common.js",".DS_Store"].indexOf(f)==-1; +}); +for(i=0;i<module_list.length;i++){ + if(module_list[i]=="$common.js")continue; + require("./modules/"+module_list[i]+"/"+module_list[i]+".js")(app,io,cmn.serverdir+"/modules/"+module_list[i]); + console.log("Loaded module "+module_list[i]); +} + + + +app.get("/",function(req,res){ + res.send( + String(fs.readFileSync(cmn.serverdir+"/index.html")) + .replace(/<!--<<PULSE-KEYS>>-->/,whatpulse["keys"]) + .replace(/<!--<<PULSE-CLICKS>>-->/,whatpulse["clicks"]) + ); +}); + +app.get("/f/univq/*",cmn.authgen()); + +app.get("/f/*",function(req,res){ + var parsed=url.parse(req.url),basefname=parsed.pathname.slice(2).replace(/\/\.+[^\/]*\//g,"/"); + fname=cmn.rootdir+"/web_files"+basefname; + console.log("Requesting file "+fname); + var stats=fs.statSync(fname); + if(!fs.existsSync(fname))res.send("That file does not exist."); + else if(stats.isFile())res.sendFile(fname); + else if(stats.isDirectory()){ + var items=fs.readdirSync(fname) + .filter(function(f){return f[0]!=".";}) + .map(function(f){ + if(fs.statSync(fname+"/"+f).isDirectory())return f+"/"; + else return f; + }); + res.send( + String(fs.readFileSync(cmn.rootdir+"/web_files/dirlisting.html")) + .replace("<!--DIRDIR-->",basefname) + .replace("[/*LISTINGLISTING*/]",JSON.stringify(items)) + ); + } else res.send("I don't recognise that file."); +}); + +["o","k","rip","rip2"].forEach(function(target){ + app.get("/"+target,function(req,res){ + res.sendFile(cmn.rootdir+"/web_files/"+target+".html"); + }); + app.get("/"+target+"/*",function(req,res){ + res.set('Content-Type', 'text/html'); + res.send( + String(fs.readFileSync(cmn.rootdir+"/web_files/"+target+".html")) + .replace("<!--MESSAGEMESSAGE-->",cmn.simpleHTMLescape(url.parse(req.url).pathname.slice(target.length+2))) + ); + }); +}); + +app.get("/dr",function(req,res){ + res.sendFile(cmn.rootdir+"/web_files/duckroll.html"); +}); + +app.get(["/gpg","/pgp","/gpg.asc","/pgp.asc"],function(req,res){ + res.type("text/plain"); + res.sendFile(cmn.rootdir+"/web_files/pgp.asc"); +}); + + +/*app.get("/chat",function(req,res){ + res.redirect(301,"http://tomsmeding.com:81"); +});*/ + + +var server=httpServer.listen(PORT,function(){ + var host=server.address().address; + var port=server.address().port; + console.log("Server listening at http://"+host+":"+port); +}); |