From ffa78310a9a7d037574ddcc72a87851412671c5c Mon Sep 17 00:00:00 2001 From: Dominik Kienzler Date: Wed, 21 Sep 2016 17:08:34 +0200 Subject: [PATCH] initial commit --- .gitignore | 96 ++++++++++++++++++++++++++++++++++++++++++++ index.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 18 +++++++++ 3 files changed, 224 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0058ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,96 @@ +#################### +### PROJECT ######## +#################### + +node_modules + +#################### +### IDE ############ +#################### + +# PhpStorm +.idea + +# Eclipse +.project + +# CodeKit +config.codekit + +# NetBeans +nbproject + +# SubLime +*.sublime-workspace +*.sublime-project + +# TextMate +*.tmproj +*.tmproject + +#################### +### OSX ############ +#################### + +# Global +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +#################### +### Windows ######## +#################### + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +################## +### Linux ######## +################## + +# Global +*~ + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* diff --git a/index.js b/index.js new file mode 100644 index 0000000..1a788e5 --- /dev/null +++ b/index.js @@ -0,0 +1,110 @@ +"use strict"; + +var Application = require("neat-base").Application; +var Module = require("neat-base").Module; +var Tools = require("neat-base").Tools; +var redis = require("redis"); +var Promise = require("bluebird"); +var crypto = require('crypto'); +var fs = require('fs'); + +module.exports = class Api extends Module { + + static defaultConfig() { + return { + elementsModuleName: "elements" + } + } + + init() { + return new Promise((resolve, reject) => { + this.log.debug("Initializing..."); + return this.loadStaticPages().then(() => { + return resolve(this); + }); + }); + } + + start() { + return new Promise((resolve, reject) => { + this.log.debug("Starting..."); + return resolve(this); + }); + } + + stop() { + return new Promise((resolve, reject) => { + this.log.debug("Stopping..."); + return resolve(this); + }); + } + + loadStaticPages() { + return new Promise((resolve, reject) => { + var rootDir = Application.config.config_path + "/pages"; + + if (!fs.existsSync(rootDir)) { + fs.mkdirSync(rootDir); + } + + var files = fs.readdirSync(rootDir); + var pages = {}; + + for (var i = 0; i < files.length; i++) { + var file = files[i]; + + if (file.indexOf(".json") === -1) { + continue; + } + + var config = Tools.loadCommentedConfigFile(rootDir + "/" + file); + for (var key in config) { + if (pages[key]) { + this.log.warn("Page " + key + " duplicated"); + } + + pages[key] = config[key]; + } + } + + this.pages = pages; + + resolve(); + }); + } + + getPageFromRequest(req) { + for (var key in this.pages) { + var page = this.pages[key]; + if (new RegExp(page.path).test(req.path)) { + return page; + } + } + + return this.pages[404]; + } + + getPageJson(req) { + return new Promise((resolve, reject) => { + var page = this.getPageFromRequest(req); + + if(!page) { + return resolve({ + status: 404 + }); + } + + var status = page.status || 200; + + return Application.modules[this.config.elementsModuleName].dispatchElementsInSlots(page.slots, req).then((dispatchedSlots) => { + resolve({ + layout: page.layout || "default", + url: req.path, + status: status, + meta: req.meta, + data: dispatchedSlots + }); + }, reject); + }); + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..146d5c8 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "neat-api", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Dominik Kienzler ", + "license": "ISC", + "peerDependencies": { + "bluebird": "^3.4.6", + "neat-base": "~1.x.x" + }, + "dependencies": { + "redis": "^2.6.2" + } +}