first draft on the backend, no auth, no subscription, no edition

This commit is contained in:
Nikola Bjelo committed 2018-10-18 08:31:36 +02:00
1 parent 1b0e344f5c
commit dd339f6899
10 files changed
+6216

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
{
"presets": ["env", "stage-2"]
}
+4
View File
@@ -0,0 +1,4 @@
PORT=3000
DB_USERNAME=example_name
DB_PASSWORD=example_password
DB_DOMAIN=example.com:1234/example_name
+23
View File
@@ -0,0 +1,23 @@
# Logs
logs
*.log
npm-debug.log*
# Dependency directories
node_modules/
# Optional npm cache directory
.npm
# dotenv environment variables file
.env
# Build folder js files
build/
dist/
# Visual Studio Code
.vscode
#Mac specific files
.DS_Store
+5850
View File
File diff suppressed because it is too large. Load diff
+32
View File
@@ -0,0 +1,32 @@
{
"name": "@advancedalgos/hosts-client",
"version": "0.0.1",
"description": "Advanced Algos Host client-side web app",
"author": "",
"license": "",
"main": "src/index.js",
"scripts": {
"start": "nodemon --exec babel-node --presets env,stage-2 src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-2": "^6.24.1",
"nodemon": "^1.18.4"
},
"dependencies": {
"cors": "^2.8.4",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"express-graphql": "^0.6.12",
"express-jwt": "^5.3.1",
"graphql": "^14.0.0",
"graphql-playground-middleware-express": "^1.7.6",
"jsonwebtoken": "^8.3.0",
"jwks-rsa": "^1.3.0",
"lodash": "^4.17.10",
"mongoose": "^5.3.4",
"winston": "^3.1.0"
}
}
+45
View File
@@ -0,0 +1,45 @@
import express from 'express'
import cors from 'cors'
import mongoose from 'mongoose'
import graphqlHTTP from 'express-graphql'
import 'dotenv/config'
import expressPlayground from 'graphql-playground-middleware-express'
import schema from './schema'
// Initialise express
const app = express()
// Allow cross origing request
app.use(cors())
/* -------------------------------------------------
---------------- Database setup --------------------
------------------------------------------------- */
// Database Connection
mongoose.connect(`mongodb://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_DOMAIN}`, { useNewUrlParser: true })
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise
// Get the default connection
var db = mongoose.connection
// bind error and success to console log
db.on('error', () => { console.error.bind(console, 'MongoDB connection error:') })
db.once('open', () => { console.log('Connected to the DB.') })
/* -------------------------------------------------
----------------- Graphql setup --------------------
------------------------------------------------- */
app.use('/graphql', graphqlHTTP(req => {
return {
schema: schema,
context: { user: req.user }
}
}))
app.get('/playground', expressPlayground({ endpoint: '/graphql' }))
app.listen(process.env.PORT, () => {
console.log(`Listening on port ${process.env.PORT}`)
})
+40
View File
@@ -0,0 +1,40 @@
import mongoose, { Schema } from 'mongoose'
const competitionSchema = new Schema({
codeName: String,
displayName: String,
host: String,
description: String,
startDatetime: Date,
finishDatetime: Date,
formula: String,
plotter: {
codeName: String,
host: String,
repo: String,
moduleName: String
},
rules: [{
number: Number,
title: String,
description: String
}],
prizes: [{
position: Number,
algoPrize: Number,
otherPrizes: [{
condition: String,
amount: Number,
asset: String
}]
}],
participants: [{
id: String,
bot: String,
release: String
}]
})
const Competition = mongoose.model('Competition', competitionSchema)
export default Competition
+166
View File
@@ -0,0 +1,166 @@
import {
GraphQLObjectType,
GraphQLSchema,
GraphQLID,
GraphQLString,
GraphQLInt,
GraphQLList,
GraphQLNonNull
} from 'graphql'
import { slugify } from './utils/functions'
import Competition from './models/competition'
const PlotterType = new GraphQLObjectType({
name: 'Plotter',
fields: () => ({
codeName: { type: GraphQLString },
host: { type: GraphQLString },
repo: { type: GraphQLString },
moduleName: { type: GraphQLString }
})
})
const RuleType = new GraphQLObjectType({
name: 'Rule',
fields: () => ({
number: { type: GraphQLInt },
title: { type: GraphQLString },
description: { type: GraphQLString }
})
})
const ParticipantType = new GraphQLObjectType({
name: 'Participant',
fields: () => ({
devTeam: { type: GraphQLString },
bot: { type: GraphQLString },
release: { type: GraphQLString }
})
})
const OtherPrizeType = new GraphQLObjectType({
name: 'OtherPrize',
fields: () => ({
condition: { type: GraphQLString },
amount: { type: GraphQLInt },
asset: { type: GraphQLString }
})
})
const PrizeType = new GraphQLObjectType({
name: 'Prize',
fields: () => ({
position: { type: GraphQLInt },
algoPrize: { type: GraphQLInt },
otherPrizes: {
type: new GraphQLList(OtherPrizeType),
resolve (parent, args) {
return parent.otherPrizes
}
}
})
})
const CompetitionType = new GraphQLObjectType({
name: 'Competition',
fields: () => ({
codeName: { type: GraphQLID },
displayName: { type: GraphQLString },
host: { type: GraphQLString },
description: { type: GraphQLString },
startDatetime: { type: GraphQLString },
finishDatetime: { type: GraphQLString },
formula: { type: GraphQLString },
plotter: {
type: PlotterType,
resolve (parent, args) {
return parent.plotter
}
},
rules: {
type: new GraphQLList(RuleType),
resolve (parent, args) {
return parent.rules
}
},
participants: {
type: new GraphQLList(ParticipantType),
resolve (parent, args) {
return parent.participants
}
},
prizes: {
type: new GraphQLList(PrizeType),
resolve (parent, args) {
return parent.prizes
}
}
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
competitions: {
type: new GraphQLList(CompetitionType),
resolve (parent, args, context) {
return Competition.find()
}
},
competitionsByHost: {
type: new GraphQLList(CompetitionType),
args: {
host: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, args, context) {
return Competition.find({
host: args.host
})
}
}
}
})
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
hostCompetition: {
type: CompetitionType,
args: {
displayName: { type: new GraphQLNonNull(GraphQLString) },
description: { type: new GraphQLNonNull(GraphQLString) },
startDatetime: { type: new GraphQLNonNull(GraphQLString) },
finishDatetime: { type: new GraphQLNonNull(GraphQLString) },
formula: { type: new GraphQLNonNull(GraphQLString) }
},
resolve (parent, args, context) {
// let authIdOnSession = context.user.sub
let authIdOnSession = 'some-id'
let newCompetition = new Competition({
host: authIdOnSession,
displayName: args.displayName,
description: args.description,
// startDatetime: args.startDatetime,
// finishDatetime: args.finishDatetime,
formula: args.formula
})
newCompetition.codeName = slugify(newCompetition.displayName) + '-' + newCompetition._id
return new Promise((resolve, reject) => {
newCompetition.save((err) => {
if (err) reject(err)
else {
resolve(newCompetition)
}
})
})
}
}
}
})
const Schema = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
export default Schema
+14
View File
@@ -0,0 +1,14 @@
export const capitalize = (string) => {
return (string).charAt(0).toUpperCase() + (string).slice(1)
}
export const slugify = (text) => {
return text.toString().toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
}
export default { capitalize, slugify }
+39
View File
@@ -0,0 +1,39 @@
const { createLogger, format, transports } = require('winston')
const { combine, timestamp, simple } = format
const options = {
file: {
level: 'info',
filename: '../logs/app.log',
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
timestamp: true
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
timestamp: true
}
}
const logger = createLogger({
level: 'info',
format: combine(
timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
simple()
),
transports: [
new transports.File(options.file),
new transports.Console(options.console)
],
exitOnError: false // do not exit on handled exceptions
})
export default logger