Compare commits

..

6 commits

Author SHA1 Message Date
Izalia Mae 6a90fbadb5 Merge branch 'sqldatabase' into 'master'
Draft: Use SQL database backend

See merge request pleroma/relay!47
2022-12-14 18:42:10 +00:00
Izalia Mae e3c4377db6 fix NameError in Connection.delete_instance 2022-12-14 09:03:57 -05:00
Izalia Mae 1d8de63d95 fix AttributeError when fetching an instance by name 2022-12-14 08:39:56 -05:00
Izalia Mae 0322fa567b set log level from database config 2022-12-14 05:31:45 -05:00
Izalia Mae 426adf1117 fix tinysql url 2022-12-14 05:14:17 -05:00
Izalia Mae 3907620f24 add missing imports for tinysql 2022-12-14 03:28:50 -05:00
5 changed files with 38 additions and 18 deletions

View file

@ -14,7 +14,15 @@ a = Analysis(
'aputils.errors',
'aputils.misc',
'aputils.objects',
'aputils.signer'
'aputils.signer',
'tinysql.base',
'tinysql.database',
'tinysql.error',
'tinysql.mysql',
'tinysql.postgresql',
'tinysql.sqlite',
'tinysql.statement'
],
hookspath=[],
hooksconfig={},

View file

@ -14,6 +14,7 @@ from datetime import datetime, timedelta
from .config import Config
from .database import Database
from .http_client import HttpClient
from .logger import set_level
from .misc import DotDict, check_open_port, set_app
from .views import routes
@ -41,6 +42,9 @@ class Application(web.Application):
self.database.create()
self.set_signal_handler()
with self.database.session as s:
set_level(s.get_config('log_level'))
@property
def client(self):
@ -113,6 +117,7 @@ class Application(web.Application):
logging.info(f'Starting webserver at {self.config.host} ({self.config.listen}:{self.config.port})')
asyncio.run(self.handle_run())
self.database.disconnect()
def stop(self, *_):

View file

@ -4,6 +4,7 @@ from datetime import datetime
from tinysql import Column, Table
from urllib.parse import urlparse
from .logger import set_level
from .misc import AppBase, DotDict, boolean
@ -124,7 +125,7 @@ class Connection(tinysql.ConnectionMixin):
row = self.get_instance(domain)
if not row:
raise KeyError(name)
raise KeyError(domain)
self.delete('instances', id=row.id)
@ -186,11 +187,12 @@ class Connection(tinysql.ConnectionMixin):
query = 'SELECT * FROM instances WHERE domain = :data OR actor = :data OR inbox = :data'
row = self.execute(query, dict(data=data), table='instances').one()
return row if row.joined else None
return row if row and row.joined else None
def get_instances(self):
query = 'SELECT * FROM instances WHERE joined IS NOT NULL'
query += ' ORDER BY domain ASC'
return self.execute(query, table='instances').all()
@ -203,7 +205,7 @@ class Connection(tinysql.ConnectionMixin):
def get_requests(self):
query = 'SELECT * FROM instances WHERE joined IS NULL'
query = 'SELECT * FROM instances WHERE joined IS NULL ORDER BY domain ASC'
return self.execute(query, table='instances').all()
@ -245,6 +247,9 @@ class Connection(tinysql.ConnectionMixin):
if value == '__DEFAULT__':
value = DEFAULT_CONFIG[key][1]
if key == 'log_level':
set_level(value)
row = self.select('config', key=key).one()
if row:

View file

@ -4,6 +4,16 @@ import os
from pathlib import Path
LEVELS = {
'critical': logging.CRITICAL,
'error': logging.ERROR,
'warning': logging.WARNING,
'info': logging.INFO,
'verbose': 15,
'debug': logging.DEBUG
}
## Add the verbose logging level
def verbose(message, *args, **kwargs):
if not logging.root.isEnabledFor(logging.VERBOSE):
@ -15,10 +25,6 @@ setattr(logging, 'verbose', verbose)
setattr(logging, 'VERBOSE', 15)
logging.addLevelName(15, 'VERBOSE')
## Get log level and file from environment if possible
env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
try:
env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve()
@ -26,14 +32,6 @@ except TypeError:
env_log_file = None
## Make sure the level from the environment is valid
try:
log_level = getattr(logging, env_log_level)
except AttributeError:
log_level = logging.INFO
## Set logging config
handlers = [logging.StreamHandler()]
@ -41,7 +39,11 @@ if env_log_file:
handlers.append(logging.FileHandler(env_log_file))
logging.basicConfig(
level = log_level,
level = logging.INFO,
format = "[%(asctime)s] %(levelname)s: %(message)s",
handlers = handlers
)
def set_level(level):
logging.getLogger().setLevel(LEVELS[level.lower()])

View file

@ -4,4 +4,4 @@ aputils@https://git.barkshark.xyz/barkshark/aputils/archive/0.1.3.tar.gz
cachetools>=5.2.0
click>=8.1.2
pyyaml>=6.0
tinysql[postgres,mysql]@https:/git.barkshark.xyz/barkshark/tinysql/archive/0.1.0.tar.gz
tinysql[postgres,mysql]@https://git.barkshark.xyz/barkshark/tinysql/archive/0.1.0.tar.gz