diff --git a/api/branding/index.js b/api/branding/index.js new file mode 100644 index 000000000..6bacb2887 --- /dev/null +++ b/api/branding/index.js @@ -0,0 +1,95 @@ +(function (){ + 'use strict'; + + var async = require("async") + , express = require("express") + , request = require("request") + , helpers = require("../../helpers") + , config = require("../../config") + , endpoints = require("../endpoints") + , app = express() + , fs = require('fs') + , busboy = require('express-busboy'); + + busboy.extend(app, { + upload: true, + path: '/tmp/logo', + allowedPath: /./ + }); + + app.post("/branding", function (req, res, next) { + if (config.branding.set == true) { + res.redirect("/"); + return; + } else { + console.log(req.body.brand); + if (req.body.brand !== undefined) { + var brand = req.body.brand; + var image; + + if (brand == "cncf") { + image = "./public/img/cncf-logo.png"; + config.branding.set = true; + config.branding.values.name = "cncf"; + config.branding.values.company = "Cloud Native Computing Foundation c/o The Linux Foundation"; + config.branding.values.street = "1 Letterman Drive Suite D4700"; + config.branding.values.city = "San Francisco"; + config.branding.values.state = "CA"; + config.branding.values.zip = "94129"; + config.branding.values.country = "USA"; + } else if (brand == "weave") { + image = "./public/img/weave-logo.png"; + config.branding.set = true; + config.branding.values.name = "weave"; + config.branding.values.company = "Weaveworks Ltd."; + config.branding.values.street = "32 – 38 Scrutton Street"; + config.branding.values.city = "London"; + config.branding.values.zip = "EC2A 4RQ"; + config.branding.values.country = "UK"; + } else if (brand == "other") { + config.branding.set = true; + config.branding.values.name = "other"; + console.log(req.files.logo.file); + if (req.files.logo == undefined) { + res.redirect("/welcome.html"); + return; + } + image = req.files.logo.file; + config.branding.values.company = req.body.company; + config.branding.values.street = req.body.street; + config.branding.values.city = req.body.city; + config.branding.values.zip = req.body.zip; + config.branding.values.state = req.body.state; + config.branding.values.country = req.body.country; + } + else { + res.redirect("/welcome.html"); + return; + } + config.branding.values.logo = fs.readFileSync(image).toString("base64"); + res.redirect("/"); + return; + } else { + res.redirect("/welcome.html"); + return; + } + } + }); + + app.get("/img/logo*", function (req, res, next) { + console.log("fired") + var buf = new Buffer(config.branding.values.logo, 'base64'); + res.writeHead(200, { + 'Content-Type': "image/png", + 'Content-disposition': 'attachment;filename=logo.png', + 'Content-Length': buf.length + }); + res.end(new Buffer(buf, 'binary')); + }); + + app.get('/footer.html', function (req, res) { + res.render('footer', config.branding.values); + }); + + module.exports = app; +}()); diff --git a/config.js b/config.js index 274fc8ec6..335bea900 100644 --- a/config.js +++ b/config.js @@ -1,8 +1,9 @@ (function (){ 'use strict'; - var session = require("express-session"), - RedisStore = require('connect-redis')(session) + var session = require("express-session") + , RedisStore = require('connect-redis')(session) + , redis = require("redis"); module.exports = { session: { @@ -18,6 +19,21 @@ secret: 'sooper secret', resave: false, saveUninitialized: true + }, + + redis_client: null, + branding: { + set: false, + values: { + name: "", + logo: "", + company: "", + street: "", + city: "", + zip: "", + state: "", + country: "", + } } }; }()); diff --git a/helpers/index.js b/helpers/index.js index 348e785db..de625fb3d 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -1,8 +1,11 @@ (function (){ 'use strict'; - var request = require("request"); - var helpers = {}; + var request = require("request") + , config = require("../config") + , fs = require('fs') + , redis = require("redis") + , helpers = {} /* Public: errorHandler is a middleware that handles your errors * @@ -11,7 +14,6 @@ * var app = express(); * app.use(helpers.errorHandler); * */ - helpers.errorHandler = function(err, req, res, next) { var ret = { message: err.message, @@ -53,10 +55,10 @@ /* Rewrites and redirects any url that doesn't end with a slash. */ helpers.rewriteSlash = function(req, res, next) { - if(req.url.substr(-1) == '/' && req.url.length > 1) - res.redirect(301, req.url.slice(0, -1)); - else - next(); + if(req.url.substr(-1) == '/' && req.url.length > 1) + res.redirect(301, req.url.slice(0, -1)); + else + next(); } /* Public: performs an HTTP GET request to the given URL @@ -102,5 +104,59 @@ return req.session.customerId; } + + + helpers.getBrandingConfig = function(callback) { + if(process.env.SESSION_REDIS) { + config.redis_client = redis.createClient("redis://session-db:6379"); + config.redis_client.on("error", function (err) { + console.log("Redis error encountered", err); + }); + + config.redis_client.on("end", function() { + console.log("Redis connection closed"); + }); + + if(process.env.DEFAULT_BRANDING) { + var image = "./public/img/weave-logo.png"; + config.branding.set = true; + config.branding.values.name = "weave"; + config.branding.values.logo = fs.readFileSync(image).toString("base64"); + callback(config.branding); + return + } else { + config.redis_client.get("branding_info", function(err, reply) { + if (reply !== null) { + config.branding.values = JSON.parse(reply); + config.branding.set = true; + callback(config.branding); + } else { + callback(config.branding); + } + }); + } + } else { + if (fs.existsSync("./branding.json")) { + config.branding.values = JSON.parse(fs.readFileSync('./branding.json')); + config.branding.set = true; + callback(config.branding); + } else { + callback(config.branding); + } + } + }; + + helpers.setBrandingConfig = function() { + if(process.env.SESSION_REDIS) { + if (config.redis_client.connected) { + config.redis_client.set("branding_info", JSON.stringify(config.branding.values)); + } + } else { + fs.writeFile("./branding.json", JSON.stringify(config.branding.values, null, 2), function(err) { + return err; + }); + } + } + module.exports = helpers; }()); diff --git a/package.json b/package.json index ad0d695f5..7155cc8a6 100644 --- a/package.json +++ b/package.json @@ -20,15 +20,19 @@ "dependencies": { "async": "^1.5.2", "body-parser": "^1.15.1", + "connect-busboy": "0.0.2", + "connect-redis": "^3.2.0", "cookie-parser": "^1.4.3", + "ejs": "^2.5.6", "express": "^4.13.4", + "express-busboy": "^6.0.1", "express-session": "^1.13.0", "finalhandler": "^0.4.1", - "request": "^2.72.0", - "serve-static": "^1.10.2", - "prom-client": "^6.3.0", "morgan": "^1.7.0", - "connect-redis": "^3.2.0" + "prom-client": "^6.3.0", + "redis": "^2.7.1", + "request": "^2.72.0", + "serve-static": "^1.10.2" }, "devDependencies": { "chai": "^3.5.0", diff --git a/public/img/cncf-logo.png b/public/img/cncf-logo.png new file mode 100644 index 000000000..2fe93ec41 Binary files /dev/null and b/public/img/cncf-logo.png differ diff --git a/public/img/logo.png b/public/img/logo.png deleted file mode 100644 index ef0521bc7..000000000 Binary files a/public/img/logo.png and /dev/null differ diff --git a/public/img/logo-small.png b/public/img/weave-logo.png similarity index 100% rename from public/img/logo-small.png rename to public/img/weave-logo.png diff --git a/public/welcome.html b/public/welcome.html new file mode 100644 index 000000000..b8eb72da6 --- /dev/null +++ b/public/welcome.html @@ -0,0 +1,154 @@ + + + +
+ + + + + + + + + +Weaveworks Ltd.
-
32 – 38 Scrutton Street
-
London
-
EC2A 4RQ
-
UK
+
<%= company %>
+
<%= street %>
+
<%= city %>
+
<%= state %> <%= zip %>
+
<%= country %>