add logger to dev mode

This commit is contained in:
Evert Prants 2017-11-07 18:52:12 +02:00
parent f8f67549dc
commit 11e5ceb0b7
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
6 changed files with 82 additions and 40 deletions

48
package-lock.json generated
View File

@ -408,10 +408,12 @@
"dev": true
},
"basic-auth": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-1.1.0.tgz",
"integrity": "sha1-RSIe5Cn37h5QNb4/UVM/HN/SmIQ=",
"optional": true
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.0.tgz",
"integrity": "sha1-AV2z81PgLlY3d1X5YnQuiYHnu7o=",
"requires": {
"safe-buffer": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz"
}
},
"bcrypt-pbkdf": {
"version": "1.0.1",
@ -3256,16 +3258,30 @@
}
},
"morgan": {
"version": "1.8.2",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.8.2.tgz",
"integrity": "sha1-eErHc05KRTqcbm6GgKkyknXItoc=",
"optional": true,
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.0.tgz",
"integrity": "sha1-0B+mxlhZt2/PMbPLU6OCGjEdgFE=",
"requires": {
"basic-auth": "1.1.0",
"debug": "https://registry.npmjs.org/debug/-/debug-2.6.8.tgz",
"depd": "https://registry.npmjs.org/depd/-/depd-1.1.0.tgz",
"basic-auth": "2.0.0",
"debug": "2.6.9",
"depd": "1.1.1",
"on-finished": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"on-headers": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz"
},
"dependencies": {
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz"
}
},
"depd": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.1.tgz",
"integrity": "sha1-V4O04cRZ8G+lyif5kfPQbnoxA1k="
}
}
},
"ms": {
@ -3460,7 +3476,7 @@
"body-parser": "https://registry.npmjs.org/body-parser/-/body-parser-1.17.2.tgz",
"express": "https://registry.npmjs.org/express/-/express-4.15.3.tgz",
"express-session": "https://registry.npmjs.org/express-session/-/express-session-1.15.3.tgz",
"morgan": "1.8.2"
"morgan": "1.9.0"
}
},
"oauth-sign": {
@ -4765,10 +4781,6 @@
}
}
},
"string_decoder": {
"version": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
},
"string-width": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
@ -4780,6 +4792,10 @@
"strip-ansi": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz"
}
},
"string_decoder": {
"version": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
},
"stringstream": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",

View File

@ -60,6 +60,7 @@
"concurrently": "^3.5.0",
"eslint-plugin-import": "^2.7.0",
"jquery": "^3.2.1",
"morgan": "^1.9.0",
"mustache": "^2.3.0",
"standard": "^10.0.3",
"uglifyjs-webpack-plugin": "^0.4.6",

View File

@ -29,6 +29,8 @@ function stampAndWrite (fnc, prfx, message) {
lfs.write(message + '\n')
}
message = message.replace(/\\u001b/g, '\x1b')
fnc.call(this, message)
}

View File

@ -62,7 +62,24 @@ async function cleanBanObject (dbe) {
}
}
function dataFilter (data, fields, optional = []) {
// Remove keys not listed in `fields`
for (let i in data) {
if (fields.indexOf(i) === -1) {
delete data[i]
}
}
// Ensure all the entries in `fields` are present as keys in `data` or are `optional`
for (let j in fields) {
if (!data[fields[j]] && optional.indexOf(fields[j]) === -1) return null
}
return data
}
const API = {
// List all users (paginated)
getAllUsers: async function (page, adminId) {
let count = await Models.User.query().count('id as ids')
if (!count.length || !count[0]['ids'] || isNaN(page)) {
@ -86,6 +103,7 @@ const API = {
users: users
}
},
// List all clients (paginated)
getAllClients: async function (page) {
let count = await Models.OAuth2Client.query().count('id as ids')
if (!count.length || !count[0]['ids'] || isNaN(page)) {
@ -108,26 +126,23 @@ const API = {
clients: clients
}
},
// Get information about a client via id
getClient: async function (id) {
let raw = await Models.OAuth2Client.query().where('id', id)
if (!raw.length) return null
return cleanClientObject(raw[0])
},
// Update a client `id` in database with `data`
updateClient: async function (id, data) {
let fields = [
'title', 'description', 'url', 'redirect_url', 'scope'
'title', 'description', 'url', 'redirect_url', 'scope', 'verified'
]
for (let i in data) {
if (fields.indexOf(i) === -1) {
delete data[i]
}
}
data = dataFilter(data, fields, ['scope', 'verified'])
if (!data) return { error: 'Missing fields' }
for (let i in fields) {
if (!data[fields[i]] && fields[i] !== 'scope') return {error: 'Missing fields'}
}
data.verified = (data.verified != null ? 1 : 0)
try {
await Models.OAuth2Client.query().patchAndFetchById(id, data)
@ -138,6 +153,7 @@ const API = {
return {}
},
// Create a new secret for a client
newSecret: async function (id) {
if (isNaN(id)) return { error: 'Invalid client ID' }
let secret = Users.Hash(16)
@ -150,20 +166,14 @@ const API = {
return {}
},
// Create a new client
createClient: async function (data, user) {
let fields = [
'title', 'description', 'url', 'redirect_url', 'scope'
]
for (let i in data) {
if (fields.indexOf(i) === -1) {
delete data[i]
}
}
for (let i in fields) {
if (!data[fields[i]] && fields[i] !== 'scope') return {error: 'Missing fields'}
}
data = dataFilter(data, fields, ['scope'])
if (!data) return { error: 'Missing fields' }
let obj = Object.assign({
secret: Users.Hash(16),
@ -174,6 +184,7 @@ const API = {
return Models.OAuth2Client.query().insert(obj)
},
// Remove a client and all associated data
removeClient: async function (id) {
if (isNaN(id)) return {error: 'Invalid ID number'}
await Models.OAuth2Client.query().delete().where('id', id)
@ -182,6 +193,7 @@ const API = {
await Models.OAuth2RefreshToken.query().delete().where('client_id', id)
return true
},
// List all bans (paginated)
getAllBans: async function (page) {
let count = await Models.Ban.query().count('id as ids')
if (!count.length || !count[0]['ids'] || isNaN(page)) {
@ -204,9 +216,11 @@ const API = {
bans: bans
}
},
// Remove a ban
removeBan: async function (banId) {
return Models.Ban.query().delete().where('id', banId)
},
// Create a ban
addBan: async function (data, adminId) {
let user = await Users.User.get(parseInt(data.user_id))

View File

@ -67,7 +67,14 @@ module.exports = (args) => {
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, '../views'))
if (args.dev) console.log('Worker is in development mode')
if (args.dev) {
console.log('Worker is in development mode')
// Dev logger
const morgan = require('morgan')
app.use(morgan('dev'))
}
let staticAge = args.dev ? 1000 : 7 * 24 * 60 * 60 * 1000 // 1 week of cache in production
// Static content directories, cache these requests.

View File

@ -52,6 +52,8 @@ block body
<input type="text" id="scope" name="scope" value="{{scope}}">
<label for="redirect_url">Redirect</label>
<input type="text" id="redirect_url" name="redirect_url" value="{{redirect_url}}">
<label for="verified">Verified</label>
<input type="checkbox" id="verified" name="verified" {{#verified}}checked{{/verified}}>
<input type="submit" value="Edit">
</form>
script(type="x-tmpl-mustache" id="clientNew").