Atlas - main.py

Home / toolkits / LWPS / src Lines: 1 | Size: 2718 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1# SPDX-License-Identifier: GPL-3.0 2# LWPS - Lightweight PHP Server 3# 4# Main indexer and server. 5# main.py 6# 7# COPYRIGHT NOTICE 8# Copyright (C) 2026 0x4248 and contributors 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the license is not changed. 11# 12# This software is free and open source. Licensed under the GNU general 13# public license version 3.0 as published by the Free Software Foundation. 14 15import os 16import fastapi 17import uvicorn 18from fastapi.responses import FileResponse 19import subprocess 20 21 22BASE_DIR = os.path.join(os.path.dirname(__file__), '..', 'server') 23app = fastapi.FastAPI() 24 25env = os.environ.copy() 26env['SERVER_SOFTWARE'] = 'LWPS' 27env['DOCUMENT_ROOT'] = BASE_DIR 28 29 30def convert_php(file_path: str) -> str: 31 """Convert a PHP file to HTML using the PHP CLI.""" 32 try: 33 result = subprocess.run( 34 ['php', file_path], 35 capture_output=True, 36 text=True, 37 check=True, 38 env=env 39 ) 40 return result.stdout 41 except subprocess.CalledProcessError as e: 42 error_page_path = os.path.join(os.path.dirname(__file__), 'static', '500.html') 43 with open(error_page_path, 'r') as f: 44 error_page = f.read() 45 return error_page.replace('{{error}}', str(e.stderr)) 46 except FileNotFoundError: 47 error_page_path = os.path.join(os.path.dirname(__file__), 'static', '500.html') 48 with open(error_page_path, 'r') as f: 49 error_page = f.read() 50 return error_page.replace('{{error}}', 'PHP CLI not found. Please install PHP.') 51 52@app.get("/{full_path:path}") 53async def serve_file(full_path: str): 54 file_path = os.path.join(BASE_DIR, full_path) 55 56 if os.path.isdir(file_path): 57 index_php = os.path.join(file_path, 'index.php') 58 index_html = os.path.join(file_path, 'index.html') 59 if os.path.isfile(index_php): 60 file_path = index_php 61 elif os.path.isfile(index_html): 62 file_path = index_html 63 else: 64 not_found_page_path = os.path.join(os.path.dirname(__file__), 'static', '404.html') 65 return FileResponse(not_found_page_path, status_code=404) 66 67 if file_path.endswith('.php') and os.path.isfile(file_path): 68 html_content = convert_php(file_path) 69 return fastapi.responses.HTMLResponse(content=html_content) 70 71 if os.path.isfile(file_path): 72 return FileResponse(file_path) 73 74 not_found_page_path = os.path.join(os.path.dirname(__file__), 'static', '404.html') 75 return FileResponse(not_found_page_path, status_code=404) 76 77if __name__ == "__main__": 78 uvicorn.run(app, host="0.0.0.0", port=8000)
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.