Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
from flask import Flask, send_from_directory, abort, render_template_string
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
# リポジトリをクローンするディレクトリ
|
7 |
+
temp_dir = "/tmp/nebula_repo"
|
8 |
+
|
9 |
+
# リポジトリのクローンとセットアップを行う
|
10 |
+
def clone_and_setup_repo():
|
11 |
+
# 一時ディレクトリが存在する場合は削除
|
12 |
+
if os.path.exists(temp_dir):
|
13 |
+
shutil.rmtree(temp_dir)
|
14 |
+
|
15 |
+
print("Cloning the repository...")
|
16 |
+
result = os.system(f"git clone https://github.com/binary-person/rammerhead.git --recursive {temp_dir}")
|
17 |
+
|
18 |
+
if result != 0:
|
19 |
+
print("Error: Failed to clone the repository.")
|
20 |
+
return
|
21 |
+
|
22 |
+
# クローンしたディレクトリに移動してセットアップ
|
23 |
+
os.chdir(temp_dir)
|
24 |
+
os.system("npm i")
|
25 |
+
os.system("cp config.example.toml config.toml")
|
26 |
+
os.system("npm run build")
|
27 |
+
os.system("node src/server.js")
|
28 |
+
|
29 |
+
# index.htmlをカレントディレクトリに移動
|
30 |
+
index_html_path = os.path.join(temp_dir, 'index.html')
|
31 |
+
if os.path.exists(index_html_path):
|
32 |
+
if os.path.exists('index.html'):
|
33 |
+
os.remove('index.html')
|
34 |
+
shutil.move(index_html_path, '.')
|
35 |
+
|
36 |
+
# 静的ファイルをstaticディレクトリに移動
|
37 |
+
if not os.path.exists('static'):
|
38 |
+
os.mkdir('static')
|
39 |
+
for item in os.listdir(temp_dir):
|
40 |
+
if item != 'index.html':
|
41 |
+
shutil.move(os.path.join(temp_dir, item), os.path.join('static', item))
|
42 |
+
|
43 |
+
# クローンとセットアップを実行
|
44 |
+
clone_and_setup_repo()
|
45 |
+
|
46 |
+
# Flaskアプリケーションの設定
|
47 |
+
app = Flask(__name__)
|
48 |
+
|
49 |
+
# ルートでindex.htmlを表示
|
50 |
+
@app.route('/')
|
51 |
+
def index():
|
52 |
+
# index.htmlが存在しない場合は404エラー
|
53 |
+
if not os.path.exists("index.html"):
|
54 |
+
return abort(404, description="index.html not found.")
|
55 |
+
|
56 |
+
# index.htmlの内容を読み込む
|
57 |
+
with open("index.html", "r") as file:
|
58 |
+
index_html_content = file.read()
|
59 |
+
|
60 |
+
return render_template_string(index_html_content)
|
61 |
+
|
62 |
+
# 静的ファイルを提供するためのルート
|
63 |
+
@app.route('/<path:filename>')
|
64 |
+
def static_files(filename):
|
65 |
+
return send_from_directory('static', filename)
|
66 |
+
|
67 |
+
# main.jsの存在を確認するエンドポイント
|
68 |
+
@app.route('/check_main_js')
|
69 |
+
def check_main_js():
|
70 |
+
if os.path.exists('static/main.js'):
|
71 |
+
return "main.js exists."
|
72 |
+
else:
|
73 |
+
return "main.js does not exist."
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
# port 7860でFlaskアプリを起動
|
77 |
+
app.run(host='0.0.0.0', port=7860)
|