Skip to content

Commit

Permalink
fix: migrate account.js to firestore
Browse files Browse the repository at this point in the history
Closes #177
  • Loading branch information
stdavis committed Jun 3, 2024
1 parent ec90b94 commit cb55ba4
Show file tree
Hide file tree
Showing 16 changed files with 1,757 additions and 154 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "ut-dts-agrc-print-proxy-dev"
}
}
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
run: npm run lint

- name: 🧪 Run tests
run: npm run test
run: npm run test:ci
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
.DS_Store
.env
*.pem
*.log
.emulator-data/
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"cloudrun",
"entrypoint",
"enviro",
"Firestore",
"gserviceaccount",
"mkcert",
"packagejson",
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This project solves this problem by acting as a proxy between web applications a

## Usage

1. [Open a pull request](https://github.com/agrc/serverless-print-proxy/compare) to add a configuration for a new [account](./accounts.js).
1. Send an email to [ugrc-developers@utah.gov](mailto:ugrc-developers@utah.gov) with your locked down quad-word to request a new account number.

1. Take the path to your print service...

Expand All @@ -23,7 +23,7 @@ This project solves this problem by acting as a proxy between web applications a
https://<b>utility.arcgisonline.com</b>/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task
</pre>

...and replace the domain name with `print.ugrc.utah.gov/v2/<account-number>` like this...
...and replace the domain name with `print.ugrc.utah.gov/v2/<your-account-number>` like this...

<pre>
https://<b>print.ugrc.utah.gov/v2/99</b>/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task
Expand All @@ -44,10 +44,11 @@ _The account number must have a corresponding key in [`accounts.js`](./accounts.
1. Create `.env` file with print proxy wide open quad-word.
1. `touch .env && echo 'OPEN_QUAD_WORD=<wide-open-quad-word>' >> .env`
1. `npm install` & `npm start`
1. `node scripts/seed-firestore-emulator.js`

## Testing

Run `npm test` to run tests.
Run `npm test` to run tests after starting the emulator with `npm run start:firestore`.

There's also an [AGOL web app](https://experience.arcgis.com/experience/2ade141aca3244ee99b8e16185a76f32) that you can test the different environments with.

Expand Down
101 changes: 0 additions & 101 deletions accounts.js

This file was deleted.

15 changes: 15 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"firestore": {
"port": 8081
},
"ui": {
"enabled": true
},
"singleProjectMode": true
}
}
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
7 changes: 7 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
}
}
}
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Firestore } from '@google-cloud/firestore';
import cors from 'cors';
import 'dotenv/config';
import express from 'express';
import fs from 'fs';
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
import https from 'https';
import accounts from './accounts.js';

const firestore = new Firestore();

export const WEB_MAP_AS_JSON = 'Web_Map_as_JSON';

Expand All @@ -29,11 +31,16 @@ app.use(

app.use(express.json());

async function getAccountSnapshot(accountNumber) {
return await firestore.collection('accounts').doc(accountNumber).get();
}

// verify account number for all paths requests except "/"
app.use((request, response, next) => {
app.use(async (request, response, next) => {
if (request.path !== '/') {
const accountNumber = request.path.split('/')[2];
if (!accounts[accountNumber]) {
const snapshot = await getAccountSnapshot(accountNumber);
if (!snapshot.exists) {
response.status(400);
return response.send(`Invalid account number: ${accountNumber}`);
}
Expand All @@ -45,7 +52,7 @@ app.use((request, response, next) => {
method: request.method,
});

response.locals.account = accounts[accountNumber];
response.locals.account = snapshot.data();
}

return next();
Expand Down Expand Up @@ -78,10 +85,11 @@ app.use(
fixRequestBody(proxyReq, request);
},
},
router: (request) => {
router: async (request) => {
// response is not passed to this function
const accountNumber = request.path.split('/')[1];
const url = new URL(accounts[accountNumber].arcgisServer);
const snapshot = await getAccountSnapshot(accountNumber);
const url = new URL(snapshot.data().arcgisServer);

return {
protocol: url.protocol,
Expand Down
6 changes: 3 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('async print task', () => {
let jobId;
test('submitJob', () => {
return request(server)
.post('/v2/11/arcgis/rest/services/WRI/Print/GPServer/Export%20Web%20Map/submitJob')
.post('/v2/-3/arcgis/rest/services/WRI/Print/GPServer/Export%20Web%20Map/submitJob')
.type('form')
.send({
f: 'json',
Expand All @@ -149,7 +149,7 @@ describe('async print task', () => {

test('check job requests', () => {
return request(server)
.get(`/v2/11/arcgis/rest/services/WRI/Print/GPServer/Export%20Web%20Map/jobs/${jobId}`)
.get(`/v2/-3/arcgis/rest/services/WRI/Print/GPServer/Export%20Web%20Map/jobs/${jobId}`)
.query({ f: 'json' })
.expect(/jobStatus/)
.expect(200);
Expand All @@ -159,7 +159,7 @@ describe('async print task', () => {
await sleep(500);
request(server)
.get(
`/v2/11/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task/jobs/${jobId}/results/Output_File`,
`/v2/-3/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task/jobs/${jobId}/results/Output_File`,
)
.query({
f: 'json',
Expand Down
Loading

0 comments on commit cb55ba4

Please sign in to comment.