import app from './express'
import API from './api'
import useSSR from './ssr'
import chalk from 'chalk'

const {BASE_API, PORT} = process.env
// Add API route
app.use(BASE_API, API)
// Add SSR handler
app.use(useSSR)
// Start server
app.listen(PORT, () => {
	console.log(chalk.magenta(`\nServer is running on ${PORT} port!\n`))
})

/*
 * @flow
 */
import addMiddlewares from './middlewares'
import API from './api'
import SSR from './ssr'
import fetch from 'isomorphic-fetch'

global.fetch = fetch

/**
 * Mount API, SSR and middlewares to app.
 * @param  {Object} app - Express server instance
 * @return {Object}     - Decorated server instance
 */
export default (app: Object): Object => {
	// Add global middlewares
	addMiddlewares(app)
	// Add API
	app.use('/api/v1', API)
	// Add SSR
	app.use(SSR)
	return app
}

add `server.js` which mounts middlewares, ssr, api