Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Memri
pymemri
Commits
8697a4f0
Commit
8697a4f0
authored
2 years ago
by
Eelco van der Wel
Browse files
Options
Download
Plain Diff
Merge branch 'eelco/serve-qr-string' into 'dev'
Serve QR as json string and json svg See merge request
!232
parents
ab10073a
a195264e
Pipeline
#9989
passed with stages
in 1 minute and 27 seconds
Changes
2
Pipelines
8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pymemri/webserver/qr_endpoints.py
+37
-2
pymemri/webserver/qr_endpoints.py
pymemri/webserver/webserver.py
+21
-3
pymemri/webserver/webserver.py
with
58 additions
and
5 deletions
+58
-5
pymemri/webserver/qr_endpoints.py
+
37
-
2
View file @
8697a4f0
import
os
from
.helpers
import
get_root_path
,
DictProxy
from
fastapi
import
APIRouter
,
Request
from
fastapi.responses
import
HTMLResponse
,
JSONResponse
from
fastapi.templating
import
Jinja2Templates
from
fastapi.responses
import
HTMLResponse
from
.helpers
import
DictProxy
,
get_root_path
qr_code_dict
=
DictProxy
()
...
...
@@ -20,6 +22,7 @@ templates = Jinja2Templates(directory=os.path.join(get_root_path(__name__), "tem
router
=
APIRouter
()
QR_CODE_KEY
=
"qr_code"
QR_STRING_KEY
=
"qr_string"
AUTHENTICATED
=
"authenticated"
...
...
@@ -37,3 +40,35 @@ def qr(request: Request):
return
templates
.
TemplateResponse
(
"success.html"
,
{
"request"
:
request
})
else
:
return
templates
.
TemplateResponse
(
"images.html"
,
{
"request"
:
request
,
"chart_output"
:
qr_code_data
})
@
router
.
get
(
"/qr_svg"
,
response_class
=
JSONResponse
)
def
qr_svg
():
"""Returns the QR svg as json"""
global
qr_code_dict
content
=
{
"qr"
:
qr_code_dict
.
get
(
QR_CODE_KEY
,
None
),
"authenticated"
:
qr_code_dict
.
get
(
AUTHENTICATED
,
False
),
}
return
JSONResponse
(
content
=
content
,
headers
=
{
'Access-Control-Allow-Origin'
:
'*'
}
)
@
router
.
get
(
"/qr_string"
,
response_class
=
JSONResponse
)
def
qr_string
():
"""Returns the QR svg as json"""
global
qr_code_dict
content
=
{
"qr"
:
qr_code_dict
.
get
(
QR_STRING_KEY
,
None
).
decode
(
"utf-8"
),
"authenticated"
:
qr_code_dict
.
get
(
AUTHENTICATED
,
False
),
}
return
JSONResponse
(
content
=
content
,
headers
=
{
'Access-Control-Allow-Origin'
:
'*'
}
)
This diff is collapsed.
Click to expand it.
pymemri/webserver/webserver.py
+
21
-
3
View file @
8697a4f0
import
threading
from
uvicorn
import
Server
,
Config
from
fastapi
import
FastAPI
from
starlette.middleware.cors
import
CORSMiddleware
from
uvicorn
import
Config
,
Server
class
WebServer
:
def
__init__
(
self
,
port
:
int
):
self
.
_app
=
FastAPI
(
title
=
"Plugin Webserver"
,
redoc_url
=
None
,
swagger_ui_oauth2_redirect_url
=
None
)
self
.
_app
=
self
.
_setup_app
()
self
.
_server_handle
=
None
self
.
_uvicorn
=
None
self
.
_port
=
port
def
_setup_app
(
self
)
->
FastAPI
:
app
=
FastAPI
(
title
=
"Plugin Webserver"
,
redoc_url
=
None
,
swagger_ui_oauth2_redirect_url
=
None
)
# TODO setup allow_origin_regex for *.memri.io and localhost
app
.
add_middleware
(
CORSMiddleware
,
allow_origins
=
[
"*"
],
allow_methods
=
[
"*"
],
allow_headers
=
[
"*"
],
)
return
app
@
property
def
app
(
self
)
->
FastAPI
:
return
self
.
_app
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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
Menu
Explore
Projects
Groups
Snippets