1 const fs = require('fs');
3 const util = require('util');
4 const execFile = util.promisify(require('child_process').execFile);
6 const express = require('express');
8 const session = require('express-session');
9 const FileStore = require('session-file-store')(session);
10 const http = require('http');
11 const server = http.createServer(app);
12 const { Server } = require("socket.io");
13 const io = new Server(server);
16 const python_path = '/usr/bin/python3'
17 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
18 const state_file = 'zombocom-state.json'
22 if (!process.env.ZOMBOCOM_SESSION_SECRET) {
23 console.log("Error: Environment variable ZOMBOCOM_SESSION_SECRET not set.");
24 console.log("Please set it to a random, but persistent, value.")
28 const session_middleware = session(
29 {store: new FileStore,
30 secret: process.env.ZOMBOCOM_SESSION_SECRET,
32 saveUninitialized: true
35 app.use(session_middleware);
37 // convert a connect middleware to a Socket.IO middleware
38 const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
40 io.use(wrap(session_middleware));
42 // Load comments at server startup
43 fs.readFile(state_file, (err, data) => {
45 state = { images: [] };
47 state = JSON.parse(data);
50 // Save comments when server is shutting down
52 fs.writeFileSync('zombocom-state.json', JSON.stringify(state), (error) => {
58 // And connect to that on either clean exit...
59 process.on('exit', cleanup);
61 // ... or on a SIGINT (control-C)
62 process.on('SIGINT', () => {
67 app.get('/index.html', (req, res) => {
68 res.sendFile(__dirname + '/index.html');
71 io.on('connection', (socket) => {
73 // First things first, tell the client their name (if any)
74 if (socket.request.session.name) {
75 socket.emit('inform-name', socket.request.session.name);
78 // Replay old comments and images to a newly-joining client
80 state.images.forEach((image) => {
81 socket.emit('image', image)
84 socket.on('set-name', (name) => {
85 console.log("Received set-name event: " + name);
86 socket.request.session.name = name;
87 socket.request.session.save();
90 // When any client comments, send that to all clients (including sender)
91 socket.on('comment', (comment) => {
92 const images = state.images;
94 // Send comment to clients after adding commenter's name
95 comment.name = socket.request.session.name;
96 io.emit('comment', comment);
98 const index = images.findIndex(image => image.id == comment.image_id);
100 // Before adding the comment to server's state, drop the image_id
101 delete comment.image_id;
103 // Now add the comment to the image, remove the image from the
104 // images array and then add it back at the end, (so it appears
105 // as the most-recently-modified image for any new clients)
106 const image = images[index];
107 image.comments.push(comment);
108 images.splice(index, 1);
112 // Generate an image when requested
113 socket.on('generate', (request) => {
114 console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
115 async function generate_image(code, prompt) {
118 promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
120 promise = execFile(python_path, [generate_image_script, prompt])
122 const child = promise.child;
123 child.stdout.on('data', (data) => {
124 const images = JSON.parse(data);
125 images.forEach((image) => {
126 image.id = state.images.length;
128 io.emit('image', image);
129 state.images.push(image);
132 child.stderr.on('data', (data) => {
133 console.log("Error occurred during generate-image: " + data);
136 const { stdout, stderr } = await promise;
140 socket.emit('generation-done');
142 generate_image(request['code'], request['prompt']);
146 server.listen(port, () => {
147 console.log(`listening on *:${port}`);