summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2024-09-25 14:02:52 -0500
committerJohn Estabrook <jestabro@vyos.io>2024-09-29 22:21:21 -0500
commit954be34bc938acc9e14d9fb3b32c8f96cd999959 (patch)
treeddc8b084bde5d5002b0547fcf7807d829f6e1d56 /src
parentfc9885f859617bab36c971f4eaa56240741f52c4 (diff)
downloadvyos-1x-954be34bc938acc9e14d9fb3b32c8f96cd999959.tar.gz
vyos-1x-954be34bc938acc9e14d9fb3b32c8f96cd999959.zip
http-api: T6736: remove routes on config delete
Avoid duplicate entries in the list of routes when adding/deleting endpoints.
Diffstat (limited to 'src')
-rw-r--r--src/services/api/graphql/routers.py10
-rw-r--r--src/services/api/rest/routers.py8
-rwxr-xr-xsrc/services/vyos-http-api-server10
3 files changed, 28 insertions, 0 deletions
diff --git a/src/services/api/graphql/routers.py b/src/services/api/graphql/routers.py
index d04375a49..f02380cdc 100644
--- a/src/services/api/graphql/routers.py
+++ b/src/services/api/graphql/routers.py
@@ -38,6 +38,10 @@ def graphql_init(app: "FastAPI"):
in_spec = state.introspection
+ # remove route and reinstall below, for any changes; alternatively, test
+ # for config_diff before proceeding
+ graphql_clear(app)
+
if state.origins:
origins = state.origins
app.add_route('/graphql', CORSMiddleware(GraphQL(schema,
@@ -52,3 +56,9 @@ def graphql_init(app: "FastAPI"):
context_value=get_user_context,
debug=True,
introspection=in_spec))
+
+
+def graphql_clear(app: "FastAPI"):
+ for r in app.routes:
+ if r.path == '/graphql':
+ app.routes.remove(r)
diff --git a/src/services/api/rest/routers.py b/src/services/api/rest/routers.py
index 1568f7d0c..38b10ef7d 100644
--- a/src/services/api/rest/routers.py
+++ b/src/services/api/rest/routers.py
@@ -682,4 +682,12 @@ def poweroff_op(data: PoweroffModel):
def rest_init(app: "FastAPI"):
+ if all(r in app.routes for r in router.routes):
+ return
app.include_router(router)
+
+
+def rest_clear(app: "FastAPI"):
+ for r in router.routes:
+ if r in app.routes:
+ app.routes.remove(r)
diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server
index 456ea3d17..6bfc2c435 100755
--- a/src/services/vyos-http-api-server
+++ b/src/services/vyos-http-api-server
@@ -159,11 +159,21 @@ def initialization(session: SessionState, app: FastAPI = app):
if session.rest:
from api.rest.routers import rest_init
rest_init(app)
+ else:
+ from api.rest.routers import rest_clear
+ rest_clear(app)
# add GraphQL route
if session.graphql:
from api.graphql.routers import graphql_init
graphql_init(app)
+ else:
+ from api.graphql.routers import graphql_clear
+ graphql_clear(app)
+
+ LOG.debug('Active routes are:')
+ for r in app.routes:
+ LOG.debug(f'{r.path}')
config = ApiServerConfig(app, uds="/run/api.sock", proxy_headers=True)
server = ApiServer(config)