// Environment
global.ENV = process.env.NODE_ENV || 'dev'

const Koa = require('koa')
const cors = require('koa-cors')
const log = require('./modules/logger')('arxivum:api')

const { API_PORT } = require('../config/')

const app = new Koa()

// Init database
require('./modules/database')

// TODO: Remove by default, allow by options.
app.use(cors({
  credentials: true,
  methods: ['GET', 'PUT', 'POST', 'DELETE', 'PATCH']
}))

// Global middlewares
const logger = require('./middleware/requestLogger')
app.use(logger)

const errorHandler = require('./middleware/errorHandler')
app.use(errorHandler)

const bodyParser = require('koa-bodyparser')
app.use(bodyParser())

// Init API routes
const apiRouter = require('./modules/apiRouter')
app.use(apiRouter.routes())

app.listen(API_PORT)

log('App listening on port 3000')

@@ -184,16 +184,28 @@
 API_PORT
+, PUBLIC_URL
  %7D = req
@@ -223,16 +223,16 @@
 nfig/')%0A
-
 %0Aconst a
@@ -345,23 +345,207 @@
 ns.%0A
-app.use(cors(%7B%0A
+let corsOpts%0Aif (process.env.NODE_ENV === 'prod') %7B%0A  corsOpts = %7B%0A    origin: PUBLIC_URL,%0A    credentials: true,%0A    methods: %5B'GET', 'PUT', 'POST', 'DELETE', 'PATCH'%5D%0A  %7D%0A%7D else %7B%0A  corsOpts = %7B%0A  
   cr
@@ -561,16 +561,18 @@
 : true,%0A
+  
   method
@@ -616,17 +616,44 @@
 PATCH'%5D%0A
-%7D
+  %7D%0A%7D%0A%0Aapp.use(cors(corsOpts
 ))%0A%0A// G

add cors for prod with origin