-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eee444b
commit e2880c5
Showing
1 changed file
with
44 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,74 @@ | ||
/* eslint-disable global-require */ | ||
|
||
const path = require('path'); | ||
const Hapi = require('@hapi/hapi'); | ||
const Inert = require('@hapi/inert'); | ||
const Hapi = require('@auth0/hapi'); | ||
const Inert = require('inert'); | ||
const jwt = require('hapi-auth-jwt2'); | ||
const config = require('../lib/config'); | ||
const logger = require('../lib/logger'); | ||
const routes = require('./routes'); | ||
const defaultHandlers = require('./handlers'); | ||
const auth = require('./auth'); | ||
|
||
const createServer = async (handlers = defaultHandlers) => { | ||
const server = Hapi.server({ | ||
const createServer = (cb, handlers = defaultHandlers) => { | ||
const server = new Hapi.Server(); | ||
|
||
server.connection({ | ||
host: 'localhost', | ||
port: config('PORT'), | ||
routes: { | ||
cors: true, | ||
validate: {}, | ||
files: { | ||
relativeTo: path.join(__dirname, '../public') | ||
} | ||
} | ||
}); | ||
|
||
// Register plugins | ||
await server.register([jwt, Inert]); | ||
server.register([jwt, Inert], () => {}); | ||
|
||
// Define routes | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/js/{file*}', | ||
options: { auth: false }, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../public/js') | ||
} | ||
server.route({ | ||
method: 'GET', | ||
path: '/js/{file*}', | ||
config: { auth: false }, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../public/js') | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/css/{file*}', | ||
options: { auth: false }, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../public/css') | ||
} | ||
} | ||
}); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/css/{file*}', | ||
config: { auth: false }, | ||
handler: { | ||
directory: { | ||
path: path.join(__dirname, '../public/css') | ||
} | ||
} | ||
]); | ||
}); | ||
|
||
server.register([auth, handlers, routes], (err) => { | ||
// Use the server logger. | ||
logger.debug = (...args) => { | ||
server.log(['debug'], args.join(' ')); | ||
}; | ||
logger.info = (...args) => { | ||
server.log(['info'], args.join(' ')); | ||
}; | ||
logger.error = (...args) => { | ||
server.log(['error'], args.join(' ')); | ||
}; | ||
|
||
// Register additional plugins and routes | ||
await server.register([auth, handlers, routes]); | ||
if (err) { | ||
cb(err); | ||
} | ||
|
||
// Configure server logging | ||
logger.debug = (...args) => { | ||
server.log(['debug'], args.join(' ')); | ||
}; | ||
logger.info = (...args) => { | ||
server.log(['info'], args.join(' ')); | ||
}; | ||
logger.error = (...args) => { | ||
server.log(['error'], args.join(' ')); | ||
}; | ||
cb(null, server); | ||
}); | ||
|
||
return server; // Return the server instance directly | ||
return server; | ||
}; | ||
|
||
module.exports = createServer; |