|
|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
import express from 'express'
|
|
|
|
|
import { WebSocketServer } from 'ws'
|
|
|
|
|
import { createContainerPty } from './docker.js'
|
|
|
|
|
import { runCommand } from './utils.js'
|
|
|
|
|
import { getContainerPath, runCommand } from './utils.js'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
|
|
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
|
|
@ -9,11 +12,12 @@ app.use(express.json())
|
|
|
|
|
|
|
|
|
|
app.get('/api/status', c => c.json(42))
|
|
|
|
|
|
|
|
|
|
app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => {
|
|
|
|
|
app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls/*', async (req, res) => {
|
|
|
|
|
const uuid = req.params.uuid
|
|
|
|
|
const p = path.normalize(req.params[0])
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const files = await runCommand('/bin/ls', ['exec', uuid, '/bin/ls'])
|
|
|
|
|
const files = await runCommand('/bin/ls', [path.join(getContainerPath('phc', uuid), p)])
|
|
|
|
|
|
|
|
|
|
res.json(files.trim().split('\n'))
|
|
|
|
|
} catch (e) {
|
|
|
|
|
@ -23,13 +27,12 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/exec/ls', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
|
|
|
|
|
const uuid = req.params.uuid
|
|
|
|
|
const path = req.params[0]
|
|
|
|
|
const p = path.normalize(req.params[0])
|
|
|
|
|
|
|
|
|
|
console.log('Getting', path)
|
|
|
|
|
console.log('Getting', p)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const content = await runCommand('docker', ['exec', uuid, '/bin/cat', `/project/${path}`])
|
|
|
|
|
|
|
|
|
|
const content = await fs.readFile(path.join(getContainerPath('phc', uuid), p), 'utf8')
|
|
|
|
|
res.json(content)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
res.json({ error: e.toString() })
|
|
|
|
|
@ -37,15 +40,13 @@ app.get('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.put('/api/container/:uuid([a-zA-Z0-9-]+)/fs/*', async (req, res) => {
|
|
|
|
|
const path = req.params[0]
|
|
|
|
|
const p = path.normalize(req.params[0])
|
|
|
|
|
|
|
|
|
|
const content = req.body
|
|
|
|
|
console.log(content)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await runCommand('docker', ['exec', uuid, '/bin/sh', '-c', `cat - > "/project/${path}"`], {
|
|
|
|
|
stdin: content,
|
|
|
|
|
})
|
|
|
|
|
await fs.writeFile(path.join(getContainerPath('phc', uuid), p), content)
|
|
|
|
|
|
|
|
|
|
res.json('ok')
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|