Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
nomad-FAIR
Commits
bac3146d
Commit
bac3146d
authored
Mar 12, 2021
by
Markus Scheidgen
Browse files
Added a more friendly default 404 response for the nomad app.
parent
fd8e62d2
Pipeline
#95568
passed with stages
in 19 minutes and 35 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
bac3146d
...
...
@@ -34,3 +34,4 @@ parser.osio.log
gui/src/metainfo.json
gui/src/searchQuantities.json
examples/workdir/
*/node_modules/
nomad/app/main.py
View file @
bac3146d
...
...
@@ -17,8 +17,11 @@
#
from
fastapi
import
FastAPI
,
status
,
Response
from
fastapi.responses
import
JSONResponse
,
HTMLResponse
from
fastapi.middleware.wsgi
import
WSGIMiddleware
from
starlette.middleware.base
import
BaseHTTPMiddleware
from
starlette.exceptions
import
HTTPException
as
StarletteHTTPException
from
fastapi.exception_handlers
import
http_exception_handler
as
default_http_exception_handler
from
nomad
import
config
,
infrastructure
...
...
@@ -70,3 +73,57 @@ async def startup_event():
pass
infrastructure
.
setup
()
@
app
.
exception_handler
(
StarletteHTTPException
)
async
def
http_exception_handler
(
request
,
exc
):
if
exc
.
status_code
!=
404
:
return
await
default_http_exception_handler
(
request
,
exc
)
try
:
accept
=
request
.
headers
[
'accept'
]
except
Exception
:
accept
=
None
if
accept
is
not
None
and
'html'
in
accept
:
return
HTMLResponse
(
status_code
=
404
,
content
=
f
'''
<html>
<head><title>
{
config
.
meta
.
name
}
</title></head>
<body>
<h1>NOMAD app</h1>
<h2>info</h2>
{
'<br/>'
.
join
(
f
'
{
key
}
:
{
value
}
' for key, value in config.meta.items())
}
<h2>apis</h2>
<a href="
{
app_base
}
/api">NOMAD API v0</a><br/>
<a href="
{
app_base
}
/api/v1/extensions/docs">NOMAD API v1</a><br/>
<a href="
{
app_base
}
/optimade/v1/extensions/docs">Optimade API</a><br/>
<a href="
{
app_base
}
/dcat">DCAT API</a><br/>
</body>
</html>
'''
)
return
JSONResponse
(
status_code
=
404
,
content
=
{
'detail'
:
'Not found'
,
'info'
:
{
'app'
:
config
.
meta
,
'apis'
:
{
'v0'
:
{
'root'
:
f
'
{
app_base
}
/api'
,
'dashboard'
:
f
'
{
app_base
}
/api'
,
},
'v1'
:
{
'root'
:
f
'
{
app_base
}
/api/v1'
,
'dashboard'
:
f
'
{
app_base
}
/api/v1/extensions/docs'
,
'documentation'
:
f
'
{
app_base
}
/api/v1/extensions/redoc'
,
},
'optimade'
:
{
'root'
:
f
'
{
app_base
}
/optimade/v1'
,
'dashboard'
:
f
'
{
app_base
}
/optimade/v1/extensions/docs'
},
'dcat'
:
{
'root'
:
f
'
{
app_base
}
/dcat'
,
'dashboard'
:
f
'
{
app_base
}
/dcat'
}
}
}
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment