|
| 1 | +const express = require('express'); |
| 2 | + |
| 3 | +const app = express(); |
| 4 | +const port = 8080; |
| 5 | + |
| 6 | +// Log all R2 environment variables on startup |
| 7 | +console.log('=== R2 Binding Environment Variables ==='); |
| 8 | +const r2EnvVars = Object.keys(process.env).filter(key => key.startsWith('R2_')); |
| 9 | +r2EnvVars.forEach(key => { |
| 10 | + console.log(`${key}=${process.env[key]}`); |
| 11 | +}); |
| 12 | + |
| 13 | +if (r2EnvVars.length === 0) { |
| 14 | + console.log(' No R2 environment variables found'); |
| 15 | +} else { |
| 16 | + console.log(` Found ${r2EnvVars.length} R2 environment variables`); |
| 17 | +} |
| 18 | + |
| 19 | +console.log('======================================='); |
| 20 | +console.log(` /mnt/data exists: ${require('fs').existsSync('/mnt/data')}`); |
| 21 | +console.log(` /mnt/logs exists: ${require('fs').existsSync('/mnt/logs')}`); |
| 22 | +console.log('====================================='); |
| 23 | + |
| 24 | +app.get('/r2-env-test', (req, res) => { |
| 25 | + console.log('\n=== R2 Environment Test ==='); |
| 26 | + |
| 27 | + const r2Config = {}; |
| 28 | + Object.keys(process.env).forEach(key => { |
| 29 | + if (key.startsWith('R2_')) { |
| 30 | + r2Config[key] = process.env[key]; |
| 31 | + console.log(`${key}=${process.env[key]}`); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + const response = { |
| 36 | + message: 'R2 Environment Variables', |
| 37 | + r2Config, |
| 38 | + mountPaths: { |
| 39 | + '/mnt/data': require('fs').existsSync('/mnt/data'), |
| 40 | + '/mnt/logs': require('fs').existsSync('/mnt/logs') |
| 41 | + }, |
| 42 | + timestamp: new Date().toISOString() |
| 43 | + }; |
| 44 | + |
| 45 | + console.log('Response:', JSON.stringify(response, null, 2)); |
| 46 | + console.log('===========================\n'); |
| 47 | + |
| 48 | + res.json(response); |
| 49 | +}); |
| 50 | + |
| 51 | +app.get('/file-test', async (req, res) => { |
| 52 | + console.log('\n=== R2 File Operations Test ==='); |
| 53 | + |
| 54 | + const results = {}; |
| 55 | + |
| 56 | + try { |
| 57 | + // Test data directory (read/write) |
| 58 | + const dataDir = '/mnt/data'; |
| 59 | + const testFile = path.join(dataDir, 'test-file.txt'); |
| 60 | + const testContent = `Hello R2! Written at ${new Date().toISOString()}`; |
| 61 | + |
| 62 | + console.log(`Testing write to ${testFile}`); |
| 63 | + |
| 64 | + try { |
| 65 | + await fs.writeFile(testFile, testContent); |
| 66 | + console.log('✓ Write successful'); |
| 67 | + |
| 68 | + const readContent = await fs.readFile(testFile, 'utf8'); |
| 69 | + console.log(`✓ Read successful: ${readContent}`); |
| 70 | + |
| 71 | + results.dataDirectory = { |
| 72 | + path: dataDir, |
| 73 | + writable: true, |
| 74 | + testFile: testFile, |
| 75 | + content: readContent |
| 76 | + }; |
| 77 | + } catch (writeError) { |
| 78 | + console.log(`✗ Write failed: ${writeError.message}`); |
| 79 | + results.dataDirectory = { |
| 80 | + path: dataDir, |
| 81 | + writable: false, |
| 82 | + error: writeError.message |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + // Test logs directory (read-only) |
| 87 | + const logsDir = '/mnt/logs'; |
| 88 | + const logFile = path.join(logsDir, 'test-log.txt'); |
| 89 | + |
| 90 | + console.log(`Testing write to ${logFile} (should fail - read-only)`); |
| 91 | + |
| 92 | + try { |
| 93 | + await fs.writeFile(logFile, 'This should fail'); |
| 94 | + console.log('✗ Unexpected: Write succeeded to read-only directory'); |
| 95 | + results.logsDirectory = { |
| 96 | + path: logsDir, |
| 97 | + readOnly: false, |
| 98 | + error: 'Expected read-only but write succeeded' |
| 99 | + }; |
| 100 | + } catch (readOnlyError) { |
| 101 | + console.log(`✓ Expected: Write failed to read-only directory: ${readOnlyError.message}`); |
| 102 | + results.logsDirectory = { |
| 103 | + path: logsDir, |
| 104 | + readOnly: true, |
| 105 | + message: 'Correctly prevented write to read-only mount' |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + // List directory contents |
| 110 | + try { |
| 111 | + const dataContents = await fs.readdir(dataDir); |
| 112 | + console.log(`Data directory contents: ${dataContents.join(', ')}`); |
| 113 | + results.dataDirectory.contents = dataContents; |
| 114 | + } catch (e) { |
| 115 | + console.log(`Could not list data directory: ${e.message}`); |
| 116 | + } |
| 117 | + |
| 118 | + try { |
| 119 | + const logsContents = await fs.readdir(logsDir); |
| 120 | + console.log(`Logs directory contents: ${logsContents.join(', ')}`); |
| 121 | + results.logsDirectory.contents = logsContents; |
| 122 | + } catch (e) { |
| 123 | + console.log(`Could not list logs directory: ${e.message}`); |
| 124 | + } |
| 125 | + |
| 126 | + } catch (error) { |
| 127 | + console.log(`File test error: ${error.message}`); |
| 128 | + results.error = error.message; |
| 129 | + } |
| 130 | + |
| 131 | + console.log('File test results:', JSON.stringify(results, null, 2)); |
| 132 | + console.log('===============================\n'); |
| 133 | + |
| 134 | + res.json({ |
| 135 | + message: 'R2 File Operations Test Results', |
| 136 | + results, |
| 137 | + timestamp: new Date().toISOString() |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +app.get('/r2-binding-test', (req, res) => { |
| 142 | + console.log('\n=== R2 Binding Test ==='); |
| 143 | + |
| 144 | + // Use Container class helper methods for clean UX |
| 145 | + const bindingInfo = global.container.getR2BindingInfo(); |
| 146 | + const validation = global.container.validateR2BindingEnvironment(); |
| 147 | + const summary = global.container.getR2BindingSummary(); |
| 148 | + |
| 149 | + console.log('R2 Binding Summary:'); |
| 150 | + console.log(` Total bindings configured: ${summary.configured}`); |
| 151 | + summary.bindings.forEach(binding => { |
| 152 | + console.log(` ${binding.name} -> ${binding.bucket}`); |
| 153 | + }); |
| 154 | + |
| 155 | + console.log('\nR2 Binding Validation:'); |
| 156 | + if (validation.valid) { |
| 157 | + console.log(' ✓ All R2 bindings are properly configured'); |
| 158 | + } else { |
| 159 | + console.log(' ✗ R2 binding configuration errors:'); |
| 160 | + validation.errors.forEach(error => { |
| 161 | + console.log(` - ${error}`); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + console.log('\nDetailed Binding Information:'); |
| 166 | + Object.keys(bindingInfo).forEach(bindingName => { |
| 167 | + const info = bindingInfo[bindingName]; |
| 168 | + console.log(` ${bindingName}:`); |
| 169 | + console.log(` Bucket: ${info.bucketName}`); |
| 170 | + console.log(` Environment Variables:`); |
| 171 | + Object.keys(info.envVars).forEach(envVar => { |
| 172 | + console.log(` ${envVar}=${info.envVars[envVar]}`); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + const results = { |
| 177 | + summary, |
| 178 | + validation, |
| 179 | + bindingInfo |
| 180 | + }; |
| 181 | + |
| 182 | + console.log('======================\n'); |
| 183 | + |
| 184 | + res.json({ |
| 185 | + message: 'R2 Binding Test Results', |
| 186 | + results, |
| 187 | + timestamp: new Date().toISOString() |
| 188 | + }); |
| 189 | +}); |
| 190 | + |
| 191 | +app.get('/health', (req, res) => { |
| 192 | + res.json({ status: 'healthy', timestamp: new Date().toISOString() }); |
| 193 | +}); |
| 194 | + |
| 195 | +app.listen(PORT, () => { |
| 196 | + console.log(`\n🚀 R2 Storage Container listening on port ${PORT}`); |
| 197 | + console.log('Available endpoints:'); |
| 198 | + console.log(' /r2-env-test - Show R2 environment variables'); |
| 199 | + console.log(' /file-test - Test R2 mounted directory operations'); |
| 200 | + console.log(' /health - Health check'); |
| 201 | +}); |
0 commit comments