Skip to content

Commit 83f1a64

Browse files
authored
feat(Authenticator): make db grabbing lazy in attempt to reduce cold start time
1 parent 0ef0ef6 commit 83f1a64

10 files changed

Lines changed: 265 additions & 209 deletions

File tree

.github/workflows/cloudrun.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ jobs:
2727
- name: Setup Cloud SDK
2828
uses: google-github-actions/setup-gcloud@v2
2929
with:
30-
project_id: drivebackup-276620
30+
project_id: ${{ secrets.GCP_PROJECT_ID }}
3131

3232
- name: Authorize Docker push
3333
run: gcloud auth configure-docker
3434

3535
- name: Build and Push Container
3636
run: |-
37-
docker build -t gcr.io/drivebackup-276620/authenticator:${{ github.sha }} .
38-
docker push gcr.io/drivebackup-276620/authenticator:${{ github.sha }}
37+
docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/authenticator:${{ github.sha }} .
38+
docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/authenticator:${{ github.sha }}
3939
4040
- name: Deploy to Cloud Run
4141
id: deploy
4242
uses: google-github-actions/deploy-cloudrun@v2
4343
with:
4444
service: authenticator
45-
image: gcr.io/drivebackup-276620/authenticator:${{ github.sha }}
45+
image: gcr.io/${{ secrets.GCP_PROJECT_ID }}/authenticator:${{ github.sha }}
4646
region: us-central1

Authenticator/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:22-slim
1+
FROM node:24-slim
22

33
WORKDIR /usr/src/app
44

Authenticator/app.js

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,36 @@
1-
const { cert, initializeApp } = require('firebase-admin/app');
2-
const { getFirestore } = require('firebase-admin/firestore');
3-
initializeApp({ credential: cert(JSON.parse(process.env.GOOGLE_CREDENTIALS)) });
4-
5-
const db = getFirestore();
6-
exports.db = db;
7-
81
const express = require('express');
9-
const compression = require('compression');
10-
11-
const pinRouter = require('./routes/pin');
12-
const providerRouter = require('./routes/provider');
13-
const callbackRouter = require('./routes/callback');
14-
const tokenRouter = require('./routes/token');
15-
162
let app = express();
173

18-
app.use(compression());
194
app.use(express.json());
205
app.use(express.urlencoded({ extended: false }));
216
app.use(express.static(__dirname + '/public'));
227

23-
const authMiddleware = (req, res, next) => {
24-
if (req.body.client_secret === process.env.AUTHENTICATOR_CLIENT_SECRET) {
25-
return next();
26-
}
27-
28-
res.send({ success: false, msg: 'request_unauthenticated' });
8+
let _db;
9+
function getDb() {
10+
if (_db) return _db;
11+
const { cert, initializeApp } = require('firebase-admin/app');
12+
const { getFirestore } = require('firebase-admin/firestore');
13+
initializeApp({ credential: cert(JSON.parse(process.env.GOOGLE_CREDENTIALS)) });
14+
_db = getFirestore();
15+
return _db;
2916
}
17+
exports.getDb = getDb;
3018

31-
app.use('/pin', authMiddleware, pinRouter);
32-
app.use('/provider', providerRouter);
33-
app.use('/callback', callbackRouter);
34-
app.use('/token', authMiddleware, tokenRouter);
35-
36-
const pages = {
37-
"/": "index.html",
38-
"/privacy-policy": "privacy-policy.html",
39-
"/about": "about.html"
19+
const authMiddleware = (req, res, next) => {
20+
if (req.body.client_secret === process.env.AUTHENTICATOR_CLIENT_SECRET) return next();
21+
res.send({ success: false, msg: 'request_unauthenticated' });
4022
};
4123

42-
app.get(Object.keys(pages), (req, res) => {
43-
res.sendFile(`${__dirname}/views/${pages[req.path]}`);
44-
});
24+
app.use('/pin', authMiddleware, (req, res, next) => require('./routes/pin')(req, res, next));
25+
app.use('/provider', (req, res, next) => require('./routes/provider')(req, res, next));
26+
app.use('/callback', (req, res, next) => require('./routes/callback')(req, res, next));
27+
app.use('/token', authMiddleware, (req, res, next) => require('./routes/token')(req, res, next));
28+
29+
const pages = { "/": "index.html", "/privacy-policy": "privacy-policy.html", "/about": "about.html" };
30+
app.get(Object.keys(pages), (req, res) => res.sendFile(`${__dirname}/views/${pages[req.path]}`));
4531

4632
app.get('/:user_code', async function (req, res) {
33+
const db = getDb();
4734
let docRef = await db.collection('pins').doc(req.params.user_code.toUpperCase()).get();
4835

4936
if (!docRef.exists) {
@@ -53,13 +40,13 @@ app.get('/:user_code', async function (req, res) {
5340
}
5441
})
5542

56-
app.listen(process.env.PORT, () => {
57-
console.log("App listening");
58-
})
59-
60-
setInterval(async () => {
61-
let pinsRef = db.collection("pins");
62-
let allInvalidRefs = await pinsRef.where("timestamp", "<", Date.now() - 300000).get();
43+
app.listen(process.env.PORT, () => console.log("App listening"));
6344

64-
if (!allInvalidRefs.empty) allInvalidRefs.forEach(doc => doc.ref.delete());
65-
}, 150000);
45+
setTimeout(() => {
46+
setInterval(async () => {
47+
if (!_db) return; // skip if firebase hasn't been touched yet
48+
const allInvalidRefs = await _db.collection("pins")
49+
.where("timestamp", "<", Date.now() - 300000).get();
50+
if (!allInvalidRefs.empty) allInvalidRefs.forEach(doc => doc.ref.delete());
51+
}, 150000);
52+
}, 20000);

Authenticator/dev.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const express = require('express');
2-
const compression = require('compression');
32

43
const app = express();
54

6-
app.use(compression());
75
app.use(express.json());
86
app.use(express.urlencoded({ extended: false }));
97
app.use(express.static(__dirname + '/public'));

0 commit comments

Comments
 (0)