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() {
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()

View File

@ -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):