social_test / app.py
broadfield-dev's picture
Update app.py
23a50be verified
import subprocess
import os
import time
from huggingface_hub import HfApi
def start_mastodon():
# Ensure secrets are available
required_secrets = [
"SECRET_KEY_BASE",
"OTP_SECRET",
"VAPID_PRIVATE_KEY",
"VAPID_PUBLIC_KEY"
]
for secret in required_secrets:
if not os.getenv(secret):
raise ValueError(f"Missing required secret: {secret}")
# Set environment variables, use port 7860
os.environ["RAILS_ENV"] = "development"
os.environ["PORT"] = "7860" # Changed from 3000 to 7860
os.environ["BIND"] = "0.0.0.0"
os.environ["LOCAL_DOMAIN"] = os.getenv("LOCAL_DOMAIN", "localhost")
os.environ["RAILS_SERVE_STATIC_FILES"] = os.getenv("RAILS_SERVE_STATIC_FILES", "true")
# Change to Mastodon directory
os.chdir("/app/mastodon")
# Patch Mastodon to use custom storage
with open("config/initializers/custom_storage.rb", "w") as f:
f.write("""
# Custom storage initializer
require 'open3'
module CustomStorage
def self.call_python(action, *args)
cmd = ['python3', '/app/mastodon/mastodon_config.py', action, *args]
stdout, stderr, status = Open3.capture3(*cmd)
stdout.strip if status.success?
end
end
# Override User model (example)
class User
def self.find_by_username(username)
data = CustomStorage.call_python('get_user', username)
data ? new(JSON.parse(data)) : nil
end
def save
CustomStorage.call_python('save_user', username, to_json)
end
end
# Override Status model (example)
class Status
def save
CustomStorage.call_python('save_post', id.to_s, to_json)
end
def self.find(id)
data = CustomStorage.call_python('get_post', id.to_s)
data ? new(JSON.parse(data)) : nil
end
end
""")
# Start Puma server
process = subprocess.Popen(
["/root/.rbenv/shims/bundle", "exec", "puma", "-C", "config/puma.rb"],
env=os.environ,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Stream logs
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
print(output.decode().strip())
return process
if __name__ == "__main__":
print("Starting Mastodon with Hugging Face Secrets and Storage on port 7860...")
mastodon_process = start_mastodon()
mastodon_process.wait()