
// Configure Feathers app. (Can be re-generated.)
<%- insertFragment('preface') %>
<%- tplImports('path', null, 'as') %>
<%- tplImports('compress', 'compression', 'req') %>
<%- tplImports('cors', null, 'req') %>
<%- tplImports('helmet', null, 'req') %>
<%- tplImports('logger', './logger') %>

<%- insertFragment('favicon_import', [
  `${tplImports('favicon', 'serve-favicon', 'req')}`
]) %>

<%- tplImports('feathers', '@feathersjs/feathers') %>
<%# -%>
<%# --- if-1 starts below. -%>
<% if (appConfigPath !== 'config') { -%>
process.env['NODE_CONFIG_DIR'] = './<%- appConfigPath %>'<%- sc %>
<% } -%>
<%# --- if-1 ends above. -%>
<%- tplImports('configuration', '@feathersjs/configuration') %>
<%- tplImports('express', '@feathersjs/express') %>
<%- hasProvider('socketio') ? tplImports('socketio', '@feathersjs/socketio') : '' %>
<%- hasProvider('primus') ? tplImports('primus', '@feathersjs/primus') : '' %>
<%- tplImports('middleware', './middleware') %>
<%- tplImports('services', './services') %>
<%- tplImports('appHooks', './app.hooks') %>
<%- (hasProvider('socketio') || hasProvider('primus')) ? tplImports('channels', './channels') : '' %>
<%- tplTsOnly([`// ${lintDisableNextLine}`, '']) -%>
const generatorSpecs = require('../feathers-gen-specs.json')<%- sc %>
<%# -%>
<%# --- if-2 starts below. -%>
<% if (specs.authentication) { -%>
<%- tplImports('authentication', './authentication') %>
<% } -%>
<%# --- if-2 ends above. -%>

<%# -%>
<%# --- forEach-1 starts below. -%>
<% Object.keys(specs._adapters || []).sort().forEach(__adapter => {
  __temp = specs._adapters[__adapter];
  __module = __temp.substring(0, __temp.length - 4);
  __safeAdapter = __adapter === 'sequelize-mssql' ? 'sequelizeMssql' : __adapter;
-%>
<%- tplImports(__safeAdapter, `./${__module}`) %>
<% }) -%>
<%# --- forEach-1 ends above. -%>
<%- insertFragment('imports') %>
<%- insertFragment('init') %>

const app = express(feathers())<%- sc %>
<%- insertFragment('use_start') %>

// Load app configuration
app.configure(configuration())<%- sc %>
<%- insertFragment('init_config', [
  `app.set('generatorSpecs', generatorSpecs)${sc}`,
]) %>

// Enable security, CORS, compression, favicon and body parsing
app.use(helmet(
  <%- insertFragment('helmet_config') %>
))<%- sc %>
app.use(cors(
  <%- insertFragment('cors_config') %>
))<%- sc %>
app.use(compress(
  <%- insertFragment('compress_config') %>
))<%- sc %>
app.use(express.json(
  <%- insertFragment('json_config') %>
))<%- sc %>
app.use(express.urlencoded(
  <%- insertFragment('urlencoded_config', [
    '  { extended: true }'
  ]) %>
))<%- sc %>
<%- insertFragment('use_favicon', [
  '// Use favicon',
  `app.use(favicon(path.join(app.get('public'), 'favicon.ico')))${sc}`
]) %>
<%- insertFragment('use_static', [
  '// Host the public folder',
  `app.use('/', express.static(app.get('public')))${sc}`
]) %>
<%- insertFragment('use_end') %>

// Set up Plugins and providers
<%- insertFragment('config_start') %>
<%# -%>
<%# --- if-90a starts below. -%>
<% if (hasProvider('rest')) { -%>
app.configure(express.rest(
  <%- insertFragment('express_rest') %>
))<%- sc %>
<% } -%>
<%# --- if-90a ends above. -%>
<%# -%>
<%# --- if-90b starts below. -%>
<% if (hasProvider('socketio')) { -%>
app.configure(socketio(
  <%- insertFragment('express_socketio') %>
))<%- sc %>
<% } -%>
<%# --- if-90b ends above. -%>
<%# -%>
<%# --- if-90c starts below. -%>
<% if (hasProvider('primus')) { -%>
app.configure(primus(
  <%- insertFragment('express_socketio', '  { transformer: \'websockets\' }') %>
))<%- sc %>
<% } -%>
<%# --- if-90c ends above. -%>
<%# --- forEach-3 starts below. -%>
<% Object.keys(specs._adapters || []).sort().forEach((__adapter, __i) => {
  __safeAdapter = __adapter === 'sequelize-mssql' ? 'sequelizeMssql' : __adapter;
-%>
<%- __i === 0 ? '// Configure database adapters\n' : '' -%>
app.configure(<%- __safeAdapter %>)<%- sc %>
<% }) -%>
<%# --- forEach-3 ends above. -%>

// Configure other middleware (see `middleware/index.<%- js %>`)
app.configure(middleware)<%- sc %>
<%# -%>
<%# --- if-2 starts below. -%>
<% if (specs.authentication) { -%>
// Configure authentication (see `authentication.<%- js %>`)
app.configure(authentication)<%- sc %>
<% } -%>
<%# --- if-2 ends above. -%>
// Set up our services (see `services/index.<%- js %>`)
app.configure(services)<%- sc %>
<%# --- if-4 starts below. -%>
<% if (hasProvider('primus') || hasProvider('socketio')) { -%>
// Set up event channels (see channels.<%- js %>)
app.configure(channels)<%- sc %>
<% } -%>
<%# --- if-4 ends above. -%>
<%- insertFragment('config_middle') %>

// Configure a middleware for 404s and the error handler
app.use(express.notFound())<%- sc %>
app.use(express.errorHandler({ logger }))<%- sc %>
<%- insertFragment('config_end') %>

app.hooks(appHooks)<%- sc %>

const moduleExports = app<%- sc %>
<%- insertFragment('exports') %>
<%- `${tplExport('moduleExports')}${sc}` %>

<%- insertFragment('funcs') %>
<%- insertFragment('end') %>
