This commit is contained in:
Alex Rodionov 2024-05-30 20:40:48 -07:00
parent 319902179c
commit 729d401ac3
12 changed files with 77 additions and 32 deletions

View file

@ -6,13 +6,14 @@ const fs = require('fs')
const server = http.createServer(async (req, res) => {
const { method, url } = req
const [, , cacheType, sha] = url.split('/')
const cacheKey = `setup-bazel-1-remote-cache-${sha}`
const filePath = `/tmp/cache-${cacheType}-${sha}`
if (method === 'GET') {
try {
const cacheId = await cache.restoreCache([filePath], sha)
const cacheId = await cache.restoreCache([filePath], cacheKey)
if (!cacheId) {
console.log(`Cache miss for ${sha}`)
console.log(`Cache miss for ${cacheKey}`)
res.writeHead(404)
return res.end('Cache miss')
}
@ -20,7 +21,7 @@ const server = http.createServer(async (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/octet-stream' })
res.end(data)
} catch (error) {
console.error(`Error retrieving cache for ${sha}: ${error}`)
console.error(`Error retrieving cache for ${cacheKey}: ${error}`)
res.writeHead(500)
res.end('Internal Server Error')
}
@ -30,12 +31,12 @@ const server = http.createServer(async (req, res) => {
req.on('end', async () => {
try {
fs.writeFileSync(filePath, Buffer.concat(data))
await cache.saveCache([filePath], sha)
console.log(`Cache saved for ${sha}`)
await cache.saveCache([filePath], cacheKey)
console.log(`Cache saved for ${cacheKey}`)
res.writeHead(201)
res.end('Cache saved')
} catch (error) {
console.error(`Error saving cache for ${sha}: ${error}`)
console.error(`Error saving cache for ${cacheKey}: ${error}`)
res.writeHead(500)
res.end('Internal Server Error')
}
@ -46,5 +47,5 @@ const server = http.createServer(async (req, res) => {
}
})
const PORT = process.env.PORT || 8080
const PORT = process.env.PORT || 9889
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`))