Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Kienzler committed Sep 21, 2016
0 parents commit ffa7831
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
96 changes: 96 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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-*
110 changes: 110 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "ISC",
"peerDependencies": {
"bluebird": "^3.4.6",
"neat-base": "~1.x.x"
},
"dependencies": {
"redis": "^2.6.2"
}
}

0 comments on commit ffa7831

Please sign in to comment.