|
@@ -0,0 +1,230 @@
|
|
|
+/*
|
|
|
+servera index file
|
|
|
+*/
|
|
|
+var argvs = require("optimist").argv;
|
|
|
+var path = require("path");
|
|
|
+var fs = require("fs-extra");
|
|
|
+var _ = require("lodash");
|
|
|
+
|
|
|
+var options = {
|
|
|
+ pwd: argvs.datadir || process.env.PWD,
|
|
|
+ dirname: argvs.dirname || __dirname,
|
|
|
+ port: argvs.port || 3004,
|
|
|
+ verbose: argvs.verbose || false,
|
|
|
+ debug: argvs.debug || false,
|
|
|
+ origin: argvs.origin || process.env.APP_DOMAIN || (Math.floor(Math.random() * 0xffffffff)),
|
|
|
+}
|
|
|
+options.configfile = argvs.configfile || path.join(options.pwd, "/servera.config.json")
|
|
|
+try {
|
|
|
+ options = Object.assign(require(options.configfile), options);
|
|
|
+} catch (e) {
|
|
|
+ !!options.verbose ? console.log("no file:", options.configfile) : null;
|
|
|
+ options.noconfigfile = true;
|
|
|
+}
|
|
|
+
|
|
|
+!!options.verbose ? console.log("using config:", options) : null;
|
|
|
+
|
|
|
+if (argvs.help) {
|
|
|
+ console.log("--datadir", options.pwd);
|
|
|
+ console.log("--dirname", options.dirname);
|
|
|
+ console.log("--port", options.port);
|
|
|
+ console.log("--verbose", options.verbose);
|
|
|
+ console.log("--debug", options.debug);
|
|
|
+ console.log("--origin", options.origin);
|
|
|
+ console.log("--configfile", options.noconfigfile ? "not found" : "", options.configfile);
|
|
|
+ console.log("--repl", argvs.repl);
|
|
|
+ console.log("--client", argvs.client);
|
|
|
+ console.log("--init");
|
|
|
+ process.exit()
|
|
|
+}
|
|
|
+if (argvs.init) {
|
|
|
+ console.log("INIT");
|
|
|
+ var fs = require("graceful-fs");
|
|
|
+ if (!fs.existsSync(path.join(options.pwd, "/views"))) {
|
|
|
+ fs.mkdirSync(path.join(options.pwd, "/views"))
|
|
|
+ fs.copyFileSync(path.join(options.dirname, "/views/index.html"), path.join(options.pwd, "/views/index.html"))
|
|
|
+ }
|
|
|
+ console.log(fs);
|
|
|
+ process.exit();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+var server = require("aserver")(options);
|
|
|
+server = require("aserver-extend")(server);
|
|
|
+server = require("aserver-tools")(server);
|
|
|
+server.rooten = {};
|
|
|
+
|
|
|
+server.options = options;
|
|
|
+
|
|
|
+server.add_action("NOPE server.init.first", function(app) {
|
|
|
+ app.use(server.ratelimit({
|
|
|
+ max: 120,
|
|
|
+ windowMs: 120 * 1000,
|
|
|
+ delayAfter: 100
|
|
|
+ }));
|
|
|
+}, 7)
|
|
|
+
|
|
|
+server.add_action("server.init.first", function(app) {
|
|
|
+ app.use(function(req, res, next) {
|
|
|
+ console.log(req.method, req.url);
|
|
|
+ next();
|
|
|
+ });
|
|
|
+}, 7)
|
|
|
+
|
|
|
+var tool = {};
|
|
|
+/*tool.template = function template(str, obj) {
|
|
|
+ return str.replace(/\{\{([a-zA-Z0-9]*)\}\}/gi, function(a, m) {
|
|
|
+ return obj[m] ? obj[m] : m
|
|
|
+ })
|
|
|
+}*/
|
|
|
+
|
|
|
+tool._ = require("lodash");
|
|
|
+tool.util = require("util");
|
|
|
+//tool = require("./clienta")(tool);
|
|
|
+//tool = require("./dist/lib_src")(tool, false)
|
|
|
+server.tool = tool
|
|
|
+
|
|
|
+
|
|
|
+//require("./serverstd")(server);
|
|
|
+//require("./plugins/buckets/bucketstd")(server);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+try {
|
|
|
+ var ff = fs.readdirSync(path.join(options.pwd, "./node_modules")).map(function(file) {
|
|
|
+ console.log("FIII", file);
|
|
|
+ return {
|
|
|
+ file: file,
|
|
|
+ isa: fs.readdirSync(path.join(options.pwd, "./node_modules/", file)).filter(function(filea) {
|
|
|
+ return filea === "electra.json"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }).filter(function(f) {
|
|
|
+ return f.isa.length > 0
|
|
|
+ })
|
|
|
+ ff.map(function(ffa) {
|
|
|
+ console.log("**** loading module: ", ffa.file)
|
|
|
+ require(ffa.file)(server);
|
|
|
+ })
|
|
|
+} catch (e) {
|
|
|
+ console.log("ERROR:", e)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+server.add_action("server.init", function(app) {
|
|
|
+
|
|
|
+ /* app.get("/plugins/:plugin/:file", function(req, res, next) {
|
|
|
+ res.render(path.join(options.dirname, "./plugins/", req.params.plugin, req.params.file + ".html"), Object.assign({
|
|
|
+ req: req,
|
|
|
+ tool: tool,
|
|
|
+ }, tool));
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ app.get("/", function(req, res, next) {
|
|
|
+ res.render("index.html", Object.assign({
|
|
|
+ req: req,
|
|
|
+ tool: tool,
|
|
|
+ }, tool));
|
|
|
+ })
|
|
|
+
|
|
|
+ app.get("/tester", function(req, res, next) {
|
|
|
+ res.render("test.html", Object.assign({
|
|
|
+ req: req,
|
|
|
+ tool: tool,
|
|
|
+ }, tool));
|
|
|
+ })
|
|
|
+*/
|
|
|
+
|
|
|
+ app.get("/", function(req, res, next) {
|
|
|
+
|
|
|
+ res.send({
|
|
|
+ "status": "ok"
|
|
|
+ })
|
|
|
+
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+});
|
|
|
+
|
|
|
+server.add_action("request.init", function(req, res, next) {
|
|
|
+ console.log("req.url", req.ip, req.url, req.parts)
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+try {
|
|
|
+ require(path.join(options.pwd, "/server.js"))(server);
|
|
|
+} catch (e) {
|
|
|
+ !!options.verbose ? console.log("no file:", path.join(options.pwd, "/server.js"), e) : null;
|
|
|
+}
|
|
|
+
|
|
|
+if (argvs.repl) {
|
|
|
+ var repl = require("repl");
|
|
|
+ var cmdline = repl.start({
|
|
|
+ prompt: '> '
|
|
|
+ });
|
|
|
+ cmdline.context.server = server;
|
|
|
+ cmdline.context.options = options;
|
|
|
+ Object.defineProperty(cmdline.context, 'rooten', {
|
|
|
+ enumerable: true,
|
|
|
+ get: function get() {
|
|
|
+ return server.rooten;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ function bb(arg) {
|
|
|
+ this.name = arg
|
|
|
+ }
|
|
|
+ bb.prototype.test = function() {
|
|
|
+ return this.name;
|
|
|
+ }
|
|
|
+ cmdline.context.bb = bb;
|
|
|
+ cmdline.context.alle =
|
|
|
+ cmdline.context.app = server;
|
|
|
+ cmdline.context._ = _;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+if (argvs.client) {
|
|
|
+ console.log("client code to come");
|
|
|
+} else {
|
|
|
+ server.startit();
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+
|
|
|
+
|
|
|
+function sttttttt() {
|
|
|
+ AWS = require("aws-sdk");
|
|
|
+ app.aws = AWS;
|
|
|
+ AWS.config.update({
|
|
|
+ region: "eu-west-1",
|
|
|
+ credentials: new AWS.CognitoIdentityCredentials({
|
|
|
+ IdentityPoolId: "eu-west-1:5343270a-03fd-4856-ad07-25f1e2c78246"
|
|
|
+ })
|
|
|
+ });
|
|
|
+ var s3 = new AWS.S3({
|
|
|
+ apiVersion: '2006-03-01',
|
|
|
+ params: {
|
|
|
+ Bucket: "a.tum.dk"
|
|
|
+ }
|
|
|
+ });
|
|
|
+ app.s3 = s3;
|
|
|
+ app.s3.upload({
|
|
|
+ Key: "data/test.json",
|
|
|
+ Body: "{}",
|
|
|
+ ContentType: "application/json",
|
|
|
+ "headers": {
|
|
|
+ "Access-Control-Allow-Origin": "*"
|
|
|
+ },
|
|
|
+ ACL: 'public-read'
|
|
|
+ }, function(err, data) {
|
|
|
+ console.log("err", err, data);
|
|
|
+ })
|
|
|
+
|
|
|
+}
|