File size: 1,198 Bytes
d14121a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify, send_from_directory
import os
import re

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__, static_folder=BASE_DIR, static_url_path="")
app.secret_key = os.urandom(24)


@app.route("/")
def home():
    return send_from_directory(BASE_DIR, "index.html")


@app.route("/main.js")
def js_file():
    return send_from_directory(BASE_DIR, "main.js")


@app.route("/style.css")
def css_file():
    return send_from_directory(BASE_DIR, "style.css")


@app.route("/contact", methods=["POST"])
def contact():
    data = request.get_json(silent=True) or request.form

    name = str(data.get("name", "")).strip()
    email = str(data.get("email", "")).strip()
    message = str(data.get("message", "")).strip()

    if not all([name, email, message]):
        return jsonify({"ok": False, "error": "All fields required."}), 400

    if not re.match(r"^[^@]+@[^@]+\.[^@]+$", email):
        return jsonify({"ok": False, "error": "Invalid email."}), 400

    print(f"\nNew message from {name} <{email}>:\n{message}\n{'-' * 50}")
    return jsonify({"ok": True})


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=True)