<%
// "custom_routes.ts" => "custom_routes"
function removeExtension(filename) {
    return filename.replace(/\.ts$/, '')
}
-%>
import express from 'express'
import { NextFunction, Request, Response } from 'express'
import mysql from 'mysql'

const routes = require('./routes')
<% customRouteFilenames.forEach(filename => {
    const routerName = removeExtension(filename)
-%>
const <%- routerName %> = require('./<%- routerName %>')
<% }) -%>

const app = express()
app.use(express.json())

const pool = mysql.createPool({
    connectionLimit: 2,
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    // Support of named placeholders (https://github.com/mysqljs/mysql#custom-format)
    queryFormat: function(this: mysql.Pool, query, values) {
        if (!values) {
            return query
        }
<%# See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_the_replacement -%>
        return query.replace(/\:(\w+)/g, function(this: mysql.Pool, matchedSubstring: string, capturedValue: string) {
            if (values.hasOwnProperty(capturedValue)) {
                return this.escape(values[capturedValue])
            }
            return matchedSubstring
        }.bind(this))
    },
<%# LATER: add it only when there is at least one DTO with boolean type -%>
    // Support for conversion from TINYINT(1) to boolean (https://github.com/mysqljs/mysql#custom-type-casting)
    typeCast: function(field, next) {
        if (field.type === 'TINY' && field.length === 1) {
            return field.string() === '1'
        }
        return next()
    }
})

routes.register(app, pool)
<% customRouteFilenames.forEach(filename => {
    const routerName = removeExtension(filename)
-%>
<%- routerName %>.register(app, pool)
<% }) -%>

app.use((error: any, req: Request, res: Response, next: NextFunction) => {
    console.error(error)
    res.status(500).json({ "error": "Internal Server Error" })
})

const port = process.env.PORT || 3000
app.listen(port, () => {
    console.log(`Listen on ${port}`)
})
