|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +/** |
| 15 | + * Express webapp that generates files to a mounted NFS when deployed to Cloud Run. |
| 16 | + * |
| 17 | + * See https://cloud.google.com/run/docs/tutorials/network-filesystems-filestore before running the code snippet. |
| 18 | + */ |
| 19 | + |
| 20 | +const express = require('express'); |
| 21 | +const rateLimit = require('express-rate-limit'); |
| 22 | +const fs = require('fs/promises'); |
| 23 | +const app = express(); |
| 24 | +const mntDir = process.env.MNT_DIR || '/mnt/nfs/filestore'; |
| 25 | +const port = parseInt(process.env.PORT) || 8080; |
| 26 | +const limit = rateLimit({ |
| 27 | + // Use of rate limit to fullfill CodeQL rule js/missing-rate-limiting |
| 28 | + // HTTP request handlers should not perform expensive operations such as |
| 29 | + // accessing the file system. Setting rate limit to maximum 100 requests |
| 30 | + // per 15 minute window. |
| 31 | + windowMs: 15 * 60 * 1000, |
| 32 | + max: 100, |
| 33 | + message: 'Rate limit exceeded', |
| 34 | + headers: true, |
| 35 | +}); |
| 36 | + |
| 37 | +app.use(limit); |
| 38 | +app.use(mntDir, express.static(mntDir)); |
| 39 | + |
| 40 | +app.listen(port, () => { |
| 41 | + console.log(`Listening on port ${port}`); |
| 42 | +}); |
| 43 | + |
| 44 | +app.get(mntDir, async (req, res) => { |
| 45 | + // Have all requests to mount directory generate a new file on the filesystem. |
| 46 | + try { |
| 47 | + writeFile(mntDir); |
| 48 | + const html = await generateIndex(mntDir); |
| 49 | + // Respond with html with list of files on the filesystem. |
| 50 | + res.send(html); |
| 51 | + } catch (error) { |
| 52 | + console.error(error); |
| 53 | + res |
| 54 | + .status(500) |
| 55 | + .send( |
| 56 | + 'Something went wrong when writing to the filesystem. Refresh the page to try again.' |
| 57 | + ); |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +app.all('*', (req, res) => { |
| 62 | + // Redirect all requests to the mount directory |
| 63 | + res.redirect(mntDir); |
| 64 | +}); |
| 65 | + |
| 66 | +const writeFile = async (path, filePrefix = 'test') => { |
| 67 | + // Write a test file to the provided path. |
| 68 | + const date = new Date(); |
| 69 | + const formattedDate = date.toString().split(' ').slice(0, 5).join('-'); |
| 70 | + const filename = `${filePrefix}-${formattedDate}.txt`; |
| 71 | + const contents = `This test file was created on ${formattedDate}.\n`; |
| 72 | + |
| 73 | + try { |
| 74 | + const newFile = fs.writeFile(`${path}/${filename}`, contents); |
| 75 | + await newFile; |
| 76 | + } catch (error) { |
| 77 | + console.error(error); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +const generateIndex = async mntDir => { |
| 82 | + // Return html for page with a list of files on the mounted filesystem. |
| 83 | + try { |
| 84 | + const header = |
| 85 | + '<html><body>A new file is generated each time this page is reloaded.<p>Files created on filesystem:<p>'; |
| 86 | + const footer = '</body></html>'; |
| 87 | + // Get list of files on mounted filesystem. |
| 88 | + const existingFiles = await fs.readdir(mntDir); |
| 89 | + // Insert each file into html content |
| 90 | + const htmlBody = existingFiles.map(fileName => { |
| 91 | + const sanitized = encodeURIComponent(fileName); |
| 92 | + return `<a href="${mntDir}/${sanitized}">${decodeURIComponent( |
| 93 | + sanitized |
| 94 | + )}</a><br>`; |
| 95 | + }); |
| 96 | + return header + htmlBody.join(' ') + footer; |
| 97 | + } catch (error) { |
| 98 | + console.log(error); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +process.on('SIGTERM', () => { |
| 103 | + console.log('Received SIGTERM signal. Exiting.'); |
| 104 | +}); |
| 105 | + |
| 106 | +module.exports = app; |
0 commit comments