from flask import Flask, render_template, send_from_directory, jsonify
import os

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/tiles/<path:filename>')
def serve_tiles(filename):
    print(f'请求瓦片: {filename}')  # 添加调试信息
    try:
        return send_from_directory('tiles', filename)
    except Exception as e:
        print(f'瓦片加载错误: {str(e)}')  # 添加错误信息
        return str(e), 404

@app.route('/kml/<path:filename>')
def serve_kml(filename):
    return send_from_directory('kml', filename)

@app.route('/kml_files')
def list_kml_files():
    kml_files = [f for f in os.listdir('kml') if f.endswith('.kml')]
    return jsonify(kml_files)

if __name__ == '__main__':
    # 确保必要的目录存在
    os.makedirs('tiles', exist_ok=True)
    os.makedirs('kml', exist_ok=True)
    os.makedirs('templates', exist_ok=True)
    os.makedirs('static', exist_ok=True)
    
    app.run(debug=True, port=5000) 