Commit cacca7bb authored by frekk's avatar frekk

Cleaned up, using autogenerated express skeleton & wrote conversion script from old memberdb

parent 8667eaab
# VS Code metadata
.vscode
.vscode/**
node_modules
node_modules/**
# local testing database stuff
.db/
misc/pg.json
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
This diff is collapsed.
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.get("/user/:userid/", function (req, res) {
res.send({test: 'data'});
});
app.get("/:str/blah/:name/:id", function (req, res) {
res.send("Get request to " + req.url + " was received!");
res.send(req.params);
});
module.exports = app;
#!/usr/bin/env node
/** Module dependencies. */
var app = require('../app');
var debug = require('debug')('uccportal:server');
var http = require('http');
var mongoose = require('mongoose');
/** Get port from environment and store in Express. */
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/** Create HTTP server. */
var server = http.createServer(app);
/** Listen on provided port, on all network interfaces. */
server.listen(port);
server.on('error', onHTTPError);
server.on('listening', onHTTPListening);
/** Connect to mongodb */
mongoose.connect('mongodb://localhost/uccportal-dev');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'db connection error:'));
db.once('open', function() {
console.log("Connected to db");
});
/** Normalize a port into a number, string, or false. */
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/** Event listener for HTTP server "error" event. */
function onHTTPError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/** Event listener for HTTP server "listening" event. */
function onHTTPListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
#!/usr/bin/env node
/* script to import users from memberdb format (MySQL) to mongoose for testing */
const pg = require('pg');
var mongoose = require('mongoose');
var conf = require('./pg.json');
var Member = require('../models/memberSchema');
/* specify stuff via environment variables
PGUSER=uccmemberdb PGHOST=localhost PGPASSWORD=[redacted] PGDATABASE=uccmemberdb_2018 PGPORT=5432 node misc/import_memberdb.js
*/
const pgclient = new pg.Client(conf);
pgclient.connect();
console.log("waiting for pgclient to connect");
mongoose.connect('mongodb://localhost/uccportal-dev');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'db connection error:'));
db.once('open', function() {
console.log("Connected to db");
Member.remove({}, function (err) {
console.log(err);
pgclient.query('SELECT * FROM memberdb_member', processOldMembers);
});
});
console.log("waiting for mongoose to open");
/** old memberdb schema from postgresql:
CREATE TABLE memberdb_member (
id integer NOT NULL,
real_name character varying(200) NOT NULL,
username character varying(16) NOT NULL,
membership_type integer NOT NULL, // 1: O-day, 2: student, 3: non-student
guild_member boolean NOT NULL,
phone_number character varying(14) NOT NULL,
email_address character varying(200) NOT NULL,
student_no character varying(20) NOT NULL,
date_of_birth date,
signed_up date DEFAULT '2018-02-23'::date NOT NULL
);
*/
function processOldMembers(err, res) {
var newMembers = [];
if (err) {
console.log(err.stack);
} else {
console.log("Dumping " + res.rows.length + " rows...");
for (var i = 0; i < res.rows.length; i++) {
if (res.rows[i].username) {
console.log("user: " + res.rows[i].username);
} else {
console.log("new user: " + res.rows[i].real_name);
}
newMembers.push(convertMember(res.rows[i]));
}
Member.insertMany(newMembers, function (err, docs) {
console.log("Done.");
pgclient.end();
Member.find().exec(verifyMongoMembers);
});
}
};
function convertMember(row) {
var renewtype, is_student;
switch (row.membership_type) {
case 1: // O-Day
is_student = true;
renewtype = "oday";
break;
case 2: // Student
is_student = true;
renewtype = "student";
break;
case 3: // Non-student
is_student = false;
renewtype = "nonstudent";
break;
}
return {
firstname: row.real_name,
lastname: "",
is_student: is_student,
is_guild: row.guild_member,
id_number: row.student_no,
email: row.email_address,
phone: row.phone_number,
birthdate: row.date_of_birth,
signupdate: row.signed_up,
renewals: [ { renewtype: renewtype, date: row.signed_up } ],
tlas: [ "???" ]
};
}
function verifyMongoMembers(err, res) {
console.log(res[0]);
console.log("Currently " + res.length + " members in mongodb.");
db.close();
}
\ No newline at end of file
#!/bin/bash
echo "Starting mongod..."
cd $(dirname $0)
mkdir -p ../.db/ 2>/dev/null
mongod --dbpath ../.db/
\ No newline at end of file
'use strict';
var mongoose = require('mongoose');
// Here we define the schema used by the model for gumby documents
var memberSchema = mongoose.Schema({
firstname: String,
lastname: String,
is_student: Boolean,
is_guild: Boolean,
id_number: String,
email: String,
phone: String,
birthdate: Date,
username: String,
tlas: [String],
signupdate: Date,
renewals: [{renewtype: String, date: Date}]
});
// And we export (return) a model based on the schema
module.exports = mongoose.model('Member', memberSchema);
\ No newline at end of file
{
"name": "uccportal",
"version": "1.0.0",
"version": "0.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -169,6 +169,14 @@
}
}
},
"basic-auth": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.0.tgz",
"integrity": "sha1-AV2z81PgLlY3d1X5YnQuiYHnu7o=",
"requires": {
"safe-buffer": "5.1.1"
}
},
"binary-extensions": {
"version": "1.11.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-1.11.0.tgz",
......@@ -256,6 +264,12 @@
"resolved": "https://registry.npmjs.org/bson/-/bson-1.0.6.tgz",
"integrity": "sha512-D8zmlb46xfuK2gGvKmUjIklQEouN2nQ0LEHHeZ/NoHM2LDiMk2EYzZ5Ntw/Urk+bgMDosOZxaRzXxvhI5TcAVQ=="
},
"buffer-writer": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-1.0.1.tgz",
"integrity": "sha1-Iqk2kB4wKa/NdUfrRIfOtpejvwg=",
"dev": true
},
"bytes": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
......@@ -422,6 +436,15 @@
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz",
"integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s="
},
"cookie-parser": {
"version": "1.4.3",
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.3.tgz",
"integrity": "sha1-D+MfoZ0AC5X0qt8fU/3CuKIDuqU=",
"requires": {
"cookie": "0.3.1",
"cookie-signature": "1.0.6"
}
},
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
......@@ -1958,6 +1981,18 @@
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
"integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ=="
},
"morgan": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.0.tgz",
"integrity": "sha1-0B+mxlhZt2/PMbPLU6OCGjEdgFE=",
"requires": {
"basic-auth": "~2.0.0",
"debug": "2.6.9",
"depd": "~1.1.1",
"on-finished": "~2.3.0",
"on-headers": "~1.0.1"
}
},
"mpath": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.4.1.tgz",
......@@ -2131,6 +2166,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz",
"integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c="
},
"p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
......@@ -2149,6 +2189,12 @@
"semver": "^5.1.0"
}
},
"packet-reader": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-0.3.1.tgz",
"integrity": "sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc=",
"dev": true
},
"parseurl": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
......@@ -2198,6 +2244,73 @@
"through": "~2.3"
}
},
"pg": {
"version": "7.4.3",
"resolved": "https://registry.npmjs.org/pg/-/pg-7.4.3.tgz",
"integrity": "sha1-97b5P1NA7MJZavu5ShPj1rYJg0s=",
"dev": true,
"requires": {
"buffer-writer": "1.0.1",
"packet-reader": "0.3.1",
"pg-connection-string": "0.1.3",
"pg-pool": "~2.0.3",
"pg-types": "~1.12.1",
"pgpass": "1.x",
"semver": "4.3.2"
},
"dependencies": {
"semver": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz",
"integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=",
"dev": true
}
}
},
"pg-connection-string": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz",
"integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=",
"dev": true
},
"pg-pool": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.3.tgz",
"integrity": "sha1-wCIDLIlJ8xKk+R+2QJzgQHa+Mlc=",
"dev": true
},
"pg-types": {
"version": "1.12.1",
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz",
"integrity": "sha1-1kCH45A7WP+q0nnnWVxSIIoUw9I=",
"dev": true,
"requires": {
"postgres-array": "~1.0.0",
"postgres-bytea": "~1.0.0",
"postgres-date": "~1.0.0",
"postgres-interval": "^1.1.0"
}
},
"pgpass": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz",
"integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=",
"dev": true,
"requires": {
"split": "^1.0.0"
},
"dependencies": {
"split": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",
"integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==",
"dev": true,
"requires": {
"through": "2"
}
}
}
},
"pify": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz",
......@@ -2210,6 +2323,33 @@
"integrity": "sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=",
"dev": true
},
"postgres-array": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-1.0.2.tgz",
"integrity": "sha1-jgsy6wO/d6XAp4UeBEHBaaJWojg=",
"dev": true
},
"postgres-bytea": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz",
"integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=",
"dev": true
},
"postgres-date": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.3.tgz",
"integrity": "sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=",
"dev": true
},
"postgres-interval": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.1.1.tgz",
"integrity": "sha512-OkuCi9t/3CZmeQreutGgx/OVNv9MKHGIT5jH8KldQ4NLYXkvmT9nDVxEuCENlNwhlGPE374oA/xMqn05G49pHA==",
"dev": true,
"requires": {
"xtend": "^4.0.0"
}
},
"prepend-http": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz",
......@@ -3053,6 +3193,12 @@
"integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=",
"dev": true
},
"xtend": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",
"integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=",
"dev": true
},
"yallist": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
......
{
"name": "uccportal",
"version": "1.0.0",
"version": "0.0.1",
"description": "A management portal for UCC members.",
"main": "server.js",
"scripts": {
"test": "echo \"No test specified\" && exit 1",
"start": "nodemon server.js"
},
"repository": {
"type": "git",
"url": "https://gitlab.ucc.asn.au/frekk/uccportal.git"
},
"author": "frekk",
"license": "GPL3",
"license": "GPL-3.0-or-later",
"devDependencies": {
"nodemon": "^1.17.5"
"nodemon": "^1.17.5",
"pg": "^7.4.3"
},
"dependencies": {
"express": "^4.16.3",
"mongoose": "^5.1.4"
"mongoose": "^5.1.4",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"morgan": "~1.9.0"
}
}
<html>
<head>
<title>Express</title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1>Express</h1>
<p>Welcome to Express</p>
</body>
</html>
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: rgb(0, 183, 255);
}
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get('/:id', function(req, res, next) {
res.send("information about user with ID " + req.params.id);
});
module.exports = router;
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),