75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
import locale
|
|
import logging
|
|
|
|
from flask import Flask, request
|
|
from flask_cors import CORS, cross_origin
|
|
from flask_executor import Executor
|
|
from flask_wtf import FlaskForm, RecaptchaField
|
|
import telegram_send
|
|
from wtforms import StringField
|
|
from wtforms.validators import DataRequired
|
|
|
|
import config as cfg
|
|
|
|
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrilic loggng
|
|
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
|
|
|
logging.basicConfig(filename=f"{cfg.DEPLOY_PATH}/log.log",
|
|
filemode='a',
|
|
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
|
|
datefmt='%H:%M:%S',
|
|
level=logging.INFO)
|
|
|
|
logger = logging.getLogger()
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.secret_key = cfg.FLASK_APIKEY
|
|
app.config["APPLICATION_ROOT"] = "/api"
|
|
app.config["WTF_CSRF_ENABLED"] = False
|
|
|
|
app.config["RECAPTCHA_PUBLIC_KEY"] = cfg.RECAPTCHA_PUBLIC_KEY
|
|
app.config["RECAPTCHA_PRIVATE_KEY"] = cfg.RECAPTCHA_PRIVATE_KEY
|
|
|
|
app.config['EXECUTOR_TYPE'] = 'process'
|
|
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
|
|
|
|
executor = Executor(app)
|
|
|
|
|
|
def send_telegram(message, silent=False):
|
|
telegram_send.send(messages=[message], conf=f'{cfg.DEPLOY_PATH}/telegram.conf', silent=silent)
|
|
|
|
|
|
class ContactForm(FlaskForm):
|
|
name = StringField('Username', validators=[DataRequired()])
|
|
contacts = StringField('Contacts', validators=[DataRequired()])
|
|
message = StringField('Message')
|
|
recaptcha = RecaptchaField()
|
|
|
|
|
|
@app.route('/contact/', methods=['POST'])
|
|
def contact():
|
|
form = ContactForm(request.form)
|
|
|
|
if form.validate_on_submit():
|
|
text = cfg.CONTACT_REQUEST_TEMPLATE.format(
|
|
name=form.name.data,
|
|
contacts=form.contacts.data,
|
|
message=form.message.data
|
|
)
|
|
logger.info(f'{form.name.data} - {form.contacts.data}: [{form.message.data}]')
|
|
executor.submit(send_telegram, text)
|
|
return '', 200
|
|
|
|
return '', 400
|
|
|
|
|
|
@app.route('/')
|
|
def hello_world():
|
|
return 'Hello, World!'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run()
|