diff --git a/.circleci/config.yml b/.circleci/config.yml
index fb8f597..7100338 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -7,12 +7,12 @@ jobs: # a collection of steps
steps: # a collection of executable commands
- checkout # special step to check out source code to working directory
- run:
- name: update-npm
+ name: Update npm
command: 'sudo npm install -g npm@latest'
- restore_cache: # special step to restore the dependency cache
key: dependency-cache-{{ checksum "package.json" }}
- run:
- name: install-npm-wee
+ name: Install dependencies
command: npm install
- save_cache: # special step to save the dependency cache
key: dependency-cache-{{ checksum "package.json" }}
@@ -20,7 +20,7 @@ jobs: # a collection of steps
- ./node_modules
- run: # run tests
name: Start server in background
- command: node server.js --port=8880
+ command: node server.js --port=8880 --loglevel=debug
background: true
- run:
name: Valid HTTP Request to server
@@ -30,13 +30,5 @@ jobs: # a collection of steps
command: 'wget --retry-connrefused --waitretry=1 --timeout=15 -t 10 http://localhost:8880/invalid || :'
- run:
name: Run server without request
- command: node server.js --test --port=8888
- - store_artifacts: # special step to save test results as as artifact
- path: test-results.xml
- prefix: tests
- - store_artifacts:
- path: coverage
- prefix: coverage
- - store_test_results: # special step to upload test results for display in Test Summary
- path: test-results.xml
+ command: node server.js --test --port=8888 --loglevel=debug
# See https://circleci.com/docs/2.0/deployment-integrations/ for deploy examples
diff --git a/README.md b/README.md
index 1fe49b1..13b9ac3 100644
--- a/README.md
+++ b/README.md
@@ -1,45 +1,93 @@
-rcn-frontserver
+rcn-frontserver [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![CircleCI](https://circleci.com/gh/Trivernis/rcn-frontserver.svg?style=shield)](https://circleci.com/gh/Trivernis/rcn-frontserver) [![CodeFactor](https://www.codefactor.io/repository/github/trivernis/rcn-frontserver/badge)](https://www.codefactor.io/repository/github/trivernis/rcn-frontserver)
====
+This is the front-end web-server of the Raspberry Communication Network *(that is still in development)*.
-About
+Contents
----
-This is the front-web-server of the Raspberry pi Communication network.
+1. [Requirements](#requirements)
+2. [Getting Started](#getting-started)
+3. [Arguments](#arguments)
+4. [Configuration](#configuration)
+ 1. [Routes for each type of file](#routes-for-each-type-of-file)
+ 2. [Mounting external folders](#mounting-external-folders)
+5. [Cool Features](#cool-features)
+6. [Roadmap](#roadmap)
-Modules
+Requirements
----
-**NodeJS**
-- winston
-- pkg
-- perfy
-- jsdom
-- winston-daily-rotate-file
-- args-parser
-- node-sass
-
-**Embedded in the html-file**
-- JQuery
-- Vue.js
-
-Details
+You need to have Node.js installed to run this script. Just in case update your npm with the command:
+```commandline
+npm install -g npm@latest
+```
+
+Getting started
+----
+ To install the dependencies listed above, run the following from the commandline.
+```commandline
+npm install
+```
+Now you can run the server by typing:
+```commandline
+node server.js
+```
+
+Arguments
+----
+Just type `node server.js -h` or
+```commandline
+Usage: server.js [options]
+
+Options:
+ -h, --help show this help message and exit
+ -p, --port define the port for the server to run on
+ --loglevel define at which level the console should output
+ --test start the server and exit after 10 seconds
+```
+
+Configuration
----
-This web server uses folders for every type of file (depending on the file's ending) and routes to the folders. There are also global files, that are implemented in every html via dom-manipulation. The behaviour for every file extension can be configured in the config.json This server uses vue.js and jquery to make the website as interactive, as possible. This server works with https. To run it. it needs to have access to the folder .ssh in the same directory where a valid cert.pem and a valid key.pem is are existing. Specific directories can be mounted via the mounting parameter. The pattern is:
-``` json
+*This configuration applies to the server.json.*
+
+### Routes for each type of file
+In the server.json all common web file-types have their own configuration at **routes**.
+```json
+{
+ "routes": {
+ ".html": {
+ "path": "./res/html/",
+ "mime": "text/html"
+ }
+ }
+}
+```
+In the configuration a path and mime-type is defined. When the server starts recieves a request, it reads the configuration and searches for the file in the specific route. If the file exists it gets transportet with the defined mime-type. The type of the file is determined with the file's extension.
+
+### Mounting external folders
+It is also possible to mount additional folders into the web-application by defining them at **mounts**. Mounts are more important than routes so when the server recieves a request, it first looks if the request-url has a mounted directory (or file).
+
+```json
{
"mounts": [
{
- "mount": "mountpath",
- "path": "path"
+ "path": "./path/to/directory/",
+ "mount": "/mount/"
}
]
}
-
-You can embed sass files as link elements in html-pages. The sass files will be preprocessed to css before send to clients. The result will also be cached and the sass file will be watched for changes. If changes occur, the file will be preprocessed anew on the next request to the file. If not, the cached file will be send. HTML-files will also be cached because they are also manipulated by the preprocessor. All cached files are watched for changes. I.e. if changes to the file are detected the caching function isCached returns false.
```
+The path Attribute defines the mounted directory (of file) on the local filesystem. The mount attribute defines where the directory is mounted in the web-application. The mount-path supports RegEx strings (the value is interpreted as a regular Expression by default).
+
+Cool features
+---
+### Embedding SASS-Stylesheets, Vue.js and jQuery
+This server automatically embeds Vue.js, jQuery and the *script.js* and *style.sass* stored in *./glob*. And yeah, that's right. You can embed .sass-files. When encountering a .sass file, the server compiles it into .css. The result will then be stored in the *./.cache* directory. The server also detects changes on the original file and refreshes the cached file when the change was detected. The refreshing only happens, when the file was requested.
Roadmap
----
-**Done**
-- SASS-Files and caching of preprocessed files
-**ToDo**
-- Mounting of folders or files on other locations (by using the config.json)
-- a package.json because it seems to be important nowadays
+- [x] SASS-Files and caching of preprocessed files
+- [x] Mounting of folders or files on other locations (by using the config.json)
+- [x] A package.json because it seems to be important nowadays
+- [x] HTTP-Statuscode integration with specific pages
+- [ ] Handling POST-Methods
+- [ ] Integrating Backend-Applications via a standardized JSON-interface
+- [ ] Change from JSON to CSON and consider this in the standardized interface
\ No newline at end of file
diff --git a/config/server.json b/config/server.json
index 5fcc35d..669267f 100644
--- a/config/server.json
+++ b/config/server.json
@@ -1,48 +1,48 @@
{
- "errors": {
- "403": "./glob/errors/403.html",
- "404": "./glob/errors/404.html"
- },
- "routes": {
- ".html": {
- "path": "./res/html/",
- "mime": "text/html"
- },
- ".htm": {
- "path": "./res/html/",
- "mime": "text/html"
- },
- ".js": {
- "path": "./res/scripts/",
- "mime": "text/javascript"
- },
- ".css": {
- "path": "./res/css/",
- "mime": "text/css"
- },
- ".json": {
- "path": "./res/data/",
- "mime": "text/plain"
- },
- ".ico": {
- "path": "./res/img/",
- "mime": "image/x-icon"
- },
- ".jpg": {
- "path": "./res/img/",
- "mime": "image/jpeg"
- },
- ".png": {
- "path": "./res/img/",
- "mime": "image/png"
- },
- ".ttf": {
- "path": "./res/fonts/",
- "mime": "font/opentype"
- },
- ".sass": {
- "path": "./res/sass",
- "mime": "style/css"
- }
- }
+ "errors": {
+ "403": "./glob/errors/403.html",
+ "404": "./glob/errors/404.html"
+ },
+ "routes": {
+ ".html": {
+ "path": "./res/html/",
+ "mime": "text/html"
+ },
+ ".htm": {
+ "path": "./res/html/",
+ "mime": "text/html"
+ },
+ ".js": {
+ "path": "./res/scripts/",
+ "mime": "text/javascript"
+ },
+ ".css": {
+ "path": "./res/css/",
+ "mime": "text/css"
+ },
+ ".json": {
+ "path": "./res/data/",
+ "mime": "text/plain"
+ },
+ ".ico": {
+ "path": "./res/img/",
+ "mime": "image/x-icon"
+ },
+ ".jpg": {
+ "path": "./res/img/",
+ "mime": "image/jpeg"
+ },
+ ".png": {
+ "path": "./res/img/",
+ "mime": "image/png"
+ },
+ ".ttf": {
+ "path": "./res/fonts/",
+ "mime": "font/opentype"
+ },
+ ".sass": {
+ "path": "./res/sass",
+ "mime": "style/css"
+ }
+ }
}
diff --git a/server.js b/server.js
index 18c1fae..a66c3ff 100644
--- a/server.js
+++ b/server.js
@@ -1,5 +1,5 @@
// requirement
-var protocoll = require('https');
+let protocoll = require('https'); // if there are no certificates protocoll will be set to require(http)
const fs = require('fs'),
urlparse = require('url'),
perfy = require('perfy'),
@@ -153,12 +153,12 @@ function getMount(uri) {
if (mount.mount && mount.path) { // if the mount has the mount parameter and the path parameter
let regEx = RegExp(mount.mount); // create a regex from the mount
if (uri.match(regEx)) { // if there is a match
- return uri.replace(regEx, mount.path); // returnthe modified uri
+ logger.debug(regEx.toString() + " returns a result");
+ return uri.replace(regEx, mount.path); // return the modified uri
}
}
}
}
- return false;
}
/**
@@ -170,9 +170,27 @@ function cleanup() {
logger.end(); // let the logger finish all operations
}
+/**
+ * Offers help when the function is called via the --help flag
+ */
+function help() {
+ console.log("\nUsage: server.js [options]\n\n" +
+ "Options:\n" +
+ " -h, --help show this help message and exit\n" +
+ " --port define the port for the server to run on\n" +
+ " --loglevel define at which level the console should output\n" +
+ " --test start the server and exit after 10 seconds\n");
+ process.exit(0);
+}
+
// Executing the main function
if (typeof require !== 'undefined' && require.main === module) {
+ // do arg specific operations
+ if (args.help || args.h) help();
+ if (args.test) setTimeout(() => process.exit(0), 10000); // if in testing mode, exit after 10 seconds
+
utils.Cleanup(cleanup); // set the cleanup function
+
logger.exceptions.handle(
new winston.transports.File({
filename: './.log/frontserver-exceptions.log'
@@ -184,9 +202,7 @@ if (typeof require !== 'undefined' && require.main === module) {
cert: fs.readFileSync('.ssh/cert.pem') // the certificate-file
};
} else protocoll = require('http'); // if no certs could be found start the server as http-server
+
logger.info("Starting up... "); // log the current date so that the logfile is better to read.
- if (args.test) {
- setTimeout(() => process.exit(0), 10000); // if in testing mode, exit after 10 seconds
- }
- main();
+ main(); // execute the main function
}