File size: 5,878 Bytes
83e6f13 c9da93d 83e6f13 c9da93d 83e6f13 c9da93d 83e6f13 c9da93d 83e6f13 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# app.py - Main application file
import os
import subprocess
import threading
import time
from flask import Flask, jsonify, redirect
import signal
import sys
app = Flask(__name__)
# Global variable to track code-server process
code_server_process = None
def check_code_server():
"""Check if code-server is installed"""
try:
result = subprocess.run(
["code-server", "--version"],
check=True,
capture_output=True,
text=True
)
print(f"Code-server version: {result.stdout.strip()}")
return True
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print(f"Code-server not found: {e}")
return False
def start_code_server():
"""Start code-server in background"""
global code_server_process
try:
# Create config directory
config_dir = os.path.expanduser("~/.config/code-server")
os.makedirs(config_dir, exist_ok=True)
# Set password (you can change this)
password = os.getenv("CODE_SERVER_PASSWORD", "huggingface123")
# Start code-server
cmd = [
"code-server",
"--bind-addr", "0.0.0.0:8080",
"--auth", "password",
"--password", password,
"--disable-telemetry",
"--disable-update-check"
]
print("Starting code-server...")
code_server_process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Wait a bit for it to start
time.sleep(5)
if code_server_process.poll() is None:
print("Code-server started successfully!")
print(f"Password: {password}")
return True
else:
print("Code-server failed to start")
return False
except Exception as e:
print(f"Error starting code-server: {e}")
return False
def setup_workspace():
"""Setup initial workspace"""
try:
# Create workspace directory
workspace_dir = "/tmp/workspace"
os.makedirs(workspace_dir, exist_ok=True)
# Create a sample file
sample_file = os.path.join(workspace_dir, "README.md")
with open(sample_file, "w") as f:
f.write("""# Code-Server on Hugging Face Spaces
Welcome to your code-server environment!
## Features:
- Full VS Code experience in browser
- Python environment ready
- Terminal access
- File editing capabilities
## Getting Started:
1. Open terminal (Ctrl+Shift+`)
2. Install packages: `pip install package-name`
3. Start coding!
## Note:
- Files are temporary and will be lost on restart
- For persistent storage, use external git repos
""")
# Create sample Python file
sample_py = os.path.join(workspace_dir, "sample.py")
with open(sample_py, "w") as f:
f.write("""#!/usr/bin/env python3
# Sample Python file
def hello_world():
print("Hello from Code-Server on Hugging Face!")
if __name__ == "__main__":
hello_world()
""")
print("Workspace setup complete!")
return True
except Exception as e:
print(f"Error setting up workspace: {e}")
return False
@app.route('/health')
def health_check():
"""Health check endpoint"""
global code_server_process
status = {
"status": "healthy",
"timestamp": time.time(),
"code_server_running": False
}
if code_server_process:
if code_server_process.poll() is None:
status["code_server_running"] = True
else:
status["code_server_running"] = False
status["status"] = "code_server_down"
return jsonify(status)
@app.route('/')
def index():
"""Redirect to code-server"""
return redirect('/code-server/', code=302)
@app.route('/code-server/')
def code_server_proxy():
"""Info about code-server access"""
return """
<html>
<head><title>Code-Server Access</title></head>
<body>
<h1>Code-Server on Hugging Face Spaces</h1>
<p>Code-server should be running on port 8080</p>
<p>If you're seeing this, the proxy might not be working correctly.</p>
<p>Try accessing directly through the Hugging Face Spaces interface.</p>
<p><a href="/health">Check Health Status</a></p>
</body>
</html>
"""
def signal_handler(sig, frame):
"""Handle shutdown gracefully"""
global code_server_process
print("Shutting down...")
if code_server_process:
code_server_process.terminate()
code_server_process.wait()
sys.exit(0)
def initialize_environment():
"""Initialize the complete environment"""
print("Initializing environment...")
# Check if code-server is installed
if not check_code_server():
print("Code-server is not installed!")
return False
# Setup workspace
if not setup_workspace():
print("Failed to setup workspace")
return False
# Start code-server in background thread
def start_server():
start_code_server()
server_thread = threading.Thread(target=start_server, daemon=True)
server_thread.start()
return True
if __name__ == "__main__":
# Register signal handlers
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Initialize environment
if initialize_environment():
print("Environment initialized successfully!")
# Start Flask app
port = int(os.getenv("PORT", 7860))
app.run(host="0.0.0.0", port=port, debug=False)
else:
print("Failed to initialize environment")
sys.exit(1) |