diff options
| -rw-r--r-- | Report/report.md | 24 | ||||
| -rwxr-xr-x | getmepostgres.sh | 45 | ||||
| -rw-r--r-- | package.json | 4 | ||||
| -rwxr-xr-x | runmepostgres.sh | 7 | ||||
| -rw-r--r-- | src/FileRouter.ts | 128 | ||||
| -rw-r--r-- | src/app.ts | 5 | ||||
| -rw-r--r-- | src/serverConfig.ts | 52 | ||||
| -rw-r--r-- | test/nodeserver.test.ts | 6 |
8 files changed, 231 insertions, 40 deletions
diff --git a/Report/report.md b/Report/report.md new file mode 100644 index 0000000..664b830 --- /dev/null +++ b/Report/report.md @@ -0,0 +1,24 @@ +# Junior Honours Project 2017 - 2018 +> cjd24 - Calum +> trh - Tom +> har4 - Hafeez +> jl247 - Josh +> jw255 - Johannes + + +### Requirements Overview: + + +### Design Choices: +- Scrum + +- Database + +- Server + + + +### Year Wide Protocol Compilation: + + +### Scrum: diff --git a/getmepostgres.sh b/getmepostgres.sh new file mode 100755 index 0000000..05db59d --- /dev/null +++ b/getmepostgres.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +echo "> Checking for postgres at ~/pg"; +if [ -d "~/pg" ]; then + echo "Postgres Directory exists, nevermind..."; + exit 1; +fi + +echo "> Making postgres directory at ~/pg"; +mkdir ~/pg; +currentDir=$(pwd); + +echo "> Chaning dir to ~/pg"; +cd ~/pg; + +echo "> Cloning Repo"; +git clone https://github.com/postgres/postgres.git; + +echo "> Checking out to version 10"; +cd ./postgres; +git reset --hard REL_10_0; + +echo "> Configuring"; +sleep 2; +./configure --prefix=$HOME/pg/postgres/ --with-python PYTHON=/usr/bin/python2.7; + + +read -p "Press [Enter] key to Build Postgres..."; +echo "> Building Postgres"; +make -j 5; + +read -p "Press [Enter] key to Install Postgres..."; +echo "> Installing Postgres"; +make install; + +echo "> Creating the Database"; +~/pg/postgres/bin/initdb -D ~/pg/postgres/data/; + +echo ""; +echo ""; +echo ""; +echo ""; +echo ""; +echo "> Completed the installation of Postgres. Please use `./runmepostgres.sh` to run."; +cd $currentDir; diff --git a/package.json b/package.json index f40379b..e26c237 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,9 @@ "body-parser": "^1.18.2", "express": "^4.16.2", "morgan": "^1.9.0", + "pg": "^7.4.1", "ts-node": "^3.3.0", - "typescript": "^2.7.1" + "typescript": "^2.7.1", + "xlsx": "^0.11.19" } } diff --git a/runmepostgres.sh b/runmepostgres.sh new file mode 100755 index 0000000..fb68e5e --- /dev/null +++ b/runmepostgres.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +echo "> Starting Server"; +~/pg/postgres/bin/pg_ctl -D ~/pg/postgres/data/ -l POSTGRES_logfile start; + +echo "> Conecting as admin user: " + ${USER}; +~/pg/postgres/bin/psql -U ${USER} postgres; diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 72c54bc..19a9126 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -1,4 +1,5 @@ import {Router, Request, Response, NextFunction} from 'express'; +var serverConfig = require('./serverConfig'); const Files = null;//require('../data'); @@ -15,7 +16,7 @@ export class FileRouter { /* * Returns the supported Protocols - * + * * The server's unwrapped response MUST match: exactly({ @@ -24,11 +25,15 @@ export class FileRouter { }) */ public getProtocols(req: Request, res: Response, next: NextFunction){ + var returnValue = {}; + returnValue['supported'] = ["BE50"]; + returnValue['required'] = "BE01"; + res.send(JSON.stringify(returnValue)); } /* * Logging Endpoint for all components: - * + * * with a request matching array({ @@ -38,13 +43,14 @@ export class FileRouter { }) */ public postLog(req: Request, res: Response, next: NextFunction){ + res.status(501).send({error : "logging_not_enabled"}); } /* * Clients authorised as a user with the admin privilege can access - * + * * http://backend.endpoint/log?before=<datetime>&after=<datetime>&level=<level> - * + * * the server response should match array({ @@ -56,13 +62,14 @@ export class FileRouter { }) */ public getLog(req: Request, res: Response, next: NextFunction){ + res.status(501).send({error : "logging_not_enabled"}); } /* * Any authenticated client can access - * + * * http://backend.endpoint/properties - * + * * the server SHOULD respond with a list of properties, with the unwrapped response matching array({ @@ -79,13 +86,14 @@ export class FileRouter { }) */ public getProperties(req: Request, res: Response, next: NextFunction){ + res.status(200).send(serverConfig.default); } /* * To update properties the client POSTs to - * + * * http://backend.endpoint/properties?action=update - * + * * with body array({ @@ -93,12 +101,41 @@ export class FileRouter { "value": alternative([string, integer, boolean]) }) */ - public postProperties(req: Request, res: Response, next: NextFunction){ - } + public postProperties(req: Request, res: Response, next: NextFunction){ + var success = true; + + //For each property to update + for (var prop of req.body) { + + //Check that the property has and id and a value + if (!prop.hasOwnProperty("id") || !prop.hasOwnProperty("value")) { + res.status(400).send({error : "invalid_request", error_description : "A supplied property is missing an id or value"}); + success = false; + break; + } + + //Find out if the property exists in the config and if so, at which index + var index = serverConfig.propertyExists(prop.id); + + //check some of the properties values to ensure that it can be updated + if (index == -1 || serverConfig.default[index].read_only) { /*TODO Implement an or, using Tom's type checker to ensure the value is "valid" based on the type of the property */ + var description = index == -1 ? "Property not found" : "Property is Read_Only"; + res.status(400).send({error : "invalid_property", error_data : prop.id, error_description : description}); + success = false; + break; + } + + //make the update to the property + serverConfig.default[index].value = prop.value; + } + + //tell the client that everything was okay; + if (success) res.status(200).send(); + } /* * Listing available privileges - * + * * And the server's response, unwrapped, must match array({ @@ -108,12 +145,13 @@ export class FileRouter { }) */ public getUserPriveleges(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * Listing users - * - * If the client is authorised as a user with the admin privilege + * + * If the client is authorised as a user with the admin privilege * the server MUST respond with a list of users, with the unwrapped response matching array({ @@ -130,19 +168,22 @@ export class FileRouter { }) */ public getUsers(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); + } /* * expose additional configuration options for individual users - * - * + * + * */ public getUserProperties(req: Request, res: Response, next: NextFunction, user?: String){ + res.status(500).send(); } /* * Listing a particular User - * + * * If the client is authorised as a user with the admin privilege the server MUST respond with details of the particular user if they exist (unwrapped response): { @@ -159,18 +200,20 @@ export class FileRouter { } */ public getUsername(req: Request, res: Response, next: NextFunction, user?: String){ + res.status(500).send(); } public getCurUser(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); let curUser : String = ""; this.getUsername(req, res, next, curUser); } /* * Updating the current user - * + * * http://backend.endpoint/current_user?action=update - * + * * with body matching { @@ -182,17 +225,18 @@ export class FileRouter { "private_user_metadata": optional(metadata), "public_admin_metadata": not_present, "private_admin_metadata": not_present - } + } */ public postCurUser(req: Request, res: Response, next: NextFunction){ - + res.status(500).send(); + } /* * Creating a User/updating a user - * + * * http://backend.endpoint/users/<username>?action=create - * + * * with body matching { @@ -205,11 +249,12 @@ export class FileRouter { } */ public postUsername(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * Listing available Roles - * + * * The servers response, unwrapped, must match array({ @@ -219,11 +264,12 @@ export class FileRouter { }) */ public getProjectRoles(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * Listing Projects - * + * * The server MUST respond with a list of projects, with the unwrapped response matching array({ @@ -238,11 +284,12 @@ export class FileRouter { }) */ public getProjects(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * Listing a specific Project - * + * * The server MUST respond with details of the particular project if it exists (unwrapped response): { @@ -257,13 +304,14 @@ export class FileRouter { } */ public getProjectName(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * Creating a Project / updating a project / deleting / Project grants - * + * * http://backend.endpoint/projects/<project_name>?action=create - * + * * with body matching { @@ -273,30 +321,34 @@ export class FileRouter { } */ public postProjectName(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /* * additional configuration options for individual projects */ public getProjectProperties(req: Request, res: Response, next: NextFunction){ + res.status(500).send(); } /** * GET a File. */ public getFile_path(req: Request, res: Response, next: NextFunction) { - let fileID = req.params.file; - - res.send("File"); + res.status(500).send(); + // let fileID = req.params.file; + // + // res.send("File"); } /** * GET a File. */ public getFile_id(req: Request, res: Response, next: NextFunction) { - let fileID = req.params.file; - - res.send("File"); + res.status(500).send(); + // let fileID = req.params.file; + // + // res.send("File"); } @@ -304,9 +356,10 @@ export class FileRouter { * Post a File. / delete / move / etc */ public postFile_path(req: Request, res: Response, next: NextFunction) { - let fileID = req.params.file; - - res.send("File"); + // let fileID = req.params.file; + // + // res.send("File"); + res.status(500).send(); } @@ -318,9 +371,10 @@ export class FileRouter { // General this.router.get ('/_supported_protocols_', this.getProtocols); this.router.get ('/log', this.getLog); - this.router.post('/log', this.postLog); + this.router.post('/log', this.postLog); this.router.get ('/properties', this.getProperties); this.router.post('/properties', this.postProperties); + // Users this.router.get ('/user_privileges', this.getUserPriveleges); this.router.get ('/users', this.getUsers); this.router.get ('/users/:username', this.getUsername); @@ -25,7 +25,8 @@ class App { const defRouter = express.Router() defRouter.get('/', (req, res) => { res.json({ - message: 'Welcome to the CS3099 BE4 server!' + message: 'Welcome to the CS3099 BE4 server!', + important : 'Endpoints start from /cs3099group-be-4/' }) }) this.express.use('/', defRouter) @@ -33,4 +34,4 @@ class App { } } -export default new App().express
\ No newline at end of file +export default new App().express diff --git a/src/serverConfig.ts b/src/serverConfig.ts new file mode 100644 index 0000000..b10641a --- /dev/null +++ b/src/serverConfig.ts @@ -0,0 +1,52 @@ +var serverConfig : Property[] = []; + +/* This is a class used to define the optional attribute of Property, + if the property is user configurable*/ +class PropertyDisplay { + constructor(cat : string, grp : string, dn : string, desc : string) { + this.category = cat; + this.group = grp; + this.display_name = dn; + this.description = desc; + }; + category : string; + group : string; + display_name : string; + description : string; +}; + +/* This is a class used to contain all of the protocol attributes of a Property + of the current backend server.*/ +class Property { + id : string; + display? : PropertyDisplay; + read_only : boolean; + type : string; + value : string | number | boolean; +} + +//Logging +var property_display = new PropertyDisplay("Logging", "BE4", "Is logging enabled?", "This property defines if logging is currently enabled on this backend server."); +serverConfig.push({id : "Server_Log", display : property_display, read_only : false, type : "boolean", value : false}); + +//caching settings (global) +serverConfig.push({id : "Global_Cache_Limit", read_only : true, type : "integer", value : 1}); +//caching settings (project level) +property_display = new PropertyDisplay("Caching", "BE4", "Caching Type", "This property defines the current caching style for this project."); +serverConfig.push({id : "project_example", display : property_display, read_only : false, type : "integer", value : 1}); + +function propertyExists (propKey : string): number { + var found = -1; + for (var prop in serverConfig) { + if (serverConfig[prop].id == propKey) { + found = parseInt(prop); + break; + } + } + return found; +} + +module.exports = { + default : serverConfig, + propertyExists : propertyExists, +} diff --git a/test/nodeserver.test.ts b/test/nodeserver.test.ts index 3ad37e5..83c66e8 100644 --- a/test/nodeserver.test.ts +++ b/test/nodeserver.test.ts @@ -22,5 +22,11 @@ describe('baseRoute', () => { expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); }); }); + it('should have an important prop', () => { + return chai.request(app).get('/') + .then(res => { + expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + }); + }); }); |
