Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
e231333b
Commit
e231333b
authored
May 28, 2024
by
Timothy J. Baek
Browse files
refac
parent
b870fd11
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
13 deletions
+25
-13
backend/main.py
backend/main.py
+25
-13
No files found.
backend/main.py
View file @
e231333b
...
@@ -12,6 +12,7 @@ import mimetypes
...
@@ -12,6 +12,7 @@ import mimetypes
from
fastapi
import
FastAPI
,
Request
,
Depends
,
status
from
fastapi
import
FastAPI
,
Request
,
Depends
,
status
from
fastapi.staticfiles
import
StaticFiles
from
fastapi.staticfiles
import
StaticFiles
from
fastapi.responses
import
JSONResponse
from
fastapi
import
HTTPException
from
fastapi
import
HTTPException
from
fastapi.middleware.wsgi
import
WSGIMiddleware
from
fastapi.middleware.wsgi
import
WSGIMiddleware
from
fastapi.middleware.cors
import
CORSMiddleware
from
fastapi.middleware.cors
import
CORSMiddleware
...
@@ -123,15 +124,6 @@ app.state.MODELS = {}
...
@@ -123,15 +124,6 @@ app.state.MODELS = {}
origins
=
[
"*"
]
origins
=
[
"*"
]
app
.
add_middleware
(
CORSMiddleware
,
allow_origins
=
origins
,
allow_credentials
=
True
,
allow_methods
=
[
"*"
],
allow_headers
=
[
"*"
],
)
# Custom middleware to add security headers
# Custom middleware to add security headers
# class SecurityHeadersMiddleware(BaseHTTPMiddleware):
# class SecurityHeadersMiddleware(BaseHTTPMiddleware):
# async def dispatch(self, request: Request, call_next):
# async def dispatch(self, request: Request, call_next):
...
@@ -276,10 +268,8 @@ class PipelineMiddleware(BaseHTTPMiddleware):
...
@@ -276,10 +268,8 @@ class PipelineMiddleware(BaseHTTPMiddleware):
except
:
except
:
pass
pass
print
(
sorted_filters
)
for
filter
in
sorted_filters
:
for
filter
in
sorted_filters
:
r
=
None
try
:
try
:
urlIdx
=
filter
[
"urlIdx"
]
urlIdx
=
filter
[
"urlIdx"
]
...
@@ -303,7 +293,20 @@ class PipelineMiddleware(BaseHTTPMiddleware):
...
@@ -303,7 +293,20 @@ class PipelineMiddleware(BaseHTTPMiddleware):
except
Exception
as
e
:
except
Exception
as
e
:
# Handle connection error here
# Handle connection error here
print
(
f
"Connection error:
{
e
}
"
)
print
(
f
"Connection error:
{
e
}
"
)
pass
if
r
is
not
None
:
try
:
res
=
r
.
json
()
if
"detail"
in
res
:
return
JSONResponse
(
status_code
=
r
.
status_code
,
content
=
res
,
)
except
:
pass
else
:
pass
modified_body_bytes
=
json
.
dumps
(
data
).
encode
(
"utf-8"
)
modified_body_bytes
=
json
.
dumps
(
data
).
encode
(
"utf-8"
)
# Replace the request body with the modified one
# Replace the request body with the modified one
...
@@ -328,6 +331,15 @@ class PipelineMiddleware(BaseHTTPMiddleware):
...
@@ -328,6 +331,15 @@ class PipelineMiddleware(BaseHTTPMiddleware):
app
.
add_middleware
(
PipelineMiddleware
)
app
.
add_middleware
(
PipelineMiddleware
)
app
.
add_middleware
(
CORSMiddleware
,
allow_origins
=
origins
,
allow_credentials
=
True
,
allow_methods
=
[
"*"
],
allow_headers
=
[
"*"
],
)
@
app
.
middleware
(
"http"
)
@
app
.
middleware
(
"http"
)
async
def
check_url
(
request
:
Request
,
call_next
):
async
def
check_url
(
request
:
Request
,
call_next
):
if
len
(
app
.
state
.
MODELS
)
==
0
:
if
len
(
app
.
state
.
MODELS
)
==
0
:
...
...
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