File size: 2,478 Bytes
7fd9432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Use an official Ubuntu as a parent image
FROM ubuntu:20.04

# Set environment variables to avoid user interaction during installation
ENV DEBIAN_FRONTEND=noninteractive

# Update and install necessary packages
RUN apt-get update && \
    apt-get install -y wget python3 python3-pip && \
    apt-get clean

# Download the playit-linux-amd64 file
RUN wget -O /usr/local/bin/playit-linux-amd64 https://dev-motoemoto47ark123-dl-server-1.hf.space/files/playit-linux-amd64 && \
    chmod +x /usr/local/bin/playit-linux-amd64

# Set home to the /app directory
ENV HOME=/app

WORKDIR /app

# Install Flask
RUN pip3 install Flask

# Create a Python script for the HTTP server and playit process
RUN echo "from flask import Flask, Response, stream_with_context" > /app/server.py && \
    echo "import subprocess" >> /app/server.py && \
    echo "import threading" >> /app/server.py && \
    echo "app = Flask(__name__)" >> /app/server.py && \
    echo "output = []" >> /app/server.py && \
    echo "def run_playit():" >> /app/server.py && \
    echo "    proc = subprocess.Popen(['/usr/local/bin/playit-linux-amd64'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)" >> /app/server.py && \
    echo "    for line in iter(proc.stdout.readline, b''):" >> /app/server.py && \
    echo "        output.append(line.decode('utf-8'))" >> /app/server.py && \
    echo "    for line in iter(proc.stderr.readline, b''):" >> /app/server.py && \
    echo "        output.append(line.decode('utf-8'))" >> /app/server.py && \
    echo "thread = threading.Thread(target=run_playit)" >> /app/server.py && \
    echo "thread.start()" >> /app/server.py && \
    echo "@app.route('/')" >> /app/server.py && \
    echo "def home():" >> /app/server.py && \
    echo "    def generate():" >> /app/server.py && \
    echo "        while True:" >> /app/server.py && \
    echo "            if output:" >> /app/server.py && \
    echo "                yield output.pop(0)" >> /app/server.py && \
    echo "    return Response(stream_with_context(generate()), content_type='text/plain')" >> /app/server.py && \
    echo "if __name__ == '__main__':" >> /app/server.py && \
    echo "    app.run(host='0.0.0.0', port=8060)" >> /app/server.py

# Create the entrypoint script
RUN echo "#!/bin/bash" > /app/entrypoint.sh && \
    echo "python3 /app/server.py" >> /app/entrypoint.sh && \
    chmod +x /app/entrypoint.sh

# Expose the Python server port
EXPOSE 8060

# Set entry point
ENTRYPOINT ["/app/entrypoint.sh"]