Skip to content

Commit

Permalink
update create-app templates
Browse files Browse the repository at this point in the history
  • Loading branch information
4upz committed Jun 9, 2023
1 parent e640034 commit 8995b8a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
RecommendationsRequestValidationError,
} from '@cloud-carbon-footprint/common'

const apiLogger = new Logger('api')
const apiLogger = new Logger('API')

/**
* Returns the raw estimates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ import cors, { CorsOptions } from 'cors'

import { createRouter } from './api'
import auth from './auth'
import { Logger } from '@cloud-carbon-footprint/common'
import { Logger, configLoader } from '@cloud-carbon-footprint/common'
import MongoDbCacheManager from '@cloud-carbon-footprint/app/src/MongoDbCacheManager'

const port = process.env.PORT || 4000
const httpApp = express()
const serverLogger = new Logger('server')
const serverLogger = new Logger('Server')

if (process.env.NODE_ENV === 'production') {
httpApp.use(auth)
}

httpApp.use(helmet())

// Establish Mongo Connection if cache method selected
if (configLoader()?.CACHE_MODE === 'MONGODB') {
MongoDbCacheManager.createDbConnection()
}

if (process.env.ENABLE_CORS) {
const corsOptions: CorsOptions = {
optionsSuccessStatus: 200,
Expand All @@ -46,3 +52,13 @@ httpApp.listen(port, () =>
`Cloud Carbon Footprint Server listening at http://localhost:${port}`,
),
)

// Instructions for graceful shutdown
process.on('SIGINT', async () => {
if (configLoader()?.CACHE_MODE === 'MONGODB') {
await MongoDbCacheManager.mongoClient.close()
serverLogger.info('\nMongoDB connection closed')
}
serverLogger.info('Cloud Carbon Footprint Server shutting down...')
process.exit()
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import moment, { Moment } from 'moment'
import { inputPrompt, listPrompt } from '../common'
import { App, createValidFootprintRequest } from '@cloud-carbon-footprint/app'
import {
App,
createValidFootprintRequest,
MongoDbCacheManager,
} from '@cloud-carbon-footprint/app'
import { configLoader } from '@cloud-carbon-footprint/common'

/**
* Logs the progress of the estimate request based on the given date range
Expand Down Expand Up @@ -73,6 +78,10 @@ export default async function seedCacheFile(): Promise<void> {
}...`,
)

if (configLoader().CACHE_MODE === 'MONGODB') {
await MongoDbCacheManager.createDbConnection()
}

// Makes getCostAndEstimates requests in chunks based on request method and day frequency
while (currentDate.isSameOrBefore(endDate)) {
// Use the current date window as the inclusive start/end date of the request
Expand All @@ -92,6 +101,11 @@ export default async function seedCacheFile(): Promise<void> {
currentDate.add(daysPerRequest, 'day')
}

if (configLoader().CACHE_MODE === 'MONGODB') {
await MongoDbCacheManager.mongoClient.close()
console.info('MongoDB connection closed')
}

console.info(
`Done! Estimates have been successfully seeded to the cache file!`,
)
Expand Down
17 changes: 15 additions & 2 deletions packages/create-app/templates/default-app/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import moment from 'moment'
import path from 'path'
import * as process from 'process'

import { App, createValidFootprintRequest } from '@cloud-carbon-footprint/app'
import { EstimationResult } from '@cloud-carbon-footprint/common'
import {
App,
createValidFootprintRequest,
MongoDbCacheManager,
} from '@cloud-carbon-footprint/app'
import { EstimationResult, configLoader } from '@cloud-carbon-footprint/common'

import EmissionsByDayAndServiceTable from './EmissionsByDayAndServiceTable'
import EmissionsByServiceTable from './EmissionsByServiceTable'
Expand Down Expand Up @@ -64,6 +68,10 @@ export default async function cli(argv: string[] = process.argv) {
groupBy: 'day', // So that estimates are cached the same regardless of table grouping method
})

if (configLoader().CACHE_MODE === 'MONGODB') {
await MongoDbCacheManager.createDbConnection()
}

const { table, colWidths } = await new App()
.getCostAndEstimates(estimationRequest)
.then((estimations: EstimationResult[]) => {
Expand All @@ -76,6 +84,11 @@ export default async function cli(argv: string[] = process.argv) {
return EmissionsByDayAndServiceTable(estimations)
})

if (configLoader().CACHE_MODE === 'MONGODB') {
await MongoDbCacheManager.mongoClient.close()
console.log('MongoDB connection closed')
}

if (format === 'csv') {
const filePath = path.join(
process.cwd(),
Expand Down

0 comments on commit 8995b8a

Please sign in to comment.