File size: 2,810 Bytes
76ee95f
 
253cabf
 
 
 
 
 
 
76ee95f
 
 
253cabf
76ee95f
253cabf
 
76ee95f
253cabf
 
 
 
 
 
 
 
 
76ee95f
253cabf
 
 
 
 
 
 
 
76ee95f
 
 
 
 
253cabf
 
 
76ee95f
253cabf
 
76ee95f
253cabf
 
 
76ee95f
253cabf
 
 
76ee95f
253cabf
 
 
 
 
 
76ee95f
253cabf
 
76ee95f
253cabf
 
76ee95f
253cabf
 
76ee95f
253cabf
 
 
 
76ee95f
253cabf
 
76ee95f
253cabf
 
 
76ee95f
bba1238
 
 
 
 
 
 
 
76ee95f
 
253cabf
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
import os
import json
import logging
from datetime import datetime
from flask import Flask, render_template, jsonify, request, send_from_directory

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max upload
app.config['JSON_AS_ASCII'] = False

# Data storage
DATA_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'valuations.json')

def load_data():
    if os.path.exists(DATA_FILE):
        try:
            with open(DATA_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
        except Exception as e:
            logger.error(f"Error loading data: {e}")
            return []
    return []

def save_data(data):
    try:
        with open(DATA_FILE, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        return True
    except Exception as e:
        logger.error(f"Error saving data: {e}")
        return False

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

@app.route('/api/valuations', methods=['GET'])
def get_valuations():
    return jsonify(load_data())

@app.route('/api/valuations', methods=['POST'])
def save_valuation():
    try:
        new_item = request.json
        if not new_item:
            return jsonify({"error": "No data provided"}), 400
        
        # Add metadata
        new_item['id'] = str(int(datetime.now().timestamp() * 1000))
        new_item['created_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        
        data = load_data()
        data.insert(0, new_item)  # Newest first
        
        # Limit history to 50 items
        if len(data) > 50:
            data = data[:50]
            
        save_data(data)
        return jsonify({"success": True, "data": new_item})
    except Exception as e:
        logger.error(f"Error saving valuation: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/valuations/<id>', methods=['DELETE'])
def delete_valuation(id):
    try:
        data = load_data()
        data = [item for item in data if item.get('id') != id]
        save_data(data)
        return jsonify({"success": True})
    except Exception as e:
        logger.error(f"Error deleting valuation: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/health')
def health():
    return jsonify({"status": "healthy"})

@app.errorhandler(413)
def request_entity_too_large(error):
    return jsonify({"error": "Request entity too large (max 16MB)"}), 413

@app.errorhandler(500)
def internal_server_error(error):
    return jsonify({"error": "Internal Server Error"}), 500

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 7860))
    app.run(host='0.0.0.0', port=port)