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
2c6e2d5e
Commit
2c6e2d5e
authored
Mar 20, 2024
by
Timothy J. Baek
Browse files
feat: webhook backend
parent
16fe0ee1
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
60 additions
and
2 deletions
+60
-2
backend/apps/web/main.py
backend/apps/web/main.py
+2
-0
backend/apps/web/routers/auths.py
backend/apps/web/routers/auths.py
+12
-1
backend/config.py
backend/config.py
+1
-0
backend/constants.py
backend/constants.py
+7
-0
backend/main.py
backend/main.py
+27
-1
backend/utils/webhook.py
backend/utils/webhook.py
+11
-0
No files found.
backend/apps/web/main.py
View file @
2c6e2d5e
...
...
@@ -19,6 +19,7 @@ from config import (
DEFAULT_USER_ROLE
,
ENABLE_SIGNUP
,
USER_PERMISSIONS
,
WEBHOOK_URL
,
)
app
=
FastAPI
()
...
...
@@ -32,6 +33,7 @@ app.state.DEFAULT_MODELS = DEFAULT_MODELS
app
.
state
.
DEFAULT_PROMPT_SUGGESTIONS
=
DEFAULT_PROMPT_SUGGESTIONS
app
.
state
.
DEFAULT_USER_ROLE
=
DEFAULT_USER_ROLE
app
.
state
.
USER_PERMISSIONS
=
USER_PERMISSIONS
app
.
state
.
WEBHOOK_URL
=
WEBHOOK_URL
app
.
add_middleware
(
...
...
backend/apps/web/routers/auths.py
View file @
2c6e2d5e
...
...
@@ -27,7 +27,8 @@ from utils.utils import (
create_token
,
)
from
utils.misc
import
parse_duration
,
validate_email_format
from
constants
import
ERROR_MESSAGES
from
utils.webhook
import
post_webhook
from
constants
import
ERROR_MESSAGES
,
WEBHOOK_MESSAGES
router
=
APIRouter
()
...
...
@@ -155,6 +156,16 @@ async def signup(request: Request, form_data: SignupForm):
)
# response.set_cookie(key='token', value=token, httponly=True)
if
request
.
app
.
state
.
WEBHOOK_URL
:
post_webhook
(
request
.
app
.
state
.
WEBHOOK_URL
,
{
"action"
:
"signup"
,
"message"
:
WEBHOOK_MESSAGES
.
USER_SIGNUP
(
user
.
name
),
"user"
:
user
.
model_dump_json
(
exclude_none
=
True
),
},
)
return
{
"token"
:
token
,
"token_type"
:
"Bearer"
,
...
...
backend/config.py
View file @
2c6e2d5e
...
...
@@ -302,6 +302,7 @@ MODEL_FILTER_ENABLED = os.environ.get("MODEL_FILTER_ENABLED", False)
MODEL_FILTER_LIST
=
os
.
environ
.
get
(
"MODEL_FILTER_LIST"
,
""
)
MODEL_FILTER_LIST
=
[
model
.
strip
()
for
model
in
MODEL_FILTER_LIST
.
split
(
";"
)]
WEBHOOK_URL
=
os
.
environ
.
get
(
"WEBHOOK_URL"
,
""
)
####################################
# WEBUI_VERSION
...
...
backend/constants.py
View file @
2c6e2d5e
...
...
@@ -5,6 +5,13 @@ class MESSAGES(str, Enum):
DEFAULT
=
lambda
msg
=
""
:
f
"
{
msg
if
msg
else
''
}
"
class
WEBHOOK_MESSAGES
(
str
,
Enum
):
DEFAULT
=
lambda
msg
=
""
:
f
"
{
msg
if
msg
else
''
}
"
USER_SIGNUP
=
lambda
username
=
""
:
(
f
"New user signed up:
{
username
}
"
if
username
else
"New user signed up"
)
class
ERROR_MESSAGES
(
str
,
Enum
):
def
__str__
(
self
)
->
str
:
return
super
().
__str__
()
...
...
backend/main.py
View file @
2c6e2d5e
...
...
@@ -38,6 +38,7 @@ from config import (
FRONTEND_BUILD_DIR
,
MODEL_FILTER_ENABLED
,
MODEL_FILTER_LIST
,
WEBHOOK_URL
,
)
from
constants
import
ERROR_MESSAGES
...
...
@@ -58,6 +59,9 @@ app = FastAPI(docs_url="/docs" if ENV == "dev" else None, redoc_url=None)
app
.
state
.
MODEL_FILTER_ENABLED
=
MODEL_FILTER_ENABLED
app
.
state
.
MODEL_FILTER_LIST
=
MODEL_FILTER_LIST
app
.
state
.
WEBHOOK_URL
=
WEBHOOK_URL
origins
=
[
"*"
]
...
...
@@ -178,7 +182,7 @@ class ModelFilterConfigForm(BaseModel):
@
app
.
post
(
"/api/config/model/filter"
)
async
def
get
_model_filter_config
(
async
def
update
_model_filter_config
(
form_data
:
ModelFilterConfigForm
,
user
=
Depends
(
get_admin_user
)
):
...
...
@@ -197,6 +201,28 @@ async def get_model_filter_config(
}
@
app
.
get
(
"/api/webhook"
)
async
def
get_webhook_url
(
user
=
Depends
(
get_admin_user
)):
return
{
"url"
:
app
.
state
.
WEBHOOK_URL
,
}
class
UrlForm
(
BaseModel
):
url
:
str
@
app
.
post
(
"/api/webhook"
)
async
def
update_webhook_url
(
form_data
:
UrlForm
,
user
=
Depends
(
get_admin_user
)):
app
.
state
.
WEBHOOK_URL
=
form_data
.
url
webui_app
.
state
.
WEBHOOK_URL
=
app
.
state
.
WEBHOOK_URL
return
{
"url"
:
app
.
state
.
WEBHOOK_URL
,
}
@
app
.
get
(
"/api/version"
)
async
def
get_app_config
():
...
...
backend/utils/webhook.py
0 → 100644
View file @
2c6e2d5e
import
requests
def
post_webhook
(
url
:
str
,
json
:
dict
)
->
bool
:
try
:
r
=
requests
.
post
(
url
,
json
=
json
)
r
.
raise_for_status
()
return
True
except
Exception
as
e
:
print
(
e
)
return
False
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