1+ const express = require ( 'express' ) ;
2+ const rateLimit = require ( 'express-rate-limit' ) ;
3+ const cors = require ( 'cors' ) ;
4+ const path = require ( 'path' ) ;
5+ const app = express ( ) ;
6+ require ( 'dotenv' ) . config ( ) ;
7+
8+ app . engine ( '.ejs' , require ( 'ejs' ) . renderFile ) ;
9+ app . set ( 'view engine' , 'ejs' ) ;
10+ app . set ( 'views' , path . join ( __dirname , 'frontend/pages' ) ) ;
11+ app . use ( express . static ( path . join ( __dirname , 'frontend/public' ) , { redirect : false } ) ) ;
12+ app . use ( express . urlencoded ( { extended : true } ) ) ;
13+ app . use ( express . json ( ) ) ;
14+ app . use ( cors ( ) ) ;
15+ app . use ( rateLimit ( {
16+ windowMs : 60 * 1000 ,
17+ max : 15 ,
18+ skip : ( req , res ) => {
19+ return req . ip !== undefined ;
20+ } ,
21+ message : "Too many requests from this IP, please try again in a minute."
22+ } ) ) ;
23+
24+ let on = false ;
25+ let tenant = { } ;
26+ let returnHandler = null ;
27+ let user = { } ;
28+
29+ function init ( {
30+ tenant : newTenant = {
31+ slug : 'commtrackr' ,
32+ name : 'CommTrackr' ,
33+ description : 'This is a default tenant configuration.' ,
34+ auth : {
35+ enabled : false ,
36+ provider : '' ,
37+ url : '' ,
38+ } ,
39+ } ,
40+ handler : newHandler = null
41+ } ) {
42+ tenant = {
43+ slug : 'commtrackr' ,
44+ name : 'CommTrackr' ,
45+ description : 'This is a default tenant configuration.' ,
46+ auth : {
47+ enabled : false ,
48+ provider : '' ,
49+ url : '' ,
50+ } ,
51+ ...newTenant
52+ } ;
53+ returnHandler = newHandler ;
54+ } ;
55+
56+ function activate ( isOn = true ) {
57+ on = isOn ;
58+ if ( ! on ) user = { } ;
59+ } ;
60+
61+ function setUser ( newUser = { } ) {
62+ if ( ! newUser . id ) return user = { } ;
63+ user = newUser ;
64+ } ;
65+
66+ app . get ( '/' , async ( req , res ) => {
67+ if ( ! on ) return res . send ( 'Service is not active' ) ;
68+ if ( ! tenant . slug ) return res . send ( 'No tenant configured' ) ;
69+ if ( tenant . auth && tenant . auth . enabled && ! user . id ) return res . send ( 'Authentication required' ) ;
70+ res . send ( tenant . slug ) ;
71+ } ) ;
72+
73+ module . exports = {
74+ routes : app ,
75+ init,
76+ activate,
77+ setUser
78+ } ;
0 commit comments