From b6c758f2fc2cb502ed194f2700e033564f2521f4 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Tue, 22 Sep 2015 12:06:13 +0800 Subject: [PATCH] Added server option "useCdn", use template statement to route resources' source --- README.md | 1 + app.js | 4 +--- config.js | 1 + lib/response.js | 13 +++++++++++ public/{index.html => index.ejs} | 37 +++++++++++++++++++++++--------- public/views/foot.ejs | 9 ++++++++ public/views/head.ejs | 8 ++++++- public/views/pretty.ejs | 14 ++++++++++-- 8 files changed, 71 insertions(+), 16 deletions(-) rename public/{index.html => index.ejs} (87%) diff --git a/README.md b/README.md index ffefafa2f..aa051e9bd 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Server-side config.js settings | usessl | `true` or `false` | set to use ssl | | urladdport | `true` or `false` | set to add port on oauth callback url | | debug | `true` or `false` | set debug mode, show more logs | +| usecdn | `true` or `false` | set to use CDN resources or not | | version | `0.3.2` | currnet version, must match same var in client side `index.js` | | alloworigin | `['localhost']` | domain name whitelist | | sslkeypath | `./cert/client.key` | ssl key path | diff --git a/app.js b/app.js index 628f46315..a19715fe8 100644 --- a/app.js +++ b/app.js @@ -136,9 +136,7 @@ app.set('views', __dirname + '/public'); //set render engine app.engine('html', ejs.renderFile); //get index -app.get("/", function (req, res, next) { - res.render("index.html"); -}); +app.get("/", response.showIndex); //get status app.get("/status", function (req, res, next) { realtime.getStatus(function (data) { diff --git a/config.js b/config.js index cfd9f20d2..b04066da1 100644 --- a/config.js +++ b/config.js @@ -11,6 +11,7 @@ var urladdport = true; //add port on getserverurl var config = { debug: true, + usecdn: false, version: '0.3.2', domain: domain, alloworigin: ['add here to allow origin to cross'], diff --git a/lib/response.js b/lib/response.js index b8cfab179..1f7fbd547 100644 --- a/lib/response.js +++ b/lib/response.js @@ -34,6 +34,7 @@ var response = { showFeatures: showFeatures, showNote: showNote, showPublishNote: showPublishNote, + showIndex: showIndex, noteActions: noteActions, publishNoteActions: publishNoteActions }; @@ -55,6 +56,18 @@ function responseError(res, code, detail, msg) { res.end(); } +function showIndex(req, res, next) { + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + var template = config.indexpath; + var content = ejs.render(fs.readFileSync(template, 'utf8'), { + useCDN: config.usecdn + }); + res.write(content); + res.end(); +} + function responseHackMD(res, noteId) { if (noteId != config.featuresnotename) { if (!Note.checkNoteIdValid(noteId)) { diff --git a/public/index.html b/public/index.ejs similarity index 87% rename from public/index.html rename to public/index.ejs index 829c04461..dbc5e268f 100644 --- a/public/index.html +++ b/public/index.ejs @@ -15,13 +15,19 @@ - + <% if(useCDN) { %> + + + <% } else { %> + + + + <% } %> - @@ -203,21 +209,32 @@ - - + <% if(useCDN) { %> + + + - - + + + + + <% } else { %> + + + + + + + + + <% } %> - - - - + \ No newline at end of file diff --git a/public/views/foot.ejs b/public/views/foot.ejs index b545841f2..0f568643a 100644 --- a/public/views/foot.ejs +++ b/public/views/foot.ejs @@ -1,9 +1,18 @@ +<% if(useCDN) { %> + + + + + + +<% } else { %> +<% } %> diff --git a/public/views/head.ejs b/public/views/head.ejs index 0518ebc71..816c2a268 100644 --- a/public/views/head.ejs +++ b/public/views/head.ejs @@ -7,8 +7,15 @@ <%- title %> +<% if(useCDN) { %> + + + +<% } else { %> + +<% } %> @@ -20,7 +27,6 @@ - diff --git a/public/views/pretty.ejs b/public/views/pretty.ejs index cc4f80d27..0004bb7c9 100644 --- a/public/views/pretty.ejs +++ b/public/views/pretty.ejs @@ -10,9 +10,14 @@ <%- title %> - - + + <% if(useCDN) { %> + + + <% } else { %> + + <% } %> @@ -45,8 +50,13 @@ +<% if(useCDN) { %> + + +<% } else { %> +<% } %>