const path = require('path')
const Fastify = require('fastify')
const pointOfView = require('@fastify/view')
const static = require('@fastify/static')
const ejs = require('ejs')

const fastify = Fastify({ logger: true })

fastify.register(pointOfView, {
  engine: { ejs },
  root: path.join(__dirname, 'views'),
  viewExt: 'ejs',
})

fastify.register(static, {
  root: path.join(__dirname, 'public'),
  prefix: '/public/',
  decorateReply: true
})

fastify.get('/', async (request, reply) => {
  return reply.view('index')
})

const start = async () => {
  try {
    await fastify.listen({ port: 3000, host: '0.0.0.0' })
    fastify.log.info(`Server listening on ${fastify.server.address().port}`)
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}

start()
