UNPKG

yekonga-server

Version:
165 lines (142 loc) 5.04 kB
// @ts-nocheck /*global Yekonga, serverLibrary */ const H = Yekonga.Helper; const express = serverLibrary.express; const path = serverLibrary.path; const fs = serverLibrary.fs; const cors = serverLibrary.cors; const url = serverLibrary.url; const ApolloServer = serverLibrary.ApolloServer; const expressMiddleware = serverLibrary.expressMiddleware; const ApolloServerPluginDrainHttpServer = serverLibrary.ApolloServerPluginDrainHttpServer; // const gql = serverLibrary.gql; const mainRoute = '/yekonga'; const adminPath = '../../public/admin'; const app = express.Router(); const mainApp = express.Router(); var defaultFile = null; app.use(express.static(path.join(__dirname, adminPath))); app.use(express.static(path.join(__dirname, adminPath, 'application'))); // The GraphQL endpoint const SchemaAuth = new ApolloServer({ typeDefs: [ (fs.readFileSync(path.join(__dirname, 'graphql/auth/typeDefs.graphql'), { encoding: 'utf8' })), ], resolvers: require(path.join(__dirname, 'graphql/auth/resolvers')), introspection: true, playground: { endpoint: `${mainRoute}/api/auth`, settings: { 'editor.theme': 'dark' } }, formatError: Yekonga.Graphql.Handler.error, formatResponse: Yekonga.Graphql.Handler.response, plugins: [ApolloServerPluginDrainHttpServer({ httpServer: Yekonga.serverSetup })], }); const resolvers = { ...Yekonga.Graphql.Resolvers } const adminResolvers = require(path.join(__dirname, 'graphql/resolvers')); for (const key in resolvers) { if (Object.hasOwnProperty.call(resolvers, key)) { if (!adminResolvers[key]) { adminResolvers[key] = resolvers[key]; } else { for (const key2 in resolvers[key]) { if (Object.hasOwnProperty.call(resolvers[key], key2)) { if (!adminResolvers[key][key2]) { adminResolvers[key][key2] = resolvers[key][key2]; } } } } } } const Schema = new ApolloServer({ typeDefs: [ (fs.readFileSync(path.join(__dirname, 'graphql/typeDefs.graphql'), { encoding: 'utf8' })), ...Yekonga.Graphql.typeDefs, ], resolvers: adminResolvers, introspection: true, playground: { endpoint: `${mainRoute}/api`, settings: { 'editor.theme': 'dark' } }, formatError: Yekonga.Graphql.Handler.error, formatResponse: Yekonga.Graphql.Handler.response, plugins: [ApolloServerPluginDrainHttpServer({ httpServer: Yekonga.serverSetup })], }); const dashboardRequest = (req, res) => { let route = Yekonga.Config.dashboardRoute || mainRoute; let apiRoute = `${route}/api`; const frontApi = H.getApiRoute(); const frontAuth = H.getApiAuthRoute(); const html = fs.readFileSync(path.join(__dirname, adminPath, 'dashboard.html'), { encoding: 'utf8' }); content = Yekonga.Helper.textTemplate(html, { route: route, appName: Yekonga.Config.appName, baseHost: Yekonga.Client.origin, baseUrl: `${Yekonga.Client.proto}://${Yekonga.Client.origin}`, baseProto: Yekonga.Client.proto, baseApi: apiRoute, baseAuth: `${apiRoute}/auth`, frontApi: frontApi, frontAuth: frontAuth, }); return res.status(200).send(content); } app.get('/', dashboardRequest); app.get('/dashboard', dashboardRequest); app.get('/health', (req, res)=>{ res.send('Ok!'); }); module.exports = async function init() { mainApp.use(mainRoute, app); await SchemaAuth.start(); // @ts-ignore // SchemaAuth.applyMiddleware({ app: app, path: `/api/auth` }); app.use( '/api/auth', cors(), express.json(), expressMiddleware(SchemaAuth, { context: Yekonga.Helper.graphqlContext }), ); await Schema.start(); // @ts-ignore // Schema.applyMiddleware({ app: app, path: `/api` }); app.use( '/api', cors(), express.json(), expressMiddleware(Schema, { context: Yekonga.Helper.graphqlContext }), ); app.use(function(req, res) { if (!defaultFile) { defaultFile = path.join(__dirname, '../..', `public/404.html`); } if ( (req.header('accept') && req.header('accept').includes('json')) || (req.header('content-type') && req.header('content-type').includes('json')) ) { return res.status(404).json({ error: "Not found", code: 404 }); } var extname = path.extname(url.parse(req.url).pathname); if (!(extname == null || extname.trim() == '')) { return res.sendFile(defaultFile); } return dashboardRequest(req, res); }) return mainApp; }