refactor: move client to sperate packages, rename cli to container

This commit is contained in:
wancat committed 2023-08-21 20:40:05 +08:00
1 parent 41a12c9aa8
commit 6ace18475b
15 files changed
+51 -28

No files matched your search

-2
View File
@@ -14,10 +14,8 @@
"dockerode": "^3.3.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.1",
"marked": "^5.1.1",
"mongoose": "^7.4.0",
"socket.io": "^4.7.1",
"socket.io-client": "^4.7.1",
"swagger-ui-express": "^5.0.0",
"yaml": "^2.3.1"
},
+1 -1
View File
@@ -14,6 +14,6 @@ WORKDIR /home/rudeus
COPY ./server/quests/helloworld/home /home/rudeus/
COPY ./packages/cli /usr/local/lib/cli
COPY ./packages/container /usr/local/lib/container
CMD [ "/bin/zsh" ]
+1 -1
View File
@@ -29,7 +29,7 @@ precmd() {
export CMD_ERROR_FILE=/tmp/cmd.error
# Now run your Node.js script with the result as an argument
node /usr/local/lib/cli/cli.js
node /usr/local/lib/container/cli.js
rm /tmp/cmd.out /tmp/cmd.error 2>/dev/null # cleanup
}
@@ -6,7 +6,8 @@ import commandHandler from '../../game/commandHandler.js'
export async function newCommand(req, res) {
console.log('new command:', req.body)
const { token, command, pwd, output, error } = req.body
const { token, command, pwd, output, error, ...additionalData } = req.body
console.log('additionalData:', additionalData)
if (!command) {
res.status(400).json({ message: 'command is required' })
+4 -2
View File
@@ -50,10 +50,12 @@ export default (server) => {
const stream = await attachContainer(container, { token })
stream.on('data', (chunk) => {
socket.send(chunk.toString())
socket.emit('terminal', chunk.toString())
})
socket.on('message', function incoming(message) {
socket.on('message', console.log)
socket.on('terminal', function incoming(message) {
stream.write(message)
})
-115
View File
@@ -1,115 +0,0 @@
/* eslint-disable no-underscore-dangle */
import { stdin, stdout, exit } from 'process'
import { io } from 'socket.io-client'
import { program } from 'commander'
import { get } from './utils/env.js'
stdin.setRawMode(true)
stdin.resume()
stdin.setEncoding('utf8')
async function createdSession() {
console.log('Creating a new session...')
const res = await fetch(`${program.opts().host}/api/v1/sessions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
quest_id: 'helloworld',
}),
})
const data = await res.json()
console.log('Created Session ID:', data._id)
return data
}
async function getSessionList() {
const res = await fetch(`${program.opts().host}/api/v1/sessions`)
const data = await res.json()
return data
}
async function lastSession() {
const sessions = await getSessionList()
if (sessions.length === 0) {
console.log(
'No session found. Please use --create to create a new session.'
)
exit(1)
}
const session = sessions[sessions.length - 1]
console.log(
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
)
return session
}
async function connect(sessionId) {
console.log(`Session ID: ${sessionId}`)
console.log('Use Ctrl + D to exit.')
if (!sessionId) exit()
const socket = io(program.opts().host, {
query: {
session_id: sessionId,
},
})
socket.on('open', function open() {
console.log('Connected to the server.')
})
socket.on('message', function incoming(data) {
stdout.write(data)
})
socket.on('close', function close() {
console.log('Disconnected from the server.')
exit()
})
stdin.on('data', (key) => {
if (key === '\u0004') {
socket.close()
exit()
} else {
socket.send(key)
}
})
}
async function main() {
console.log(program.opts().session)
// main()
const sessionId =
program.opts().session ||
(program.opts().create ? (await createdSession())._id : null) ||
(await lastSession())._id
await connect(sessionId)
}
program
.option('-s, --session <string>', 'Session ID')
.option('-c, --create', 'Create a new session')
.option(
'-h, --host <string>',
'Server host',
get('API_ENDPOINT', 'http://localhost:3000')
)
.action(main)
program
.command('list')
.description('List all sessions')
.action(async () => {
const sessions = await getSessionList()
sessions.forEach(({ _id, quest, createdAt }) => {
console.log(`${_id} ${quest} ${createdAt}`)
})
exit()
})
program.parse()
+1 -1
View File
@@ -18,7 +18,7 @@ const containerOptions = {
? []
: [
`${projectPath}/server/quests/helloworld/home:/home/rudeus`,
`${projectPath}/packages/cli:/usr/local/lib/cli`,
`${projectPath}/packages/container:/usr/local/lib/container`,
],
},
}