Skip to content

Commit

Permalink
only load /:repo paths in dev env
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed May 24, 2024
1 parent 434807a commit e3fa547
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"start": "node server.mjs",
"dev": "GOOGLE_APPLICATION_CREDENTIALS=key.json BUCKET_NAME=gen-website-private PORT=3000 nodemon server.mjs",
"dev": "GOOGLE_APPLICATION_CREDENTIALS=key.json BUCKET_NAME=gen-website-private ENV=dev PORT=3000 nodemon server.mjs",
"dev-rsync": "gcloud storage rsync --recursive --cache-control 'public, max-age=60' ./public gs://gen-website-private/probcomp/test-repo"
},
"devDependencies": {
Expand Down
21 changes: 9 additions & 12 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ const handleFileRequest = async (repo, filePath, res) => {
// If there is no extension, serve the main repo index.html
filePath = 'index.html';
}
console.log('modified filePath', filePath)

// Construct the full path in the bucket
const bucketPath = path.join(BUCKET_PREFIX, repo, filePath);

console.log('handleFileRequest', {repo, filePath, bucketPath})

try {
// Serve HTML files directly, otherwise redirect to a signed URL
if (filePath.endsWith('.html')) {
Expand All @@ -101,22 +98,22 @@ const handleFileRequest = async (repo, filePath, res) => {
}
};

app.get('/:repo/*', async (req, res) => {
await handleFileRequest(req.params.repo, req.params[0], res);
});

app.get('/:repo', async (req, res) => {
await handleFileRequest(req.params.repo, '', res);
});
if (process.env.ENV == 'dev') {
app.get('/:repo/*', async (req, res) => {
await handleFileRequest(req.params.repo, req.params[0], res);
});

app.get('/:repo', async (req, res) => {
await handleFileRequest(req.params.repo, '', res);
});
}

app.get('/*', async (req, res) => {
const host = req.hostname;
console.log('host', host)
const match = host.match(HOST_REPO_REGEX);
if (match) {
const repo = match[1];
const filePath = req.params[0];
console.log('repo', repo, 'filePath', filePath)
await handleFileRequest(repo, filePath, res);
} else {
res.status(404).send('Not Found');
Expand Down

0 comments on commit e3fa547

Please sign in to comment.