use a dotenv file

This commit is contained in:
Thomas Rubini 2023-01-13 11:46:54 +01:00
parent e3f779abeb
commit bb04e744c3
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
5 changed files with 13 additions and 33 deletions

2
.env.dist Normal file
View File

@ -0,0 +1,2 @@
FLASK_SECRET=""
DISCORD_BOT_TOKEN=""

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
**/__pycache__
instance/
data_persistance/secret.py
**/.vscode
**/.vscode
.env

4
app.py
View File

@ -1,3 +1,7 @@
# Load .env file
from dotenv import load_dotenv
load_dotenv()
from truthseeker import APP as app # the variable 'app' is detected by `flask run`
if __name__ == "__main__":

View File

@ -4,3 +4,5 @@ Flask-SocketIO==5.3.2
SQLAlchemy==1.4.20
pymysql==1.0.2
discord.py==2.1.0
discord.py==2.1.0
python-dotenv==0.21.0

View File

@ -18,13 +18,13 @@ class TruthSeekerApp(flask.Flask):
super().__init__("truthseeker")
self.games_list = {}
self.set_app_secret()
self.config["SECRET_KEY"] = os.getenv("FLASK_SECRET")
self.socketio_app = SocketIO(self)
self.discord_bot = discord_bot.DiscordBot()
token = self.get_discord_bot_token()
token = os.getenv("DISCORD_BOT_TOKEN")
if token:
pass
self.discord_bot.start(token)
@ -34,35 +34,6 @@ class TruthSeekerApp(flask.Flask):
def run_app(self):
self.socketio_app.run(self)
def set_app_secret(self) -> None:
"""
Set the secret used by flask
"""
if os.path.isfile("instance/secret.txt"):
f = open("instance/secret.txt", "r")
self.config["SECRET_KEY"] = f.read()
f.close()
print("Read secret from secret.txt !")
else:
import secrets
self.config["SECRET_KEY"] = secrets.token_hex()
os.makedirs("instance", exist_ok=True)
f = open("instance/secret.txt", "w")
f.write(self.config["SECRET_KEY"])
f.close()
print("Generated secret and wrote to secret.txt !")
def get_discord_bot_token(self) -> str:
"""
Get the token used by the discord bot
"""
if os.path.isfile("instance/discord_bot_token.txt"):
f = open("instance/discord_bot_token.txt", "r")
token = f.read()
f.close()
return token
return None
APP = TruthSeekerApp()
from truthseeker.routes import routes_api, routes_ui, routes_socketio