Spaces:
Running
Running
import os | |
import shutil | |
from flask import Flask, send_from_directory, abort, render_template_string | |
import subprocess | |
# リポジトリをクローンするディレクトリ | |
temp_dir = "/tmp/nebula_repo" | |
# リポジトリのクローンとセットアップを行う | |
def clone_and_setup_repo(): | |
# 一時ディレクトリが存在する場合は削除 | |
if os.path.exists(temp_dir): | |
shutil.rmtree(temp_dir) | |
print("Cloning the repository...") | |
result = os.system(f"git clone https://github.com/izum00/nebula --recursive {temp_dir}") | |
if result != 0: | |
print("Error: Failed to clone the repository.") | |
return | |
# クローンしたディレクトリに移動してセットアップ | |
os.chdir(temp_dir) | |
os.system("npm i") | |
os.system("cp config.example.toml config.toml") | |
os.system("npm run build") | |
os.system("npm start") | |
# index.htmlをカレントディレクトリに移動 | |
index_html_path = os.path.join(temp_dir, 'index.html') | |
if os.path.exists(index_html_path): | |
if os.path.exists('index.html'): | |
os.remove('index.html') | |
shutil.move(index_html_path, '.') | |
# 静的ファイルをstaticディレクトリに移動 | |
if not os.path.exists('static'): | |
os.mkdir('static') | |
for item in os.listdir(temp_dir): | |
if item != 'index.html': | |
shutil.move(os.path.join(temp_dir, item), os.path.join('static', item)) | |
# クローンとセットアップを実行 | |
clone_and_setup_repo() | |
# Flaskアプリケーションの設定 | |
app = Flask(__name__) | |
# ルートでindex.htmlを表示 | |
def index(): | |
# index.htmlが存在しない場合は404エラー | |
if not os.path.exists("index.html"): | |
return abort(404, description="index.html not found.") | |
# index.htmlの内容を読み込む | |
with open("index.html", "r") as file: | |
index_html_content = file.read() | |
return render_template_string(index_html_content) | |
# 静的ファイルを提供するためのルート | |
def static_files(filename): | |
return send_from_directory('static', filename) | |
# main.jsの存在を確認するエンドポイント | |
def check_main_js(): | |
if os.path.exists('static/main.js'): | |
return "main.js exists." | |
else: | |
return "main.js does not exist." | |
if __name__ == '__main__': | |
# port 7860でFlaskアプリを起動 | |
app.run(host='0.0.0.0', port=7860) |