Backend
+ Added requirements.txt * Moved api keys to env
This commit is contained in:
parent
d867298599
commit
24ad080a61
4
backend/.env.template
Normal file
4
backend/.env.template
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
RECAPTCHA_PUBLIC_KEY=""
|
||||||
|
RECAPTCHA_PRIVATE_KEY=""
|
||||||
|
FLASK_APIKEY=""
|
||||||
|
DEPLOY_PATH="/home/c/cn52774/alex-sharoff/public_html/backend"
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
import os
|
|
||||||
|
|
||||||
# TODO: store keys in env variables
|
|
||||||
RECAPTCHA_PUBLIC_KEY = "RECAPTCHA_PUBLIC_KEY"
|
|
||||||
RECAPTCHA_PRIVATE_KEY = "RECAPTCHA_PRIVATE_KEY-L4DsNDlza17N7dEz36"
|
|
||||||
|
|
||||||
FLASK_APIKEY = "FLASK_APIKEY"
|
|
||||||
|
|
||||||
if 'ALEX_DEBUG' in os.environ:
|
|
||||||
DEPLOY_PATH = '.'
|
|
||||||
else:
|
|
||||||
DEPLOY_PATH = '/home/c/cn52774/alex-sharoff/public_html/backend'
|
|
||||||
|
|
||||||
CONTACT_REQUEST_TEMPLATE = """
|
|
||||||
Обращение через форму обратной связи с сайта alex-sharoff.ru:
|
|
||||||
|
|
||||||
Имя: {name}
|
|
||||||
Контакты: {contacts}
|
|
||||||
-------
|
|
||||||
{message}
|
|
||||||
"""
|
|
||||||
|
|
@ -9,12 +9,15 @@ import telegram_send
|
||||||
from wtforms import StringField
|
from wtforms import StringField
|
||||||
from wtforms.validators import DataRequired
|
from wtforms.validators import DataRequired
|
||||||
|
|
||||||
import config as cfg
|
from environs import Env
|
||||||
|
|
||||||
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrilic loggng
|
env = Env()
|
||||||
|
env.read_env()
|
||||||
|
|
||||||
|
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrillic logging
|
||||||
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
||||||
|
|
||||||
logging.basicConfig(filename=f"{cfg.DEPLOY_PATH}/log.log",
|
logging.basicConfig(filename=f'{env.str("DEPLOY_PATH")}/log.log',
|
||||||
filemode='a',
|
filemode='a',
|
||||||
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
|
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
|
||||||
datefmt='%H:%M:%S',
|
datefmt='%H:%M:%S',
|
||||||
|
|
@ -24,12 +27,12 @@ logger = logging.getLogger()
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
app.secret_key = cfg.FLASK_APIKEY
|
app.secret_key = env.str("FLASK_APIKEY")
|
||||||
app.config["APPLICATION_ROOT"] = "/api"
|
app.config["APPLICATION_ROOT"] = "/api"
|
||||||
app.config["WTF_CSRF_ENABLED"] = False
|
app.config["WTF_CSRF_ENABLED"] = False
|
||||||
|
|
||||||
app.config["RECAPTCHA_PUBLIC_KEY"] = cfg.RECAPTCHA_PUBLIC_KEY
|
app.config["RECAPTCHA_PUBLIC_KEY"] = env.str("RECAPTCHA_PUBLIC_KEY")
|
||||||
app.config["RECAPTCHA_PRIVATE_KEY"] = cfg.RECAPTCHA_PRIVATE_KEY
|
app.config["RECAPTCHA_PRIVATE_KEY"] = env.str("RECAPTCHA_PRIVATE_KEY")
|
||||||
|
|
||||||
app.config['EXECUTOR_TYPE'] = 'process'
|
app.config['EXECUTOR_TYPE'] = 'process'
|
||||||
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
|
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
|
||||||
|
|
@ -38,7 +41,7 @@ executor = Executor(app)
|
||||||
|
|
||||||
|
|
||||||
def send_telegram(message, silent=False):
|
def send_telegram(message, silent=False):
|
||||||
telegram_send.send(messages=[message], conf=f'{cfg.DEPLOY_PATH}/telegram.conf', silent=silent)
|
telegram_send.send(messages=[message], conf=f'{env.str("DEPLOY_PATH")}/telegram.conf', silent=silent)
|
||||||
|
|
||||||
|
|
||||||
class ContactForm(FlaskForm):
|
class ContactForm(FlaskForm):
|
||||||
|
|
@ -52,8 +55,17 @@ class ContactForm(FlaskForm):
|
||||||
def contact():
|
def contact():
|
||||||
form = ContactForm(request.form)
|
form = ContactForm(request.form)
|
||||||
|
|
||||||
|
msg_template = """
|
||||||
|
Обращение через форму обратной связи с сайта alex-sharoff.ru:
|
||||||
|
|
||||||
|
Имя: {name}
|
||||||
|
Контакты: {contacts}
|
||||||
|
-------
|
||||||
|
{message}
|
||||||
|
"""
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
text = cfg.CONTACT_REQUEST_TEMPLATE.format(
|
text = msg_template.format(
|
||||||
name=form.name.data,
|
name=form.name.data,
|
||||||
contacts=form.contacts.data,
|
contacts=form.contacts.data,
|
||||||
message=form.message.data
|
message=form.message.data
|
||||||
|
|
|
||||||
7
backend/requirements.txt
Normal file
7
backend/requirements.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
environs==11.0.0
|
||||||
|
Flask==2.3.2
|
||||||
|
Flask-Cors==3.0.10
|
||||||
|
Flask-Executor==1.0.0
|
||||||
|
Flask-WTF==1.1.1
|
||||||
|
telegram-send==0.34
|
||||||
|
WTForms==3.0.1
|
||||||
Loading…
Reference in New Issue
Block a user