manu-sapiens commited on
Commit
8cac065
·
1 Parent(s): 7bf48e7

back to basics

Browse files
Dockerfile CHANGED
@@ -1,49 +1,32 @@
1
- FROM nikolaik/python-nodejs:python3.10-nodejs20
2
-
3
- USER root
4
- RUN apt-get -y update && apt-get -y install nginx
5
- RUN mkdir -p /var/cache/nginx \
6
- /var/log/nginx \
7
- /var/lib/nginx
8
- RUN touch /var/run/nginx.pid
9
- RUN chown -R root:root /var/cache/nginx \
10
- /var/log/nginx \
11
- /var/lib/nginx \
12
- /var/run/nginx.pid
13
-
14
-
15
- ENV HOME=/home/root \
16
- PATH=/home/root/.local/bin:$PATH
17
  WORKDIR $HOME/app
18
 
19
  RUN mkdir -p $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/
20
  RUN chmod 777 $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/
21
  RUN curl -L https://github.com/omnitool-ai/omnitool/raw/main/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1 -o $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
22
  RUN chmod 777 $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
23
- #RUN mv _tempfile.bin $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1 && chown -R root $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
24
  RUN mkdir $HOME/app/node_modules
25
  RUN chmod 777 $HOME/app/node_modules
26
 
27
- # Install dependencies and build app as non-root
28
- #USER root
29
-
30
- #COPY --chown=root _tempfile.bin $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
31
-
32
  # FROM node:20.6.1
33
 
34
- COPY --chown=root . $HOME/app
35
 
36
  RUN yarn install
37
 
38
  EXPOSE 4444
39
  EXPOSE 1688
40
- EXPOSE 1689
41
-
42
- # Copy nginx configuration
43
- COPY --chown=root nginx.conf /etc/nginx/sites-available/default
44
 
45
  CMD ["bash", "run.sh"]
46
-
47
-
48
  # ## docker build -t manusapiens/omnitool-test2b .
49
  # ## docker run --name n13 -p 1688:1688 -d manusapiens/omnitool-test2b
 
1
+ FROM node:20.6.1
2
+
3
+ USER node
4
+
5
+ ENV HOME=/home/node
6
+ # PATH=/home/node/.local/bin:$PATH
7
+
 
 
 
 
 
 
 
 
 
8
  WORKDIR $HOME/app
9
 
10
  RUN mkdir -p $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/
11
  RUN chmod 777 $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/
12
  RUN curl -L https://github.com/omnitool-ai/omnitool/raw/main/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1 -o $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
13
  RUN chmod 777 $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
14
+ #RUN mv _tempfile.bin $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1 && chown -R node $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
15
  RUN mkdir $HOME/app/node_modules
16
  RUN chmod 777 $HOME/app/node_modules
17
 
18
+ # Install dependencies and build app as non-node
19
+ #USER node
20
+ #COPY --chown=node _tempfile.bin $HOME/app/packages/omni-server/config.default/models/nsfwjs/mobilenet-v2-quant/group1-shard1of1
 
 
21
  # FROM node:20.6.1
22
 
23
+ COPY --chown=node . $HOME/app
24
 
25
  RUN yarn install
26
 
27
  EXPOSE 4444
28
  EXPOSE 1688
 
 
 
 
29
 
30
  CMD ["bash", "run.sh"]
 
 
31
  # ## docker build -t manusapiens/omnitool-test2b .
32
  # ## docker run --name n13 -p 1688:1688 -d manusapiens/omnitool-test2b
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: O T 3 - nginx edition 4444
3
  emoji: 🐳
4
  colorFrom: yellow
5
  colorTo: gray
@@ -182,14 +182,14 @@ You can then access the Omnitool software from a web browser on your local machi
182
 
183
  ```
184
  ◐ Booting Server
185
- ✔ Server has started and is ready to accept connections on http://0.0.0.0.1688.
186
  ✔ Ctrl-C to quit.
187
  ```
188
 
189
  5. **Open Omnitool in a Web Browser**
190
 
191
  Omnitool.ai can now be accessed from:
192
- [0.0.0.0.1688](http://0.0.0.0.1688)
193
 
194
  ---
195
  6. **Explore the Sample Recipes**
@@ -266,7 +266,7 @@ Once you have your JWT, you can execute a recipe by making a POST request to the
266
  **Endpoint**
267
 
268
  ```
269
- POST http://0.0.0.0.1688/api/v1/workflow/exec
270
  ```
271
 
272
  **Header**
@@ -282,7 +282,7 @@ Authorization: Bearer <token>
282
  To make the request using curl, you would use the following command, replacing <token> with your actual JWT:
283
 
284
  ```
285
- curl -X POST http://0.0.0.0.1688/api/v1/workflow/exec -H "Authorization: Bearer <token>"
286
  ```
287
 
288
  **Response**
 
1
  ---
2
+ title: O T 4 - back to basics
3
  emoji: 🐳
4
  colorFrom: yellow
5
  colorTo: gray
 
182
 
183
  ```
184
  ◐ Booting Server
185
+ ✔ Server has started and is ready to accept connections on http://127.0.0.1:1688.
186
  ✔ Ctrl-C to quit.
187
  ```
188
 
189
  5. **Open Omnitool in a Web Browser**
190
 
191
  Omnitool.ai can now be accessed from:
192
+ [127.0.0.1:1688](http://127.0.0.1:1688)
193
 
194
  ---
195
  6. **Explore the Sample Recipes**
 
266
  **Endpoint**
267
 
268
  ```
269
+ POST http://127.0.0.1:1688/api/v1/workflow/exec
270
  ```
271
 
272
  **Header**
 
282
  To make the request using curl, you would use the following command, replacing <token> with your actual JWT:
283
 
284
  ```
285
+ curl -X POST http://127.0.0.1:1688/api/v1/workflow/exec -H "Authorization: Bearer <token>"
286
  ```
287
 
288
  **Response**
myNodeServer.js CHANGED
@@ -1,33 +1,128 @@
1
  const fastify = require('fastify')({ logger: true });
2
- let MANAGEMENT_SERVER_PORT = 1689;
3
- let SERVER_HOST = '0.0.0.0';
4
- console.log(`************ Management Server v 0.002 ************`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  fastify.get('/', async (request, reply) => {
7
  const localUrl = request.headers['host'];
8
 
9
-
10
  let htmlContent = `
11
- <html>
12
- <head><title>Proxy Server</title></head>
13
- <body>
14
- <button onclick="redirect()">Goto: ${localUrl}</button>
15
- <script>
16
- function redirect() {
17
- window.location.replace('http://${localUrl}/omnitool');
18
- }
19
- </script>
20
- <div id="logs" style="white-space: pre-wrap;"></div>
21
- </body>
22
- </html>
23
- `;
24
- reply.type('text/html').send(htmlContent);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  });
26
 
27
  const start = async () => {
28
  try {
29
- await fastify.listen({ port: MANAGEMENT_SERVER_PORT, host: SERVER_HOST });
30
- console.log(`Server is listening on port ${MANAGEMENT_SERVER_PORT}`);
31
  } catch (err) {
32
  console.error(`Error starting server: ${err}`);
33
  process.exit(1);
 
1
  const fastify = require('fastify')({ logger: true });
2
+ const { spawn } = require('child_process');
3
+ const { createProxyMiddleware } = require('http-proxy-middleware');
4
+
5
+ let OMNITOOL_READY = false;
6
+ let ALREADY_STARTING = false;
7
+
8
+ const PROXY_PORT = 4444;
9
+ const TARGET_HOST = '0.0.0.0';
10
+ const PROXY_TARGET = 'http://0.0.0.0:1688';
11
+ const VERSION = '0.0.2c';
12
+
13
+ console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
14
+ let omnitoolLogs = [];
15
+
16
+ async function startOmnitoolServer() {
17
+ if (ALREADY_STARTING) return;
18
+ ALREADY_STARTING = true;
19
+
20
+ console.log('Starting Omnitool Server...');
21
+ return new Promise((resolve, reject) => {
22
+ const omnitoolStartProcess = spawn('./omnitool_start.sh');
23
+
24
+ omnitoolStartProcess.stdout.on('data', (data) => {
25
+ omnitoolLogs.push(data.toString());
26
+ console.log(`omnitool stdout: ${data}`);
27
+ if (data.toString().includes(`Server has started and is ready to accept connections`)) {
28
+ OMNITOOL_READY = true;
29
+ console.log('Omnitool server started successfully');
30
+ resolve();
31
+ }
32
+ });
33
+
34
+ omnitoolStartProcess.stderr.on('data', (data) => {
35
+ console.error(`omnitool stderr: ${data}`);
36
+ });
37
+
38
+ omnitoolStartProcess.on('close', (code) => {
39
+ console.log(`Omnitool server process exited with code ${code}`);
40
+ if (!OMNITOOL_READY) {
41
+ ALREADY_STARTING = false;
42
+ reject(`Omnitool server did not start within the timeout period.`);
43
+ }
44
+ });
45
+ });
46
+ }
47
 
48
  fastify.get('/', async (request, reply) => {
49
  const localUrl = request.headers['host'];
50
 
51
+ if (!OMNITOOL_READY) {
52
  let htmlContent = `
53
+ <html>
54
+ <head><title>Proxy Server</title></head>
55
+ <body>
56
+ <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
57
+ <button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')" disabled>Exit Iframe</button>
58
+ <div id="logs" style="white-space: pre-wrap;"></div>
59
+ <script>
60
+ function startServer() {
61
+ document.getElementById('startServerButton').disabled = true;
62
+ fetch('/start-omnitool-server')
63
+ .then(response => response.json())
64
+ .then(data => {
65
+ startLogPolling();
66
+ });
67
+ }
68
+
69
+ function startLogPolling() {
70
+ const interval = setInterval(() => {
71
+ fetch('/omnitool-logs')
72
+ .then(response => response.json())
73
+ .then(data => {
74
+ document.getElementById('logs').innerText = data.logs.join("\\n");
75
+ if (data.ready) {
76
+ clearInterval(interval);
77
+ window.location.reload(); // Refresh the page when Omnitool is ready
78
+ }
79
+ });
80
+ }, 1000);
81
+ }
82
+
83
+ startLogPolling();
84
+ </script>
85
+ </body>
86
+ </html>
87
+ `;
88
+ reply.type('text/html').send(htmlContent);
89
+ } else {
90
+ console.log('Proxying request as OMNITOOL is ready');
91
+ const proxy = createProxyMiddleware({
92
+ target: PROXY_TARGET,
93
+ changeOrigin: true,
94
+ ws: true,
95
+ logLevel: 'debug'
96
+ });
97
+ proxy(request.raw, reply.raw, () => {});
98
+ }
99
+ });
100
+
101
+ fastify.get('/omnitool-logs', async (request, reply) => {
102
+ reply.send({ logs: omnitoolLogs, ready: OMNITOOL_READY });
103
+ });
104
+
105
+ fastify.get('/start-omnitool-server', async (request, reply) => {
106
+ if (!OMNITOOL_READY) {
107
+ if (ALREADY_STARTING) {
108
+ return { message: "Omnitool server already starting" };
109
+ }
110
+ try {
111
+ await startOmnitoolServer();
112
+ reply.send({ message: "Omnitool server started successfully" });
113
+ } catch (error) {
114
+ console.error(error);
115
+ reply.send({ message: `Error starting Omnitool server: ${error}` });
116
+ }
117
+ } else {
118
+ reply.send({ message: "Omnitool server already running" });
119
+ }
120
  });
121
 
122
  const start = async () => {
123
  try {
124
+ await fastify.listen({ port: PROXY_PORT, host: TARGET_HOST });
125
+ console.log(`Server is listening on http://${TARGET_HOST}:${PROXY_PORT}`);
126
  } catch (err) {
127
  console.error(`Error starting server: ${err}`);
128
  process.exit(1);
nginx.conf DELETED
@@ -1,36 +0,0 @@
1
- server {
2
- listen 4444 default_server;
3
- listen [::]:4444 default_server;
4
-
5
- server_name _;
6
-
7
- location / {
8
- # Serve myNodeServer 1689
9
- proxy_pass http://localhost:1689;
10
- proxy_http_version 1.1;
11
- proxy_set_header Upgrade $http_upgrade;
12
- proxy_set_header Connection 'upgrade';
13
- proxy_set_header Host $host;
14
- proxy_set_header X-Real-IP $remote_addr;
15
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
16
- proxy_cache_bypass $http_upgrade;
17
- proxy_read_timeout 86400;
18
- proxy_redirect off;
19
- }
20
-
21
- location /omnitool/ {
22
- # Serve omnitool on 1688
23
- rewrite ^/omnitool/(.*) /$1 break;
24
- proxy_pass http://localhost:1688;
25
- proxy_http_version 1.1;
26
- proxy_set_header Upgrade $http_upgrade;
27
- proxy_set_header Connection 'upgrade';
28
- proxy_set_header Host $host;
29
- proxy_set_header X-Real-IP $remote_addr;
30
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
31
- proxy_cache_bypass $http_upgrade;
32
- proxy_read_timeout 86400;
33
- proxy_redirect off;
34
- }
35
-
36
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
packages/omni-server/src/run.ts CHANGED
@@ -250,7 +250,7 @@ const boot = async (options: OptionValues) => {
250
  const chatServiceConfig: IServiceConfig = Object.assign({ id: 'chat' });
251
  server.use(ChatService, chatServiceConfig);
252
 
253
- const listenOn = new URL('http://0.0.0.0:1688');
254
  listenOn.hostname = options.listen;
255
  listenOn.protocol = options.secure ? 'https' : 'http';
256
  listenOn.port = options.port;
 
250
  const chatServiceConfig: IServiceConfig = Object.assign({ id: 'chat' });
251
  server.use(ChatService, chatServiceConfig);
252
 
253
+ const listenOn = new URL('http://127.0.0.1:1688');
254
  listenOn.hostname = options.listen;
255
  listenOn.protocol = options.secure ? 'https' : 'http';
256
  listenOn.port = options.port;
packages/omni-server/src/services/FastifyServerService.ts CHANGED
@@ -75,7 +75,7 @@ class FastifyServerService extends Service {
75
 
76
  constructor(id: string, manager: ServiceManager, config: FastifyServerServiceConfig) {
77
  config.opts ??= {};
78
- config.listen ??= { host: '0.0.0.0', port: 3000 };
79
  config.cors ??= { origin: true, credentials: false };
80
  config.session ??= {
81
  secret: 'secret that is more than 32 characters',
 
75
 
76
  constructor(id: string, manager: ServiceManager, config: FastifyServerServiceConfig) {
77
  config.opts ??= {};
78
+ config.listen ??= { host: '127.0.0.1', port: 3000 };
79
  config.cors ??= { origin: true, credentials: false };
80
  config.session ??= {
81
  secret: 'secret that is more than 32 characters',
run.sh CHANGED
@@ -1,27 +1,12 @@
1
  #!/bin/bash
2
 
3
- # Start Nginx
4
- echo "Starting Nginx..."
5
- service nginx start
6
-
7
  # Start the Node.js server in the background
8
  echo "Starting Node.js server..."
9
- node myNodeServer.js --port 1689 --host 0.0.0.0 & echo $! > node_server.pid
10
-
11
- # Start the main application with yarn in the background
12
- echo "Starting Yarn..."
13
- yarn
14
-
15
- echo "Starting Yarn service..."
16
- yarn start -u -rb -R blocks & echo $! > yarn_service.pid
17
-
18
- # The script waits here for the yarn start process to terminate
19
- echo "Waiting for Yarn to terminate..."
20
- wait $(cat yarn_service.pid)
21
 
22
- # Cleanup: terminate and clean up background services
23
- echo "Cleaning up..."
24
- pkill -F node_server.pid
25
 
26
  echo "killing .pid files..."
27
- rm node_server.pid yarn_service.pid
 
1
  #!/bin/bash
2
 
 
 
 
 
3
  # Start the Node.js server in the background
4
  echo "Starting Node.js server..."
5
+ node myNodeServer.js --port 4444 --host 0.0.0.0 & echo $! > node_server.pid
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # The script waits here for the myNodeServer process to terminate
8
+ echo "Waiting for myNodeServer to terminate..."
9
+ wait $(cat node_server.pid)
10
 
11
  echo "killing .pid files..."
12
+ rm node_server.pid