<commit_before>import express from 'express'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema, addResolveFunctionsToSchema} from 'graphql-tools'
import path from 'path'
import aws from 'aws-sdk'
import shortid from 'shortid'
import bodyParser from 'body-parser'
import {get, last, map} from 'lodash'
import cors from 'cors'
import {readFileSync} from 'fs'
import jwtExpress from 'express-jwt'
import jwt from 'jsonwebtoken'
import {Strategy as GoogleStrategy} from 'passport-google-oauth2'
import passport from 'passport'
import {User} from './models'
import resolvers from './resolvers'
import {Site} from './models'

const admin = express.Router()

admin.use(cors({origin: 'http://medinstitute.dreamscape.dev:4000', credentials: true}))

passport.use(new GoogleStrategy({
    clientID:     process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL: "http://admin.dreamscape.dev:3000/auth/google/callback",
    passReqToCallback: true
  },
  (request, accessToken, refreshToken, profile, done) => {
    User.findOne({email: profile.email}).then(user => {
      const opts = {
        name: profile.displayName,
        email: profile.email,
        avatar: profile.photos ? profile.photos[0].value : null
      }
      if (user) {
        user.set(opts)
      } else {
        user = new User(opts)
      }
      user.save().then((_user) => {
        done(null, _user.toObject())
      }).catch((err) => done(err, null))
    }).catch((err) => done(err, null))
  }
))

admin.get('/auth/google/callback',
	passport.authenticate( 'google', {session: false}), (req, res) => {
  res.cookie('authtoken', jwt.sign(req.user, process.env.JWT_SECRET), {
    domain: `.${process.env.BACKEND_DOMAIN}`,
    httpOnly: true,
    path: '/',
    expires: new Date(Date.now() + 365 * 24 * 3600 * 1000)
  })
  res.redirect(req.cookies.redirect)
})

if (process.env.NODE_ENV === 'development') {
  admin.get('/auth/as/:email', (req, res) => {
    const user = {
      email: req.params.email,
      name: req.params.email
    }
    res.cookie('authtoken', jwt.sign(user, process.env.JWT_SECRET), {
      domain: `.${process.env.BACKEND_DOMAIN}`,
      httpOnly: true,
      path: '/',
      expires: new Date(Date.now() + 365 * 24 * 3600 * 1000)
    })
    res.send(`Ok, you are now ${user.email}`)
  })
}

const typeDefs = readFileSync('./backend/schema.gql').toString()
const schema = makeExecutableSchema({typeDefs, resolvers})
addResolveFunctionsToSchema(schema, {});

admin.use(async (req, res, next) => {
  const regex = new RegExp(`(.*).${process.env.BACKEND_DOMAIN}`, 'i')
  if (!regex.test(req.hostname)) {
    next('router')
  } else {
    const match = req.hostname.match(regex)
    const site = await Site.findOne({key: match[1].toLowerCase()})
    if (site) {
      req.site = site
      next()
    } else {
      next('router')
    }
  }
})

admin.use(passport.initialize())

admin.get('/auth/google', (req, res, next) => {
  res.cookie('redirect', req.get('Referrer'), {domain: `.${process.env.BACKEND_DOMAIN}`})
  next()
},
passport.authenticate('google', { scope:
  [ 'https://www.googleapis.com/auth/plus.login',
    'https://www.googleapis.com/auth/plus.profile.emails.read' ], session: false }
))


if (process.env.NODE_ENV === 'production') {
  admin.use(express.static(path.resolve(__dirname, '../build-admin')))
  admin.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, '../build-admin/index.html'))
  })
}

admin.use(jwtExpress({
  secret: process.env.JWT_SECRET,
  credentialsRequired: false,
  getToken: req => req.cookies.authtoken,
}).unless({path: [
  '/auth/google'
]}), (req, res, next) => {
  if (!req.user) {
    res.sendStatus(401)
  } else {
    next()
  }
})

admin.get('/session', (req, res) => {
  res.json(req.user)
})

admin.use('/graphql', bodyParser.json(), graphqlExpress(req => ({
  schema,
  rootValue: req,
  formatError: error => {
    let originalErrors = get(error, 'originalError.errors')
    let errors = {}
    if (originalErrors) {
      Object.keys(originalErrors).forEach(field => {
        errors[field] = originalErrors[field].message
      })
    }
    return {
      message: error.message,
      errors,
    }
  },
})))

admin.use('/logout', (req, res) => {
  res.clearCookie('authtoken')
  res.sendStatus(200)
})

admin.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}))

admin.get('/sign-s3', async (req, res) => {
  const s3 = new aws.S3({region: 'eu-west-1'})
  const ext = last(req.query.name.split('.'))
  const fileName = `${req.site.key}/images/${shortid.generate()}.${ext}`
  const fileType = req.query.type
  const s3Params = {
    Bucket: process.env.S3_BUCKET,
    Key: fileName,
    Expires: 60,
    ContentType: fileType,
    ACL: 'public-read',
  }
  s3.getSignedUrl('putObject', s3Params, (err, data) => {
    if(err){
      console.log(err)
      return res.end()
    }
    res.write(data)
    res.end()
  });
})

admin.get('/images', async (req, res) => {
  const {type, id} = req.query
  const klass = require('./models')[type]
  const object = await klass.findById(id)
  res.json(map(object.images, url => ({
    thumb: url,
    url,
    id: url,
  })))
})

export default admin<commit_msg>serve the react app before authentication<commit_after>import express from 'express'
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'
import {makeExecutableSchema, addResolveFunctionsToSchema} from 'graphql-tools'
import path from 'path'
import aws from 'aws-sdk'
import shortid from 'shortid'
import bodyParser from 'body-parser'
import {get, last, map} from 'lodash'
import cors from 'cors'
import {readFileSync} from 'fs'
import jwtExpress from 'express-jwt'
import jwt from 'jsonwebtoken'
import {Strategy as GoogleStrategy} from 'passport-google-oauth2'
import passport from 'passport'
import {User} from './models'
import resolvers from './resolvers'
import {Site} from './models'

const admin = express.Router()

admin.use(cors({origin: 'http://medinstitute.dreamscape.dev:4000', credentials: true}))

passport.use(new GoogleStrategy({
    clientID:     process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL: "http://admin.dreamscape.dev:3000/auth/google/callback",
    passReqToCallback: true
  },
  (request, accessToken, refreshToken, profile, done) => {
    User.findOne({email: profile.email}).then(user => {
      const opts = {
        name: profile.displayName,
        email: profile.email,
        avatar: profile.photos ? profile.photos[0].value : null
      }
      if (user) {
        user.set(opts)
      } else {
        user = new User(opts)
      }
      user.save().then((_user) => {
        done(null, _user.toObject())
      }).catch((err) => done(err, null))
    }).catch((err) => done(err, null))
  }
))

admin.get('/auth/google/callback',
	passport.authenticate( 'google', {session: false}), (req, res) => {
  res.cookie('authtoken', jwt.sign(req.user, process.env.JWT_SECRET), {
    domain: `.${process.env.BACKEND_DOMAIN}`,
    httpOnly: true,
    path: '/',
    expires: new Date(Date.now() + 365 * 24 * 3600 * 1000)
  })
  res.redirect(req.cookies.redirect)
})

if (process.env.NODE_ENV === 'development') {
  admin.get('/auth/as/:email', (req, res) => {
    const user = {
      email: req.params.email,
      name: req.params.email
    }
    res.cookie('authtoken', jwt.sign(user, process.env.JWT_SECRET), {
      domain: `.${process.env.BACKEND_DOMAIN}`,
      httpOnly: true,
      path: '/',
      expires: new Date(Date.now() + 365 * 24 * 3600 * 1000)
    })
    res.send(`Ok, you are now ${user.email}`)
  })
}

const typeDefs = readFileSync('./backend/schema.gql').toString()
const schema = makeExecutableSchema({typeDefs, resolvers})
addResolveFunctionsToSchema(schema, {});

admin.use(async (req, res, next) => {
  const regex = new RegExp(`(.*).${process.env.BACKEND_DOMAIN}`, 'i')
  if (!regex.test(req.hostname)) {
    next('router')
  } else {
    const match = req.hostname.match(regex)
    const site = await Site.findOne({key: match[1].toLowerCase()})
    if (site) {
      req.site = site
      next()
    } else {
      next('router')
    }
  }
})

admin.use(passport.initialize())

admin.get('/auth/google', (req, res, next) => {
  res.cookie('redirect', req.get('Referrer'), {domain: `.${process.env.BACKEND_DOMAIN}`})
  next()
},
passport.authenticate('google', { scope:
  [ 'https://www.googleapis.com/auth/plus.login',
    'https://www.googleapis.com/auth/plus.profile.emails.read' ], session: false }
))

const auth = [
  jwtExpress({
    secret: process.env.JWT_SECRET,
    credentialsRequired: false,
    getToken: req => req.cookies.authtoken,
  }).unless({path: ['/auth/google']}),
  (req, res, next) => {
    if (!req.user) {
      res.sendStatus(401)
    } else {
      next()
    }
  }
]

admin.get('/session', auth, (req, res) => {
  res.json(req.user)
})

admin.use('/graphql', auth, bodyParser.json(), graphqlExpress(req => ({
  schema,
  rootValue: req,
  formatError: error => {
    let originalErrors = get(error, 'originalError.errors')
    let errors = {}
    if (originalErrors) {
      Object.keys(originalErrors).forEach(field => {
        errors[field] = originalErrors[field].message
      })
    }
    return {
      message: error.message,
      errors,
    }
  },
})))

admin.use('/logout', auth, (req, res) => {
  res.clearCookie('authtoken')
  res.sendStatus(200)
})

admin.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}))

admin.get('/sign-s3', auth, async (req, res) => {
  const s3 = new aws.S3({region: 'eu-west-1'})
  const ext = last(req.query.name.split('.'))
  const fileName = `${req.site.key}/images/${shortid.generate()}.${ext}`
  const fileType = req.query.type
  const s3Params = {
    Bucket: process.env.S3_BUCKET,
    Key: fileName,
    Expires: 60,
    ContentType: fileType,
    ACL: 'public-read',
  }
  s3.getSignedUrl('putObject', s3Params, (err, data) => {
    if(err){
      console.log(err)
      return res.end()
    }
    res.write(data)
    res.end()
  });
})

admin.get('/images', auth, async (req, res) => {
  const {type, id} = req.query
  const klass = require('./models')[type]
  const object = await klass.findById(id)
  res.json(map(object.images, url => ({
    thumb: url,
    url,
    id: url,
  })))
})

if (process.env.NODE_ENV === 'production') {
  admin.use(express.static(path.resolve(__dirname, '../build-admin')))
  admin.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, '../build-admin/index.html'))
  })
}

export default admin
{'fraction_non_alphanumeric': 0.1786420566908372, 'fraction_numerical': 0.009982107543083154, 'mean_word_length': 3.4435146443514646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1253133', 'n_tokens_mistral': 3769, 'n_tokens_neox': 3703, 'n_words': 729}