Skip to content

Commit

Permalink
feat(jsbattle-admin): list users and battles in admin panel
Browse files Browse the repository at this point in the history
BREAKING CHANGE: server-side persistance layer changed (Moleculer DB Adapter)
  • Loading branch information
jamro committed Jan 27, 2020
1 parent 9248bb2 commit d510add
Show file tree
Hide file tree
Showing 36 changed files with 1,546 additions and 198 deletions.
5 changes: 1 addition & 4 deletions packages/jsbattle-admin/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ module.exports = {
"func-call-spacing": "error",
"func-name-matching": "error",
"func-names": "off",
"func-style": [
"error",
"declaration"
],
"func-style": 'off',
"function-paren-newline": "error",
"generator-star-spacing": "error",
"global-require": "error",
Expand Down
189 changes: 184 additions & 5 deletions packages/jsbattle-admin/mock/db.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,205 @@
{
"users": [
{
"username": "fredric.murphy",
"displayName": "Fredric Murphy",
"provider": "facebook",
"id": "8si8Su024sj2cZwo0",
"extUserId": "facebook_10477332",
"email": "fredric.murphy@gmail.com",
"role": "admin",
"createdAt": "2020-01-01T00:00:00.000Z",
"lastLoginAt": "2020-01-01T00:00:00.000Z"
},
{
"username": "adam",
"displayName": "Adam Cage",
"provider": "github",
"extUserId": "github_433453",
"email": "adam@gmail.com",
"role": "user",
"id": "mWHI3l5oetN1NXIP",
"id": "1WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "john",
"displayName": "John Adams",
"provider": "github",
"extUserId": "github_3452234",
"email": "ajohn@gmail.com",
"role": "user",
"id": "2WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "monica",
"displayName": "Monica Wick",
"provider": "facebook",
"extUserId": "facebook_884732934",
"email": "monica@yahoo.com",
"role": "user",
"id": "3WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "maya_us",
"displayName": "Maya Goodman",
"provider": "facebook",
"extUserId": "facebook_884732934",
"email": "maya@yahoo.com",
"role": "user",
"id": "4WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "denise83",
"displayName": "Denise McDonald",
"provider": "slack",
"extUserId": "slack_43434",
"email": "denise@google.com",
"role": "user",
"id": "5WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "albkov",
"displayName": "Albert Kovalsky",
"provider": "slack",
"extUserId": "slack_43234",
"email": "albkov@gmail.com",
"role": "user",
"id": "6WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "abbn",
"displayName": "Abby Novak",
"provider": "linkedin",
"extUserId": "linkedin_98438",
"email": "abbn@slack.com",
"role": "user",
"id": "7WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "gego",
"displayName": "George Gova",
"provider": "linkedin",
"extUserId": "linkedin_338472",
"email": "gego@yahoo.com",
"role": "user",
"id": "8WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "dwalsh",
"displayName": "Diego Walsh",
"provider": "slack",
"extUserId": "slack_00832",
"email": "dwalsh@aol.com",
"role": "user",
"id": "9WHI3l5oetN1NXIP",
"createdAt": "2020-01-24T23:17:43.004Z",
"lastLoginAt": "2020-01-24T23:17:43.004Z"
},
{
"username": "fredric.murphy",
"displayName": "Fredric Murphy",
"provider": "facebook",
"id": "8si8Su024sj2cZwo0",
"extUserId": "facebook_10477332",
"email": "fredric.murphy@gmail.com",
"role": "admin",
"createdAt": "2020-01-01T00:00:00.000Z",
"lastLoginAt": "2020-01-01T00:00:00.000Z"
}
],
"battles": [
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"1QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"2QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"3QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"4QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"5QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"6QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"7QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"8QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"9QCTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"11CTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"12CTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
},
{
"ubd":"{\"version\":3,\"rngSeed\":0.8186448997231914,\"teamMode\":false,\"timeLimit\":0,\"aiList\":[{\"name\":\"jamro\",\"team\":\"2m9po29ef\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100},{\"name\":\"kamikaze\",\"team\":\"97y2ckbx\",\"code\":null,\"initData\":null,\"useSandbox\":true,\"executionLimit\":100}]}",
"id":"13CTzInfyJTAGm9i",
"createdAt": "2020-01-24T23:17:43.004Z"
}
],
"battles": [],
"authMethods": {
"github": {"name": "GitHub", "url": "../auth/github"},
"facebook": {"name": "Facebook", "url": "../auth/facebook"},
"google": {"name": "Google", "url": "../auth/google"},
"twitter": {"name": "Twitter", "url": "../auth/twitter"},
"linkedin": {"name": "LinkedIn", "url": "../auth/linkedin"},
"slack": {"name": "Slack", "url": "../auth/slack"}
},
"profile": {
"id": "8si8Su024sj2cZwo0",
"extUserId": "facebook_10477332",
"username": "fredric.mcdonald",
"username": "fredric.murphy",
"provider": "facebook",
"email": "fredric.mcdonald@gmail.com",
"email": "fredric.murphy@gmail.com",
"role": "admin",
"displayName": "Fredric McDonald",
"displayName": "Fredric Murphy",
"createdAt": "2020-01-01T00:00:00.000Z",
"lastLoginAt": "2020-01-01T00:00:00.000Z"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jsbattle-admin/mock/router.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"/api/authMethods": "/authMethods",
"/api/profile": "/profile",
"/api/admin/*": "/$1"
}
62 changes: 54 additions & 8 deletions packages/jsbattle-admin/mock/server.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,79 @@
#!/usr/bin/env node

const path = require('path')
const express = require('express')
const querystring = require('querystring')
const jsonServer = require('json-server')
const bodyParser = require('body-parser')
const server = jsonServer.create()
const router = jsonServer.router(path.resolve(__dirname , 'db.json'))
const middlewares = jsonServer.defaults({static: path.resolve(__dirname , '..', 'dist')});
const middlewares = jsonServer.defaults();
const _ = require('lodash');

let authorized = false;

router.render = (req, res) => {
if(req.url == '/profile' && !authorized) {
return res.jsonp({});
}
if(Array.isArray(res.locals.data) && Object.keys(req.query).filter((k) => (/^id$/i).test(k)).length) {
return res.jsonp(res.locals.data[0]);
}
if(!Array.isArray(res.locals.data)) {
return res.jsonp(res.locals.data);
}
let page = 1;
let pageSize = 10;
let params = querystring.parse(req.url.split('?').pop());
let page = params._page || 1;
let pageSize = params._limit || 10;
res.jsonp({
rows: res.locals.data.slice((page-1)*pageSize, page*pageSize),
total: res.locals.data.length,
rows: res.locals.data,
total: 500,
page: page,
pageSize: pageSize,
totalPages: Math.ceil(res.locals.data.length/pageSize)
totalPages: Math.ceil(500/pageSize)
})
}


let serverDelay = isNaN(process.argv[2]) ? 0 : process.argv[2];
serverDelay *= Math.round(0.5 + 1*Math.random())
server.use('/admin', express.static(path.resolve(__dirname , '..', 'dist')))
server.get('/', function (req, res) {
res.redirect('/admin')
})
server.use(bodyParser.urlencoded({ extended: true }))
server.use(bodyParser.json())
server.get('/auth/logout', function (req, res) {
console.log("Logout");
authorized = false;
res.redirect('/admin')
})
server.get('/auth/:provider', function (req, res) {
console.log("Authorized");
authorized = true;
res.redirect('/admin')
})


server.use((req, res, next) => {
if(/^\/api\/admin\//.test(req.url) && !authorized) {
return res.status(401).send('401 Unauthorized')
}
next()
});
server.use((req, res, next) => {
if(serverDelay) {
req.query._delay = serverDelay;
}
if(req.query.page) {
req.query._page = req.query.page;
req.url += "&_page=" + req.query.page
}
if(req.query.pageSize) {
req.query._limit = req.query.pageSize;
req.url += "&_limit=" + req.query.pageSize
}
next()
});
server.use((req, res, next) => {
if(req.method == 'POST') {
let resource = req.url.replace(/^.*\/([A-Za-z]+)\/?$/, '$1');
Expand All @@ -52,6 +97,7 @@ server.use(middlewares)
server.use(jsonServer.rewriter(require(path.resolve(__dirname , "router.json"))))
server.use(router)

server.listen(3000, () => {

server.listen(8071, () => {
console.log('JSON Server is running')
})
Loading

0 comments on commit d510add

Please sign in to comment.