From bc56226a282ac8f7f1e9b3c543bec981947533ce Mon Sep 17 00:00:00 2001 From: Marc-Andre Ferland Date: Fri, 14 Oct 2022 03:42:43 -0400 Subject: [PATCH] Grouped many endpoints into one --- ui/media/main.js | 11 ++++++----- ui/server.py | 47 ++++++++++++++++++++--------------------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/ui/media/main.js b/ui/media/main.js index 0bcb2fc3..e2513703 100644 --- a/ui/media/main.js +++ b/ui/media/main.js @@ -1123,7 +1123,7 @@ useBetaChannelField.addEventListener('click', async function(e) { async function getAppConfig() { try { - let res = await fetch('/app_config') + let res = await fetch('/get?key=app_config') const config = await res.json() if (config.update_branch === 'beta') { @@ -1139,7 +1139,7 @@ async function getAppConfig() { async function getModels() { try { - let res = await fetch('/models') + let res = await fetch('/get?key=models') const models = await res.json() let activeModel = models['active'] @@ -1404,7 +1404,7 @@ async function getDiskPath() { return } - let res = await fetch('/output_dir') + let res = await fetch('/get?key=output_dir') if (res.status === 200) { res = await res.json() res = res[0] @@ -1515,14 +1515,15 @@ function resizeModifierCards(val) { const classes = card.className.split(' ').filter(c => !c.startsWith(cardSizePrefix)) card.className = classes.join(' ').trim() - if(val != 0) + if(val != 0) { card.classList.add(cardSize(val)) + } }) } async function loadModifiers() { try { - let res = await fetch('/modifiers.json?v=2') + let res = await fetch('/get?key=modifiers') if (res.status === 200) { res = await res.json() diff --git a/ui/server.py b/ui/server.py index 5d7f1bfd..41bcaee9 100644 --- a/ui/server.py +++ b/ui/server.py @@ -242,31 +242,17 @@ async def setAppConfig(req : SetAppConfigRequest): print(traceback.format_exc()) return HTTPException(status_code=500, detail=str(e)) -@app.get('/app_config') -def getAppConfig(): +def getConfig(default_val={}): try: config_json_path = os.path.join(CONFIG_DIR, 'config.json') - if not os.path.exists(config_json_path): - return HTTPException(status_code=500, detail="No config file") - + return default_val with open(config_json_path, 'r') as f: return json.load(f) except Exception as e: + print(str(e)) print(traceback.format_exc()) - return HTTPException(status_code=500, detail=str(e)) - -def getConfig(): - try: - config_json_path = os.path.join(CONFIG_DIR, 'config.json') - - if not os.path.exists(config_json_path): - return {} - - with open(config_json_path, 'r') as f: - return json.load(f) - except Exception as e: - return {} + return default_val def setConfig(config): try: @@ -275,9 +261,9 @@ def setConfig(config): with open(config_json_path, 'w') as f: return json.dump(config, f) except: + print(str(e)) print(traceback.format_exc()) -@app.get('/models') def getModels(): models = { 'active': { @@ -307,14 +293,21 @@ def getModels(): return models -@app.get('/modifiers.json') -def read_modifiers(): - headers = {"Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0"} - return FileResponse(os.path.join(SD_UI_DIR, 'modifiers.json'), headers=headers) - -@app.get('/output_dir') -def read_home_dir(): - return {outpath} +@app.get('/get') +def read_web_data(key:str=None): + if key is None: # /get without parameters, stable-diffusion easter egg. + return HTTPException(status_code=418, detail="StableDiffusion is drawing a teapot!") # HTTP418 I'm a teapot + elif key == 'app_config': + config = getConfig(default_val=None) + if config is None: + return HTTPException(status_code=500, detail="Config file is missing or unreadable") + return config + elif key == 'models': + return getModels() + elif key == 'modifiers': return FileResponse(os.path.join(SD_UI_DIR, 'modifiers.json'), headers=NOCACHE_HEADERS) + elif key == 'output_dir': return {outpath} + else: + return HTTPException(status_code=404, detail=f'Request for unknown {key}') # HTTP404 Not Found # don't log certain requests class LogSuppressFilter(logging.Filter):