-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Nomenclature-for-Developer-Estimates-plus-Fo…
…rm-behavior
- Loading branch information
Showing
102 changed files
with
3,576 additions
and
2,730 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
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
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
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,59 +1,162 @@ | ||
import 'dotenv/config' | ||
import express from 'express'; | ||
import express, { Express } from 'express'; | ||
import rateLimit from 'express-rate-limit'; | ||
import bodyParser from 'body-parser'; | ||
import cors from 'cors'; | ||
import path, { dirname } from 'path'; | ||
import apiRoutes from "./routes/index.js" | ||
import { dbConnect } from './database.js'; | ||
import setup from './services/setup.js'; | ||
import settingsService from './services/settings.service.js'; | ||
import SmeeService from './services/smee.js'; | ||
import logger, { expressLoggerMiddleware } from './services/logger.js'; | ||
import { fileURLToPath } from 'url'; | ||
import * as http from 'http'; | ||
import Database from './database.js'; | ||
import logger, { expressLoggerMiddleware } from './services/logger.js'; | ||
import GitHub from './github.js'; | ||
import SettingsService from './services/settings.service.js'; | ||
import apiRoutes from "./routes/index.js" | ||
import WebhookService from './services/smee.js'; | ||
|
||
class App { | ||
e: Express; | ||
eListener?: http.Server; | ||
baseUrl?: string; | ||
public database: Database; | ||
public github: GitHub; | ||
public settingsService: SettingsService; | ||
|
||
const PORT = Number(process.env.PORT) || 80; | ||
|
||
export const app = express(); | ||
app.use(cors()); | ||
app.use(expressLoggerMiddleware); | ||
|
||
(async () => { | ||
await dbConnect(); | ||
logger.info('DB Connected ✅'); | ||
await settingsService.initializeSettings(); | ||
logger.info('Settings loaded ✅'); | ||
await SmeeService.createSmeeWebhookProxy(PORT); | ||
logger.info('Created Smee webhook proxy ✅'); | ||
|
||
try { | ||
await setup.createAppFromEnv(); | ||
logger.info('Created GitHub App from environment ✅'); | ||
} catch (error) { | ||
logger.info('Failed to create app from environment. This is expected if the app is not yet installed.', error); | ||
constructor( | ||
public port: number | ||
) { | ||
this.baseUrl = process.env.BASE_URL || 'http://localhost:' + port; | ||
this.e = express(); | ||
this.port = port; | ||
this.database = new Database(process.env.JAWSDB_URL ? process.env.JAWSDB_URL : { | ||
host: process.env.MYSQL_HOST, | ||
port: Number(process.env.MYSQL_PORT) || 3306, | ||
username: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD, | ||
database: process.env.MYSQL_DATABASE || 'value' | ||
}); | ||
const webhookService = new WebhookService({ | ||
url: process.env.WEBHOOK_PROXY_URL, | ||
path: '/api/github/webhooks', | ||
port | ||
}); | ||
this.github = new GitHub( | ||
{ | ||
appId: process.env.GITHUB_APP_ID, | ||
privateKey: process.env.GITHUB_APP_PRIVATE_KEY, | ||
webhooks: { | ||
secret: process.env.GITHUB_WEBHOOK_SECRET | ||
} | ||
}, | ||
this.e, | ||
webhookService, | ||
this.baseUrl | ||
) | ||
this.settingsService = new SettingsService({ | ||
baseUrl: this.baseUrl, | ||
webhookProxyUrl: process.env.GITHUB_WEBHOOK_PROXY_URL, | ||
webhookSecret: process.env.GITHUB_WEBHOOK_SECRET, | ||
metricsCronExpression: '0 0 * * *', | ||
devCostPerYear: '100000', | ||
developerCount: '100', | ||
hoursPerYear: '2080', | ||
percentTimeSaved: '20', | ||
percentCoding: '20' | ||
}) | ||
} | ||
|
||
app.use((req, res, next) => { | ||
if (req.path === '/api/github/webhooks') { | ||
return next(); | ||
} | ||
bodyParser.json()(req, res, next); | ||
}, bodyParser.urlencoded({ extended: true })); | ||
app.use('/api', apiRoutes); | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const frontendPath = path.resolve(__dirname, '../../frontend/dist/github-value/browser'); | ||
|
||
app.use(express.static(frontendPath)); | ||
app.get('*', rateLimit({ | ||
windowMs: 15 * 60 * 1000, max: 5000, | ||
}), (_, res) => res.sendFile(path.join(frontendPath, 'index.html'))); | ||
|
||
app.listen(PORT, () => { | ||
logger.info(`Server is running at http://localhost:${PORT} 🚀`); | ||
if (process.env.WEB_URL) { | ||
logger.debug(`Frontend is running at ${process.env.WEB_URL} 🚀`); | ||
public async start() { | ||
try { | ||
logger.info('Starting application'); | ||
|
||
logger.info('Express setup...'); | ||
this.setupExpress(); | ||
logger.info('Express setup complete'); | ||
|
||
logger.info('Database connecting...'); | ||
await this.database.connect(); | ||
logger.info('Database connected'); | ||
|
||
logger.info('Initializing settings...'); | ||
await this.initializeSettings(); | ||
logger.info('Settings initialized'); | ||
|
||
logger.info('GitHub App starting...'); | ||
await this.github.connect(); | ||
logger.info('GitHub App connected'); | ||
|
||
return this.e; | ||
} catch (error) { | ||
await this.github.smee.connect(); | ||
logger.error('Failed to start application ❌'); | ||
if (error instanceof Error) { | ||
logger.error(error.message); | ||
} | ||
logger.debug(error); | ||
} | ||
}); | ||
})(); | ||
} | ||
|
||
public async stop() { | ||
await new Promise(resolve => this.eListener?.close(resolve)); | ||
await this.database.disconnect(); | ||
await this.github.disconnect(); | ||
} | ||
|
||
private setupExpress() { | ||
this.e.use(cors()); | ||
this.e.use(expressLoggerMiddleware); | ||
this.e.use((req, res, next) => { | ||
if (req.path === '/api/github/webhooks') { | ||
return next(); | ||
} | ||
bodyParser.json()(req, res, next); | ||
}, bodyParser.urlencoded({ extended: true })); | ||
|
||
this.e.use('/api', apiRoutes); | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const frontendPath = path.resolve(__dirname, '../../frontend/dist/github-value/browser'); | ||
this.e.use(express.static(frontendPath)); | ||
this.e.get( | ||
'*', | ||
rateLimit({ | ||
windowMs: 15 * 60 * 1000, max: 5000, | ||
}), | ||
(_, res) => res.sendFile(path.join(frontendPath, 'index.html')) | ||
); | ||
|
||
this.eListener = this.e.listen(this.port); | ||
} | ||
|
||
private initializeSettings() { | ||
this.settingsService.initialize() | ||
.then(async (settings) => { | ||
if (settings.webhookProxyUrl) { | ||
this.github.smee.options.url = settings.webhookProxyUrl | ||
} | ||
if (settings.webhookSecret) { | ||
this.github.setInput({ | ||
webhooks: { | ||
secret: settings.webhookSecret | ||
} | ||
}); | ||
} | ||
if (settings.metricsCronExpression) { | ||
this.github.cronExpression = settings.metricsCronExpression; | ||
} | ||
if (settings.baseUrl) { | ||
this.baseUrl = settings.baseUrl; | ||
} | ||
}) | ||
.finally(async () => { | ||
await this.github.smee.connect() | ||
await this.settingsService.updateSetting('webhookSecret', this.github.input.webhooks?.secret || ''); | ||
await this.settingsService.updateSetting('webhookProxyUrl', this.github.smee.options.url!); | ||
await this.settingsService.updateSetting('metricsCronExpression', this.github.cronExpression!); | ||
}); | ||
} | ||
} | ||
|
||
export { | ||
App as default | ||
}; |
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
Oops, something went wrong.