Grouped many endpoints into one

This commit is contained in:
Marc-Andre Ferland 2022-10-14 03:42:43 -04:00
parent a6e5474fdb
commit bc56226a28
2 changed files with 26 additions and 32 deletions

View File

@ -1123,7 +1123,7 @@ useBetaChannelField.addEventListener('click', async function(e) {
async function getAppConfig() { async function getAppConfig() {
try { try {
let res = await fetch('/app_config') let res = await fetch('/get?key=app_config')
const config = await res.json() const config = await res.json()
if (config.update_branch === 'beta') { if (config.update_branch === 'beta') {
@ -1139,7 +1139,7 @@ async function getAppConfig() {
async function getModels() { async function getModels() {
try { try {
let res = await fetch('/models') let res = await fetch('/get?key=models')
const models = await res.json() const models = await res.json()
let activeModel = models['active'] let activeModel = models['active']
@ -1404,7 +1404,7 @@ async function getDiskPath() {
return return
} }
let res = await fetch('/output_dir') let res = await fetch('/get?key=output_dir')
if (res.status === 200) { if (res.status === 200) {
res = await res.json() res = await res.json()
res = res[0] res = res[0]
@ -1515,14 +1515,15 @@ function resizeModifierCards(val) {
const classes = card.className.split(' ').filter(c => !c.startsWith(cardSizePrefix)) const classes = card.className.split(' ').filter(c => !c.startsWith(cardSizePrefix))
card.className = classes.join(' ').trim() card.className = classes.join(' ').trim()
if(val != 0) if(val != 0) {
card.classList.add(cardSize(val)) card.classList.add(cardSize(val))
}
}) })
} }
async function loadModifiers() { async function loadModifiers() {
try { try {
let res = await fetch('/modifiers.json?v=2') let res = await fetch('/get?key=modifiers')
if (res.status === 200) { if (res.status === 200) {
res = await res.json() res = await res.json()

View File

@ -242,31 +242,17 @@ async def setAppConfig(req : SetAppConfigRequest):
print(traceback.format_exc()) print(traceback.format_exc())
return HTTPException(status_code=500, detail=str(e)) return HTTPException(status_code=500, detail=str(e))
@app.get('/app_config') def getConfig(default_val={}):
def getAppConfig():
try: try:
config_json_path = os.path.join(CONFIG_DIR, 'config.json') config_json_path = os.path.join(CONFIG_DIR, 'config.json')
if not os.path.exists(config_json_path): 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: with open(config_json_path, 'r') as f:
return json.load(f) return json.load(f)
except Exception as e: except Exception as e:
print(str(e))
print(traceback.format_exc()) print(traceback.format_exc())
return HTTPException(status_code=500, detail=str(e)) return default_val
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 {}
def setConfig(config): def setConfig(config):
try: try:
@ -275,9 +261,9 @@ def setConfig(config):
with open(config_json_path, 'w') as f: with open(config_json_path, 'w') as f:
return json.dump(config, f) return json.dump(config, f)
except: except:
print(str(e))
print(traceback.format_exc()) print(traceback.format_exc())
@app.get('/models')
def getModels(): def getModels():
models = { models = {
'active': { 'active': {
@ -307,14 +293,21 @@ def getModels():
return models return models
@app.get('/modifiers.json') @app.get('/get')
def read_modifiers(): def read_web_data(key:str=None):
headers = {"Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0"} if key is None: # /get without parameters, stable-diffusion easter egg.
return FileResponse(os.path.join(SD_UI_DIR, 'modifiers.json'), headers=headers) return HTTPException(status_code=418, detail="StableDiffusion is drawing a teapot!") # HTTP418 I'm a teapot
elif key == 'app_config':
@app.get('/output_dir') config = getConfig(default_val=None)
def read_home_dir(): if config is None:
return {outpath} 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 # don't log certain requests
class LogSuppressFilter(logging.Filter): class LogSuppressFilter(logging.Filter):