Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API data gov proxy for -demo app #908

Merged
merged 2 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifests/base.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
- crime-data-api-creds
- crime-data-creds
env:
GITHUB_ISSUE_REPO_OWNER: '18f'
GITHUB_ISSUE_REPO_NAME: 'crime-data-explorer'
Expand Down
4 changes: 3 additions & 1 deletion manifests/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ applications:
memory: 512M
domain: fr.cloud.gov
env:
CDE_API: 'https://crime-data-api-demo.fr.cloud.gov'
CDE_API: 'https://api.usa.gov/crime/fbi/ucr'
services:
- crime-data-api-key-production
2 changes: 2 additions & 0 deletions manifests/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ applications:
domain: fr.cloud.gov
env:
CDE_API: 'https://crime-data-api.fr.cloud.gov'
services:
- crime-data-api-key-development
2 changes: 2 additions & 0 deletions manifests/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ applications:
domain: fr.cloud.gov
env:
CDE_API: 'https://crime-data-api.fr.cloud.gov'
services:
- crime-data-api-key-development
35 changes: 15 additions & 20 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'babel-polyfill'
import http from 'axios'
import basicAuth from 'basic-auth-connect'
import bodyParser from 'body-parser'
import cfenv from 'cfenv'
import express from 'express'
import gzipStatic from 'connect-gzip-static'
import path from 'path'
Expand All @@ -19,35 +18,31 @@ import renderHtml from './html'
import routes from './routes'
import configureStore from './store'
import { updateFilters } from './actions/filters'
import createEnv from './util/env'
import { createIssue } from './util/github'
import history from './util/history'

const isProd = process.env.NODE_ENV === 'production'

if (isProd) require('newrelic')

const env = cfenv.getAppEnv()

const credService = env.getService('crime-data-api-creds') || {
credentials: {},
}
const getEnvVar = name => {
if (credService.credentials[name]) return credService.credentials[name]
if (process.env[name]) return process.env[name]
return false
}

const { HTTP_BASIC_USERNAME, HTTP_BASIC_PASSWORD } = credService.credentials
const API = process.env.CDE_API
const apiKey = getEnvVar('API_KEY')
const ENV = createEnv()

const {
CDE_API: API,
API_KEY: apiKey,
GITHUB_ISSUE_REPO_OWNER: repoOwner,
GITHUB_ISSUE_REPO_NAME: repoName,
GITHUB_ISSUE_BOT_TOKEN: repoToken,
HTTP_BASIC_USERNAME,
HTTP_BASIC_PASSWORD,
PORT,
} = ENV
const initState = {
agency: { loading: true },
ucr: { loading: true, data: {} },
summaries: { loading: true, data: {} },
}
const repoOwner = getEnvVar('GITHUB_ISSUE_REPO_OWNER')
const repoName = getEnvVar('GITHUB_ISSUE_REPO_NAME')
const repoToken = getEnvVar('GITHUB_ISSUE_BOT_TOKEN')

const acceptHostname = hostname => {
if (!isProd) return true
Expand Down Expand Up @@ -127,6 +122,6 @@ app.get('/*', (req, res) => {
})
})

app.listen(env.port, () => {
console.log(`Listening on ${env.port}`)
app.listen(PORT, () => {
console.log(`Listening on ${PORT}`)
})
23 changes: 23 additions & 0 deletions src/util/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import cfenv from 'cfenv'

const env = cfenv.getAppEnv()

const combineServiceCredentials = services =>
services.map(service => service.credentials || {}).reduce((a, n) => ({
...a,
...n,
}))

const combineCfCUPSAndEnv = () => {
const services = Object.keys(env.getServices()).map(service =>
env.getService(service),
)
const combined = combineServiceCredentials(services)

return {
...process.env,
...combined,
}
}

export default combineCfCUPSAndEnv