misc: add Config class, http_server: properly handle invalid methods, properly handle signals
This commit is contained in:
parent
ab87e880a1
commit
293ed5e892
|
@ -32,31 +32,31 @@ def log_import_error(package, *message):
|
|||
traceback.print_exc()
|
||||
|
||||
|
||||
try:
|
||||
from izzylib.sql import SqlColumn, CustomRows, SqlSession, SqlDatabase, Tables, SqliteClient, SqliteColumn, SqliteServer, SqliteSession
|
||||
except ImportError:
|
||||
log_import_error('sql', 'Failed to import SQL classes. Connecting to SQL databases is disabled')
|
||||
#try:
|
||||
#from izzylib.sql import SqlColumn, CustomRows, SqlSession, SqlDatabase, Tables, SqliteClient, SqliteColumn, SqliteServer, SqliteSession
|
||||
#except ImportError:
|
||||
#log_import_error('sql', 'Failed to import SQL classes. Connecting to SQL databases is disabled')
|
||||
|
||||
try:
|
||||
from izzylib.tinydb import TinyDatabase, TinyRow, TinyRows
|
||||
except ImportError:
|
||||
log_import_error('tinydb', 'Failed to import TinyDB classes. TinyDB database is disabled')
|
||||
#try:
|
||||
#from izzylib.tinydb import TinyDatabase, TinyRow, TinyRows
|
||||
#except ImportError:
|
||||
#log_import_error('tinydb', 'Failed to import TinyDB classes. TinyDB database is disabled')
|
||||
|
||||
try:
|
||||
from izzylib.template import Template, Color
|
||||
except ImportError:
|
||||
log_import_error('template', 'Failed to import http template classes. Jinja and HAML templates disabled')
|
||||
#try:
|
||||
#from izzylib.template import Template, Color
|
||||
#except ImportError:
|
||||
#log_import_error('template', 'Failed to import http template classes. Jinja and HAML templates disabled')
|
||||
|
||||
try:
|
||||
from izzylib.http_urllib_client import *
|
||||
#try:
|
||||
#from izzylib.http_urllib_client import *
|
||||
|
||||
except ImportError:
|
||||
log_import_error('http_urllib_client', 'Failed to import Requests http client classes. Requests http client is disabled')
|
||||
#except ImportError:
|
||||
#log_import_error('http_urllib_client', 'Failed to import Requests http client classes. Requests http client is disabled')
|
||||
|
||||
try:
|
||||
from izzylib.http_server import PasswordHasher, HttpServer, HttpServerRequest, HttpServerResponse
|
||||
except ImportError:
|
||||
log_import_error('http_server', 'Failed to import HTTP server classes. The HTTP server will be disabled')
|
||||
#try:
|
||||
#from izzylib.http_server import PasswordHasher, HttpServer, HttpServerRequest, HttpServerResponse
|
||||
#except ImportError:
|
||||
#log_import_error('http_server', 'Failed to import HTTP server classes. The HTTP server will be disabled')
|
||||
|
||||
try:
|
||||
from izzylib import dbus
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import logging,multiprocessing, sanic, signal, traceback
|
||||
|
||||
|
||||
from functools import partial
|
||||
from multidict import CIMultiDict
|
||||
from multiprocessing import cpu_count, current_process
|
||||
from urllib.parse import parse_qsl
|
||||
|
@ -149,7 +150,7 @@ class Application(sanic.Sanic):
|
|||
if self.cfg.access_log:
|
||||
self.add_middleware(AccessLog)
|
||||
|
||||
msg = f'Starting {self.cfg.name} at {self.cfg.host}:{self.cfg.port}'
|
||||
msg = f'Starting {self.cfg.name} at {self.cfg.listen}:{self.cfg.port}'
|
||||
|
||||
if self.cfg.workers > 1:
|
||||
msg += f' with {self.cfg.workers} workers'
|
||||
|
@ -160,13 +161,14 @@ class Application(sanic.Sanic):
|
|||
port = self.cfg.port,
|
||||
workers = self.cfg.workers,
|
||||
access_log = False,
|
||||
debug = False
|
||||
debug = False,
|
||||
register_sys_signals = False
|
||||
)
|
||||
|
||||
|
||||
def finish(self):
|
||||
def finish(self, *args):
|
||||
if self.cfg.sig_handler:
|
||||
self.cfg.sig_handler(*self.cfg.sig_handler_args, **self.cfg.sig_handler_kwargs)
|
||||
self.cfg.sig_handler(self, *self.cfg.sig_handler_args, **self.cfg.sig_handler_kwargs)
|
||||
|
||||
self.stop()
|
||||
izzylog.info('Bye! :3')
|
||||
|
|
|
@ -50,6 +50,9 @@ class Config(DotDict):
|
|||
|
||||
super().__init__({**self.defaults, **kwargs})
|
||||
|
||||
if not kwargs.get('host'):
|
||||
self.host = self.listen
|
||||
|
||||
if kwargs.get('host') and not kwargs.get('web_host'):
|
||||
self.web_host = self.host
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from sanic.exceptions import SanicException
|
||||
from sanic.views import HTTPMethodView
|
||||
|
||||
from .response import Response
|
||||
|
@ -14,8 +15,13 @@ class View(HTTPMethodView):
|
|||
self.app = request.app
|
||||
self.cfg = request.app.cfg
|
||||
|
||||
response = Response(self.app, request)
|
||||
handler = getattr(self, request.method.lower(), None)
|
||||
return handler(request, Response(self.app, request), *args, **kwargs)
|
||||
|
||||
if not handler:
|
||||
raise SanicException('Method not supported', status_code=405)
|
||||
|
||||
return handler(request, response, *args, **kwargs)
|
||||
|
||||
|
||||
### Frontend Template Views ###
|
||||
|
|
|
@ -8,6 +8,7 @@ from urllib.parse import urlparse
|
|||
|
||||
from . import izzylog
|
||||
from .dotdict import DotDict
|
||||
from .path import Path
|
||||
|
||||
|
||||
__all__ = [
|
||||
|
@ -29,6 +30,7 @@ __all__ = [
|
|||
'time_function_pprint',
|
||||
'timestamp',
|
||||
'var_name',
|
||||
'Config',
|
||||
'Url'
|
||||
]
|
||||
|
||||
|
@ -464,6 +466,44 @@ def var_name(single=True, **kwargs):
|
|||
return key[0] if single else keys
|
||||
|
||||
|
||||
class Config(DotDict):
|
||||
def __init__(self, json_file=None, **defaults):
|
||||
self._defaults = defaults
|
||||
self._json = Path(json_file)
|
||||
|
||||
super().__init__(defaults)
|
||||
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
if not key in self._defaults:
|
||||
raise KeyError(f'Not a valid config option: {key}')
|
||||
|
||||
super().__setitem__(key, value)
|
||||
|
||||
|
||||
def reset(self, key=None):
|
||||
if not key:
|
||||
self.update(self._defaults)
|
||||
|
||||
else:
|
||||
self[key] = self._defaults[key]
|
||||
|
||||
|
||||
def load(self):
|
||||
try:
|
||||
self.load_json(self._json)
|
||||
return True
|
||||
|
||||
except FileNotFoundError:
|
||||
izzylog.warning('Cannot find path to config file:', self._json)
|
||||
return False
|
||||
|
||||
|
||||
def save(self, indent='\t'):
|
||||
self._json.parent.mkdir()
|
||||
self.save_json(self._json, indent=indent)
|
||||
|
||||
|
||||
class Url(str):
|
||||
protocols = {
|
||||
'http': 80,
|
||||
|
|
Loading…
Reference in a new issue