shs3131 commited on
Commit
78c1cb8
·
1 Parent(s): 72c0f98
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -2,17 +2,18 @@
2
  import subprocess
3
  import spaces
4
  import os
 
 
 
5
 
6
- node_started = False
7
-
8
- def start_node():
9
- global node_started
10
- if node_started:
11
- return "Already running"
12
- subprocess.run("pkill -f 'node server' 2>/dev/null || true", shell=True)
13
- subprocess.Popen("node server.js", shell=True, cwd='/home/user/app')
14
- node_started = True
15
- return "Node.js started"
16
 
17
  @spaces.GPU
18
  def _dummy_gpu_placeholder():
@@ -23,10 +24,9 @@ with gr.Blocks(title="OCHF Node Test") as demo:
23
  btn_start = gr.Button("Start Node.js Server")
24
  btn_check = gr.Button("Check")
25
  output = gr.Textbox(label="Output", lines=20)
26
- btn_start.click(fn=lambda: (subprocess.run("pkill -f 'node server' 2>/dev/null || true", shell=True), subprocess.Popen("node server.js", shell=True, cwd='/home/user/app')) and 'Started', outputs=output)
27
- btn_check.click(fn=lambda: subprocess.run("curl -s http://localhost:8888 2>&1 || echo 'not running'", shell=True, capture_output=True, text=True).stdout, outputs=output)
28
 
29
- # Mount proxy using starlette
30
  from starlette.routing import Route
31
  from starlette.responses import Response
32
  import httpx
@@ -40,9 +40,8 @@ async def proxy(request):
40
  except:
41
  return Response(content='Node.js not running', status_code=502)
42
 
43
- # Add route to the underlying ASGI app
44
- demo.app.router.routes.append(Route('/node/', endpoint=proxy_node))
45
- demo.app.router.routes.append(Route('/node/{path:path}', endpoint=proxy_node))
46
 
47
  start_node()
48
  demo.launch(server_name="0.0.0.0")
 
2
  import subprocess
3
  import spaces
4
  import os
5
+ from starlette.routing import Route
6
+ from starlette.responses import Response
7
+ import httpx
8
 
9
+ async def proxy_node(request):
10
+ path = request.path_params.get('path', '')
11
+ try:
12
+ async with httpx.AsyncClient() as client:
13
+ r = await client.get(f'http://localhost:8888/{path}')
14
+ return Response(content=r.content, status_code=r.status_code, headers=dict(r.headers))
15
+ except:
16
+ return Response(content='Node.js not running', status_code=502)
 
 
17
 
18
  @spaces.GPU
19
  def _dummy_gpu_placeholder():
 
24
  btn_start = gr.Button("Start Node.js Server")
25
  btn_check = gr.Button("Check")
26
  output = gr.Textbox(label="Output", lines=20)
27
+ btn_start.click(fn=lambda: (__import__('subprocess').run("pkill -f 'node server' 2>/dev/null || true", shell=True), __import__('subprocess').Popen("node server.js", shell=True, cwd='/home/user/app')) and 'Started', outputs=output)
28
+ btn_check.click(fn=lambda: __import__('subprocess').run("curl -s http://localhost:8888 2>&1 || echo 'not running'", shell=True, capture_output=True, text=True).stdout, outputs=output)
29
 
 
30
  from starlette.routing import Route
31
  from starlette.responses import Response
32
  import httpx
 
40
  except:
41
  return Response(content='Node.js not running', status_code=502)
42
 
43
+ demo.app.router.routes.append(Route('/node/', endpoint=proxy))
44
+ demo.app.router.routes.append(Route('/node/{path:path}', endpoint=proxy))
 
45
 
46
  start_node()
47
  demo.launch(server_name="0.0.0.0")