Add support for MySQL and PostgreSQL
This commit is contained in:
parent
6e8f3ee321
commit
4d626d423c
75
config.py
75
config.py
@ -23,6 +23,12 @@ LOG.addHandler(stream)
|
|||||||
|
|
||||||
IP_TO_LOCATION = None
|
IP_TO_LOCATION = None
|
||||||
API_KEY = None
|
API_KEY = None
|
||||||
|
DB_NAME = None
|
||||||
|
DB_ENGINE = None
|
||||||
|
DB_HOST = None
|
||||||
|
DB_PORT = None
|
||||||
|
DB_USER = None
|
||||||
|
DB_PASSWORD = None
|
||||||
|
|
||||||
schema = {
|
schema = {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
@ -34,7 +40,26 @@ schema = {
|
|||||||
"api_key": {"type": "string"},
|
"api_key": {"type": "string"},
|
||||||
},
|
},
|
||||||
"required": ["ip_to_location"],
|
"required": ["ip_to_location"],
|
||||||
}
|
},
|
||||||
|
"database": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string", "default": "linklogger"},
|
||||||
|
"engine": {"type": "string"},
|
||||||
|
"host": {"type": "string"},
|
||||||
|
"port": {"type": "integer"},
|
||||||
|
"user": {"type": "string"},
|
||||||
|
"password": {"type": "string"},
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"engine",
|
||||||
|
"host",
|
||||||
|
"port",
|
||||||
|
"user",
|
||||||
|
"password",
|
||||||
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"required": ["config"],
|
"required": ["config"],
|
||||||
}
|
}
|
||||||
@ -50,16 +75,26 @@ def load_config():
|
|||||||
try:
|
try:
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
file_contents = f.read()
|
file_contents = f.read()
|
||||||
validate_config(file_contents)
|
if not validate_config(file_contents):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
# Create new config.yaml w/ template
|
# Create new config.yaml w/ template
|
||||||
with open(file_path, "w") as f:
|
with open(file_path, "w") as f:
|
||||||
f.write(
|
f.write(
|
||||||
"""
|
"""config:
|
||||||
config:
|
|
||||||
ip_to_location: false
|
ip_to_location: false
|
||||||
api_key: ''"""
|
api_key: ''
|
||||||
|
|
||||||
|
database:
|
||||||
|
engine: 'sqlite'
|
||||||
|
name: ''
|
||||||
|
host: ''
|
||||||
|
port: ''
|
||||||
|
user: ''
|
||||||
|
password: ''"""
|
||||||
)
|
)
|
||||||
LOG.critical(
|
LOG.critical(
|
||||||
"`config.yaml` was not found, a template has been created."
|
"`config.yaml` was not found, a template has been created."
|
||||||
@ -67,12 +102,10 @@ config:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
# Validate the options within config.yaml
|
# Validate the options within config.yaml
|
||||||
def validate_config(file_contents):
|
def validate_config(file_contents):
|
||||||
global IP_TO_LOCATION, API_KEY
|
global IP_TO_LOCATION, API_KEY, DB_NAME, DB_ENGINE, DB_HOST, DB_PORT, DB_USER, DB_PASSWORD
|
||||||
config = yaml.safe_load(file_contents)
|
config = yaml.safe_load(file_contents)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -88,5 +121,31 @@ def validate_config(file_contents):
|
|||||||
if IP_TO_LOCATION:
|
if IP_TO_LOCATION:
|
||||||
if not config["config"]["api_key"]:
|
if not config["config"]["api_key"]:
|
||||||
LOG.error("API_KEY is not set")
|
LOG.error("API_KEY is not set")
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
API_KEY = config["config"]["api_key"]
|
API_KEY = config["config"]["api_key"]
|
||||||
|
|
||||||
|
#
|
||||||
|
# Set/Validate the DATABASE section of the config.yaml
|
||||||
|
#
|
||||||
|
if "database" in config:
|
||||||
|
if config["database"]["engine"] not in [
|
||||||
|
"sqlite",
|
||||||
|
"mysql",
|
||||||
|
"postgresql",
|
||||||
|
]:
|
||||||
|
LOG.error(
|
||||||
|
"database_engine must be either 'sqlite', 'mysql', or"
|
||||||
|
" 'postgresql'"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
DB_ENGINE = config["database"]["engine"]
|
||||||
|
|
||||||
|
DB_NAME = config["database"]["name"]
|
||||||
|
DB_HOST = config["database"]["host"]
|
||||||
|
DB_PORT = config["database"]["port"]
|
||||||
|
DB_USER = config["database"]["user"]
|
||||||
|
DB_PASSWORD = config["database"]["password"]
|
||||||
|
|
||||||
|
return True
|
||||||
|
17
database.py
17
database.py
@ -1,13 +1,18 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
import os
|
|
||||||
|
|
||||||
# Create 'data' directory at root if it doesn't exist
|
import config
|
||||||
if not os.path.exists("data"):
|
|
||||||
os.makedirs("data")
|
|
||||||
|
|
||||||
engine = create_engine("sqlite:///data/data.db")
|
if config.DB_ENGINE == "mysql":
|
||||||
|
database_url = f"mysql+pymysql://{config.DB_USER}:{config.DB_PASSWORD}@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}"
|
||||||
|
|
||||||
|
elif config.DB_ENGINE == "postgresql":
|
||||||
|
database_url = f"postgresql+psycopg2://{config.DB_USER}:{config.DB_PASSWORD}@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}"
|
||||||
|
|
||||||
|
else:
|
||||||
|
database_url = "sqlite:///data/data.db"
|
||||||
|
|
||||||
|
engine = create_engine(database_url)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from api.main import app
|
|
||||||
from database import Base, engine
|
|
||||||
|
|
||||||
|
|
||||||
Base.metadata.create_all(bind=engine)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if config.load_config():
|
if config.load_config():
|
||||||
|
from api.main import app
|
||||||
|
from database import Base, engine
|
||||||
|
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
uvicorn.run(app, port=5252)
|
uvicorn.run(app, port=5252)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user