diff --git a/app.js b/app.js
index da63dd5..61860af 100644
--- a/app.js
+++ b/app.js
@@ -1,10 +1,10 @@
const express = require('express'),
- async = require('async'),
cors = require("cors"),
app = express(),
log = require("color-logs")(true, true, "Dryusdan.fr"),
fs = require("fs"),
path = require('path'),
+ promise = require('promise'),
hbs = require('hbs');
log.info("Application started and now preparing");
@@ -15,35 +15,46 @@ var corsOptions = {
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false
};
-
-hbs.registerHelper('list', function(tabs, options) {
-
- var out = '';
- for(var x in tabs){
- for(var i in tabs[x]){
- out = out + "" + options.fn(tabs[x][i]) + "";
- }
- }
- return out;
-});
-
-
+/**
+ * Create menu html
+ */
const config = require("./config.json");
var Template = require("./classes/Template.class");
class App {
constructor(rootPath, port){
+ /**
+ * Listen to http://0.0.0.0:port
+ */
app.listen(port);
log.info("HTTP server listening on port", port);
log.info("Application ready");
var template = new Template();
app.get("/", function(req, res){
- template.render(function(tplData){
+ Promise.all([
+ template.getNav(),
+ template.getHome()
+ ]).then(data => {
+ var nav = data[0];
+ var content = data[1];
res.setHeader("Content-type", "text/html");
+ var tplData = {
+ "blogName": config.blogName,
+ "title": 'Accueil',
+ "nav": nav,
+ "content": content
+ };
res.render('template.hbs', tplData);
+ }).catch(err => {
+ console.error(err);
+ res.setHeader("Content-type", "text/plain");
+
+ res.end("erreur");
});
+
+
});
app.get("/:slug", function(req, res){
res.setHeader("Content-type", "text/plain");
@@ -60,14 +71,4 @@ class App {
}
-new App("/", config.appPort);
-
-/** Preparing cache **/
-/*async.waterfall([
- function generateCache(callback){
- callback;
- }
-], function (err){
-
- }
-);*/
\ No newline at end of file
+new App("/", config.appPort);
\ No newline at end of file
diff --git a/classes/Article.class.js b/classes/Article.class.js
new file mode 100644
index 0000000..d7bd37e
--- /dev/null
+++ b/classes/Article.class.js
@@ -0,0 +1,13 @@
+const log = require('color-logs')(true, true, "Articles.class.js"),
+ fs = require("fs");
+
+var title,
+ author,
+ content,
+ date,
+ slug;
+
+class Article {
+}
+
+module.exports = Article;
diff --git a/classes/Articles.class.js b/classes/Articles.class.js
deleted file mode 100644
index 6c6130d..0000000
--- a/classes/Articles.class.js
+++ /dev/null
@@ -1,30 +0,0 @@
-const log = require('color-logs')(true, true, "Articles.class.js"),
- fs = require("fs");
-
-
-class Articles {
-
- constructor(r, rtdbConn){
- this.rdbConn = rtdbConn;
- this.db = r;
- }
-
- listHome(callback) {
- // r.db("dryusdan").table("articles").filter(r.row('time')).filter(r.row('title')).limit(1) get article mis en avant !
- // r.db("dryusdan").table("articles").filter(r.row('time')).limit(9)
-
- this.db.table('articles').fil.run(this.rdbConn, function(err, cursor){
- if(err) throw err;
- cursor.toArray(function(err, result){
- if(err) throw err;
- callback(result);
- });
- });
- }
-
- list(ctg, callback){
-
- }
-}
-
-module.exports = Articles;
diff --git a/classes/Categorie.class.js b/classes/Categorie.class.js
new file mode 100644
index 0000000..8cf5d38
--- /dev/null
+++ b/classes/Categorie.class.js
@@ -0,0 +1,20 @@
+const log = require('color-logs')(true, true, "Categories.class.js"),
+ path = require('path'),
+ fs = require("fs");
+
+var title,
+ slug,
+ position;
+
+class Categorie {
+
+ /**
+ *
+ * @param {string} folder where located categories
+ * @returns {nm$_Categorie.class.Categorie}
+ */
+ constructor(folder){
+ }
+}
+
+module.exports = Categorie;
\ No newline at end of file
diff --git a/classes/Categories.class.js b/classes/Categories.class.js
deleted file mode 100644
index 78d35b1..0000000
--- a/classes/Categories.class.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const log = require('color-logs')(true, true, "Categories.class.js"),
- path = require('path'),
- fs = require("fs");
-
-
-class Categories {
-
- list(callback) {
- const srcpath = 'ressources';
- var listDir = fs.readdirSync(srcpath).filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory());
- var listCtg = {};
- for(var x = 0; x < listDir.length; x++){
- var params = JSON.parse(fs.readFileSync(srcpath+'/'+listDir[x]+'/params.json', 'utf8'));
- listCtg[x] = [{'title': params.title, 'slug': listDir[x]}]
- }
- callback(listCtg);
- }
-}
-
-module.exports = Categories;
\ No newline at end of file
diff --git a/classes/Template.class.js b/classes/Template.class.js
index 2c7cbd1..adbd704 100644
--- a/classes/Template.class.js
+++ b/classes/Template.class.js
@@ -1,81 +1,75 @@
-const log = require('color-logs')(true, true, "Template.class.js"),
- fs = require("fs"),
- Handlebars = require('handlebars'),
- config = require('../config.json');
+const log = require('color-logs')(true, true, "Template.class.js"),
+ fs = require("fs"),
+ hbs = require('handlebars'),
+ promise = require('promise'),
+ config = require('../config.json');
+
+var Categorie = require('../classes/Categorie.class.js');
+var Article = require('../classes/Article.class.js');
+
+hbs.registerHelper('listNav', function(tabs, options) {
+ var out = '';
+ for(var x in options['data']['root']){
+ console.log(x);
+ out = out + "" + options.fn(options['data']['root'][x]) + "";
+
+ }
+ return out;
+});
+
+hbs.registerHelper('listArticle', function(tabs, options) {
+ var i = 1;
+ var out = '';
+ for(var x in options['data']['root']){
+
+ out = out + "" + options.fn(options['data']['root'][x]) + "";
+ if(i == 3){
+ i= 1;
+ out = out + "
";
+ }
+ else{
+ i++;
+ }
+ }
+ return out;
+});
-var Categories = require('../classes/Categories.class.js');
class Template {
+ /**
+ *
+ * @returns {Promise} the navbar in html compiled
+ */
- getNav(callback){
- var categories = new Categories();
- categories.list(function(ctgs){
- callback(ctgs);
- });
+ getNav(){
+ return new Promise((resolve, reject) => {
+ fs.readFile('./views/_nav.hbs', 'utf-8', (error, source) => {
+ const articles = require("../ressources/categories.json");
+ hbs.registerHelper('nav', (nav) =>{
+ return nav;
+ });
+ var template = hbs.compile(source);
+ var html = template(articles);
+ //console.log(html);
+ resolve(html);
+ });
+ });
}
- render(callback){
- this.getNav(function(nav){
- var tplData = {
- "blogName": config.blogName,
- "title": 'Accueil',
- "nav": nav,
- };
- callback(tplData);
- })
-
-
-
- //return tplData;
+ getHome(){
+ return new Promise((resolve, reject) => {
+ fs.readFile('./views/articles/list.hbs', 'utf-8', (error, source) => {
+ const data = require('../ressources/lastArticles.json');
+ hbs.registerHelper('articles', (articles) =>{
+ return articles;
+ });
+ var template = hbs.compile(source);
+ var html = template(data);
+ console.log(html);
+ resolve(html);
+ });
+ });
}
-
-
- generateHTML(cb){
-
- console.log("passage !");
- cb;
- }
-
- /*generateTemplate(){
- var categories = new Categories(this.db, this.rdbConn);
- var tplSource = fs.readFileSync("./view/template.hbs", "utf8");
-
- categories.list(function(ctgs, source){
- console.log(ctgs);
- });
-
- var template = Handlebars.compile(tplSource);
- var tplData = {
- "blogName": config.blogName,
- "title": title,
- "nav": '',
- };
- return template(tplData);
-
- }*/
-
- /*generateHTML(ctgs){
- var tplData = {
- "blogName": config.blogName,
- "title": title,
- "nav": ctgs,
- };
- console.log(generateSource("./view/template.hbs", "utf8", tplData));
- }
-
- static generateSource(source, data){
- var tplSource = fs.readFileSync(source);
- var template = Handlebars.compile(tplSource);
- return template(data);
- }
-
- returnHTML(source, data, title){
- var categories = new Categories(this.db, this.rdbConn);
- categories.list((function(ctgs){
- return generateHTML(ctgs);
- })(generateHTML(ctgs))); //cette methode n'est pas appelé
-
- }*/
}
diff --git a/config.json b/config.json
index 593e145..45aee91 100644
--- a/config.json
+++ b/config.json
@@ -1,6 +1,4 @@
{
"appPort": "8888",
- "rtdbHost": "ssh.dryusdan.fr",
- "rtdbPort": "28015",
"blogName": "Dryusdan"
}
diff --git a/package-lock.json b/package-lock.json
index 7e63068..06001cc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -19,14 +19,15 @@
"version": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
+ "asap": {
+ "version": "2.0.5",
+ "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.5.tgz",
+ "integrity": "sha1-UidltQw1EEkOUtfc/ghe+bqWlY8="
+ },
"async": {
"version": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo="
},
- "bluebird": {
- "version": "https://registry.npmjs.org/bluebird/-/bluebird-2.11.0.tgz",
- "integrity": "sha1-U0uQM8AiyVecVro7Plpcqvu2UOE="
- },
"camelcase": {
"version": "https://registry.npmjs.org/camelcase/-/camelcase-1.2.1.tgz",
"integrity": "sha1-m7UwTS4LVmmLLHWLCKPqqdqlijk=",
@@ -231,6 +232,11 @@
"version": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
},
+ "promise": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/promise/-/promise-8.0.0.tgz",
+ "integrity": "sha512-eT7BkLTa15CBmuufE1MPVIdyYpLKbq5dSEtJicDKtPilr3gJuTPkQ+LDuZ363Bxp5pfpL218u4udvYnnIjV/Ow=="
+ },
"proxy-addr": {
"version": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-1.1.4.tgz",
"integrity": "sha1-J+VF9pYKRKYn2bREZ+NcG2tM4vM="
@@ -247,10 +253,6 @@
"version": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc="
},
- "rethinkdb": {
- "version": "https://registry.npmjs.org/rethinkdb/-/rethinkdb-2.3.3.tgz",
- "integrity": "sha1-PcZYbiL6HavuDSVOZL0ON5+tL3I="
- },
"right-align": {
"version": "https://registry.npmjs.org/right-align/-/right-align-0.1.3.tgz",
"integrity": "sha1-YTObci/mo1FWiSENJOFMlhSGE+8=",
diff --git a/package.json b/package.json
index 11b531e..bf67089 100644
--- a/package.json
+++ b/package.json
@@ -10,14 +10,16 @@
],
"author": "dryusdan",
"scripts": {
+ "dev": "nodemon app.js",
"start": "node app.js"
},
"contributors": [],
"dependencies": {
- "handlebars": "4.0.6",
- "express": "4.15.2",
- "cors": "2.8.1",
"color-logs": "0.6.1",
- "hbs": "4.0.1"
+ "cors": "2.8.1",
+ "express": "4.15.2",
+ "handlebars": "4.0.6",
+ "hbs": "4.0.1",
+ "promise": "^8.0.0"
}
}
diff --git a/view/template.hbs b/view/template.hbs
deleted file mode 100644
index 5ed5558..0000000
--- a/view/template.hbs
+++ /dev/null
@@ -1,35 +0,0 @@
-
-
-
-
-
{{title}} - {{blogName}}
-
-
-
-
-
-{{content}}
-
-
-
-
-
-
\ No newline at end of file
diff --git a/views/_nav.hbs b/views/_nav.hbs
new file mode 100644
index 0000000..7eef073
--- /dev/null
+++ b/views/_nav.hbs
@@ -0,0 +1,10 @@
+
+
+ {{#listNav data}}{{/listNav}}
+
+
+
diff --git a/views/articles/list.hbs b/views/articles/list.hbs
new file mode 100644
index 0000000..b5b1243
--- /dev/null
+++ b/views/articles/list.hbs
@@ -0,0 +1,25 @@
+
+
+
+
+ {{#listArticle articles}}
+
+ {{/listArticle}}
+
+
+
+
+
\ No newline at end of file
diff --git a/views/template.hbs b/views/template.hbs
index 10a3552..19076d7 100644
--- a/views/template.hbs
+++ b/views/template.hbs
@@ -8,15 +8,7 @@
-
- {{#list nav}}{{/list}}
-
-
-
+ {{{nav}}}
-{{content}}
+{{{content}}}