]> git.cworth.org Git - zombocom-ai/blob - index.js
46b536b854f1d659aede3e621e6e8077c24eca8f
[zombocom-ai] / index.js
1 const fs = require('fs');
2
3 const util = require('util');
4 const execFile = util.promisify(require('child_process').execFile);
5
6 const express = require('express');
7 const app = 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);
14 const port = 2122;
15
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'
19
20 var state;
21
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.")
25     process.exit();
26 }
27
28 const session_middleware =  session(
29     {store: new FileStore,
30      secret: process.env.ZOMBOCOM_SESSION_SECRET,
31      resave: false,
32      saveUninitialized: true
33     });
34
35 app.use(session_middleware);
36
37 // convert a connect middleware to a Socket.IO middleware
38 const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
39
40 io.use(wrap(session_middleware));
41
42 // Load comments at server startup
43 fs.readFile(state_file, (err, data) => {
44     if (err)
45         state = { images: [] };
46     else
47         state = JSON.parse(data);
48 });
49
50 // Save comments when server is shutting down
51 function cleanup() {
52     fs.writeFileSync('zombocom-state.json', JSON.stringify(state), (error) => {
53         if (error)
54             throw error;
55     })
56 }
57
58 // And connect to that on either clean exit...
59 process.on('exit', cleanup);
60
61 // ... or on a SIGINT (control-C)
62 process.on('SIGINT', () => {
63     cleanup();
64     process.exit();
65 });
66
67 app.get('/index.html', (req, res) => {
68     res.sendFile(__dirname + '/index.html');
69 });
70
71 app.get('/tardis', (req, res) => {
72     res.sendFile(__dirname + '/tardis.html');
73 });
74
75 app.get('/tardis/', (req, res) => {
76     res.sendFile(__dirname + '/tardis.html');
77 });
78
79 app.get('/tardis/index.html', (req, res) => {
80     res.sendFile(__dirname + '/tardis.html');
81 });
82
83 io.on('connection', (socket) => {
84
85     // First things first, tell the client their name (if any)
86     if (socket.request.session.name) {
87         socket.emit('inform-name', socket.request.session.name);
88     }
89
90     // Replay old comments and images to a newly-joining client
91     socket.emit('reset');
92     state.images.forEach((image) => {
93         socket.emit('image', image)
94     });
95
96     socket.on('set-name', (name) => {
97         console.log("Received set-name event: " + name);
98         socket.request.session.name = name;
99         socket.request.session.save();
100         // Complete the round trip to the client
101         socket.emit('inform-name', socket.request.session.name);
102     });
103
104     // When any client comments, send that to all clients (including sender)
105     socket.on('comment', (comment) => {
106         const images = state.images;
107
108         // Send comment to clients after adding commenter's name
109         comment.name = socket.request.session.name;
110         io.emit('comment', comment);
111
112         const index = images.findIndex(image => image.id == comment.image_id);
113
114         // Before adding the comment to server's state, drop the image_id
115         delete comment.image_id;
116
117         // Now add the comment to the image, remove the image from the
118         // images array and then add it back at the end, (so it appears
119         // as the most-recently-modified image for any new clients)
120         const image = images[index];
121         image.comments.push(comment);
122         images.splice(index, 1);
123         images.push(image);
124     });
125
126     // Generate an image when requested
127     socket.on('generate', (request) => {
128         console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
129         async function generate_image(code, prompt) {
130             var promise;
131
132             // Inject the target seed for the "dice" prompt once every
133             // 6 requests for a random seed (and only if the word
134             // "dice" does not appear in the prompt).
135             if (!code && !prompt.toLowerCase().includes("dice")) {
136                 if (state.images.length % 6 == 0) {
137                     code = 319630254;
138                 }
139             }
140
141             if (code) {
142                 promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
143             } else {
144                 promise = execFile(python_path, [generate_image_script, prompt])
145             }
146             const child = promise.child;
147             child.stdout.on('data', (data) => {
148                 const images = JSON.parse(data);
149                 images.forEach((image) => {
150                     image.id = state.images.length;
151                     image.censored = false;
152                     image.link = "";
153                     image.comments = [];
154                     io.emit('image', image);
155                     state.images.push(image);
156                 });
157             });
158             child.stderr.on('data', (data) => {
159                 console.log("Error occurred during generate-image: " + data);
160             });
161             try {
162                 const { stdout, stderr } = await promise;
163             } catch(e) {
164                 console.error(e);
165             }
166             socket.emit('generation-done');
167         }
168         generate_image(request['code'], request['prompt']);
169     });
170 });
171
172 server.listen(port, () => {
173     console.log(`listening on *:${port}`);
174 });