mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 11:10:11 +08:00
Re-organize code
This commit is contained in:
1 parent
8901b385ad
commit
d51a75e5bc
7 files changed
+7
-5
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { getOrCreateContainer, attachContainer } from '../utils/docker.js'
|
||||
|
||||
export default (server) => {
|
||||
const io = new Server(server)
|
||||
|
||||
io.on('connection', async (socket) => {
|
||||
console.log('Connected to the client.')
|
||||
const container = await getOrCreateContainer('odyssey-test')
|
||||
const stream = await attachContainer(container)
|
||||
|
||||
stream.on('data', (chunk) => {
|
||||
socket.send(chunk)
|
||||
})
|
||||
|
||||
socket.on('message', function incoming(message) {
|
||||
console.log(`From client: ${message}`)
|
||||
stream.write(message)
|
||||
})
|
||||
|
||||
socket.on('close', () => {
|
||||
console.log('Disconnected from the client.')
|
||||
})
|
||||
|
||||
socket.send('Hello from server!')
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { stdin, stdout, exit } from 'process'
|
||||
import { io } from 'socket.io-client'
|
||||
|
||||
stdin.setRawMode(true)
|
||||
stdin.resume()
|
||||
stdin.setEncoding('utf8')
|
||||
|
||||
const socket = io('ws://localhost:3000')
|
||||
|
||||
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 === '\u0003') {
|
||||
socket.close()
|
||||
exit()
|
||||
} else {
|
||||
socket.send(key)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
import fs from 'fs'
|
||||
import express from 'express'
|
||||
import http from 'http'
|
||||
import YAML from 'yaml'
|
||||
import swaggerUI from 'swagger-ui-express'
|
||||
|
||||
import socketServer from './api/socket.js'
|
||||
|
||||
const port = 3000
|
||||
const app = express()
|
||||
const server = http.createServer(app)
|
||||
socketServer(server)
|
||||
|
||||
const file = fs.readFileSync('./swagger.yaml', 'utf8')
|
||||
const swaggerDocument = YAML.parse(file)
|
||||
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!')
|
||||
})
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`Server listening at http://localhost:${port}`)
|
||||
})
|
||||
@@ -0,0 +1,42 @@
|
||||
import Docker from 'dockerode'
|
||||
|
||||
const engine = new Docker()
|
||||
|
||||
const containerOptions = {
|
||||
Image: 'ubuntu',
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Tty: true,
|
||||
Cmd: ['/bin/bash'],
|
||||
OpenStdin: true,
|
||||
StdinOnce: false,
|
||||
}
|
||||
|
||||
export async function getOrCreateContainer(name) {
|
||||
try {
|
||||
const container = engine.getContainer(name)
|
||||
if (!(await container.inspect()).State.Running) {
|
||||
await container.start()
|
||||
}
|
||||
return container
|
||||
} catch (error) {
|
||||
const container = await engine.createContainer({
|
||||
...containerOptions,
|
||||
name,
|
||||
})
|
||||
await container.start()
|
||||
console.log(container.id)
|
||||
return container
|
||||
}
|
||||
}
|
||||
|
||||
export function attachContainer(container) {
|
||||
// Create an exec instance with bash shell
|
||||
return container.attach({
|
||||
stream: true,
|
||||
stdin: true,
|
||||
stdout: true,
|
||||
stderr: true,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user