Skip to content

Commit

Permalink
playing around ;)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam57719 committed Apr 17, 2024
1 parent c808767 commit cc0dc1a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
7 changes: 5 additions & 2 deletions smib/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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)
2 changes: 1 addition & 1 deletion smib/slack/plugin/loaders/abstract_plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file.
30 changes: 17 additions & 13 deletions smib/slack/plugins/core/status/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,44 @@
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],
"scheduled_jobs": [job.id for job in scheduler.get_jobs()]
}
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")
20 changes: 16 additions & 4 deletions smib/webserver/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'])
Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion tests/websocket_server.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit cc0dc1a

Please sign in to comment.