diff --git a/smib/common/config.py b/smib/common/config.py index d4ca363..2eebada 100644 --- a/smib/common/config.py +++ b/smib/common/config.py @@ -9,6 +9,8 @@ warnings.filterwarnings("ignore", category=RuntimeWarning) +ROOT_DIRECTORY = Path(smib.__file__).parent + APPLICATION_NAME = config('APPLICATION_NAME', default='S.M.I.B.') SLACK_APP_TOKEN = config('SLACK_APP_TOKEN') @@ -23,6 +25,8 @@ cast=urlparse) WEBSERVER_SECRET_KEY = config('WEBSERVER_SECRET_KEY', default=os.urandom(24)) WEBSERVER_PATH_PREFIX = config('WEBSERVER_PATH_PREFIX', default='/smib') +WEBSERVER_TEMPLATES_DIRECTORY = config('WEBSERVER_TEMPLATES_DIRECTORY', default=ROOT_DIRECTORY / 'webserver' / 'templates', cast=Path) +WEBSERVER_STATIC_DIRECTORY = config('WEBSERVER_STATIC_DIRECTORY', default=ROOT_DIRECTORY / 'webserver' / 'static', cast=Path) WEBSOCKET_SCHEME = config('WEBSERVER_SCHEME', default='ws') WEBSOCKET_HOST = config('WEBSOCKET_HOST', default='localhost') @@ -33,5 +37,4 @@ cast=urlparse) WEBSOCKET_ALLOWED_HOSTS = config('WEBSOCKET_ALLOWED_HOSTS', default='localhost,127.0.0.1,::1', cast=Csv()) -ROOT_DIRECTORY = Path(smib.__file__).parent -PLUGINS_DIRECTORY = ROOT_DIRECTORY / 'slack' / 'plugins' +PLUGINS_DIRECTORY = config('PLUGINS_DIRECTORY', default=ROOT_DIRECTORY / 'slack' / 'plugins', cast=Path) diff --git a/smib/slack/plugin/loaders/abstract_plugin_loader.py b/smib/slack/plugin/loaders/abstract_plugin_loader.py index f4cbba1..a2b10c4 100644 --- a/smib/slack/plugin/loaders/abstract_plugin_loader.py +++ b/smib/slack/plugin/loaders/abstract_plugin_loader.py @@ -120,7 +120,7 @@ def _remove_middlewares(self, plugin: Plugin) -> None: self.app._middleware_list.remove(middleware) def reload_plugin(self, plugin: Plugin) -> Plugin: - # print(plugin) + print(f"Reloading: {plugin}") self.unload_plugin(plugin) # print(self.scheduler.get_jobs()) reloaded_plugin = self.load_plugin(plugin.directory) diff --git a/smib/slack/plugins/core/status/.disable b/smib/slack/plugins/core/status/.disable deleted file mode 100644 index e69de29..0000000 diff --git a/smib/slack/plugins/core/status/__init__.py b/smib/slack/plugins/core/status/__init__.py index 858d1c3..ed7408e 100644 --- a/smib/slack/plugins/core/status/__init__.py +++ b/smib/slack/plugins/core/status/__init__.py @@ -6,26 +6,31 @@ from urllib.request import Request from injectable import inject -from smib.slack.custom_app import CustomApp as App from smib.common.utils import http_bolt_response from smib.slack.custom_app import CustomApp app: CustomApp = inject("SlackApp") -plugin_manager = inject("PluginManager") -scheduler = inject("Scheduler") + @app.middleware def context_injector(context, next): + plugin_manager = inject("PluginManager") + scheduler = inject("Scheduler") + print('Context injector') context['plugin_manager'] = plugin_manager context['scheduler'] = scheduler next() + @app.event('http_get_status') @http_bolt_response def status(request: Request): + print('Get Status') + plugin_manager = inject("PluginManager") + scheduler = inject("Scheduler") data = { "plugin_count": len(plugin_manager.plugins), "plugins": [plugin.to_json_dict() for plugin in plugin_manager], @@ -33,13 +38,12 @@ def status(request: Request): } return data - -@app.schedule('interval', '123', seconds=500) -def scheduled_task(say): - say("Scheduled Testing Testicle", channel="random") - - -@app.schedule('interval', '123', seconds=501) -def scheduled_task(say): - say("Scheduled Testing Testicle 2", channel="random") - +# +# @app.schedule('interval', '123', seconds=5, name='Testicle') +# def scheduled_task(say): +# say("Scheduled Testing Testicle", channel="random") +# +# +# @app.schedule('interval', '123', seconds=10, name='Testicle2') +# def scheduled_task(say): +# say("Scheduled Testing Testicle 2", channel="random") diff --git a/smib/webserver/__main__.py b/smib/webserver/__main__.py index 840c28c..fe62fbc 100644 --- a/smib/webserver/__main__.py +++ b/smib/webserver/__main__.py @@ -8,7 +8,9 @@ from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles -from smib.common.config import WEBSERVER_HOST, WEBSERVER_PORT, WEBSERVER_PATH_PREFIX +from smib.common.config import ( + WEBSERVER_HOST, WEBSERVER_PORT, WEBSERVER_PATH_PREFIX, WEBSERVER_STATIC_DIRECTORY, WEBSERVER_TEMPLATES_DIRECTORY +) from smib.common.utils import is_pickleable from smib.webserver.websocket_handler import WebSocketHandler @@ -42,14 +44,23 @@ async def generate_bolt_request(fastapi_request: Request): bolt_request.body = await generate_request_body(fastapi_request) return bolt_request -current_directory = Path(__file__).parent + +def create_directories(): + if not WEBSERVER_TEMPLATES_DIRECTORY.exists(): + WEBSERVER_TEMPLATES_DIRECTORY.mkdir() + + if not WEBSERVER_STATIC_DIRECTORY.exists(): + WEBSERVER_STATIC_DIRECTORY.mkdir() + ws_handler = WebSocketHandler() app = FastAPI() router = APIRouter(prefix=WEBSERVER_PATH_PREFIX) -router.mount("/static", StaticFiles(directory=current_directory / "static"), name="static") -templates = Jinja2Templates(directory=f"{current_directory / 'templates'}") +create_directories() + +router.mount("/static", StaticFiles(directory=WEBSERVER_STATIC_DIRECTORY), name="static") +templates = Jinja2Templates(directory=str(WEBSERVER_TEMPLATES_DIRECTORY)) @router.get('/event/{event}', tags=['SMIB Events']) @@ -71,6 +82,7 @@ async def smib_home(request: Request): async def custom_404_handler(request, __): return templates.TemplateResponse("404.html", {"request": request}, status_code=404) + app.include_router(router) diff --git a/tests/websocket_server.py b/tests/websocket_server.py index 6081331..c260055 100644 --- a/tests/websocket_server.py +++ b/tests/websocket_server.py @@ -1,6 +1,6 @@ import threading from smib.common.config import WEBSOCKET_HOST, WEBSOCKET_PORT -from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket +from simple_websocket_server import SimpleWebSocketServer, WebSocket class SimpleServer(WebSocket):