diff options
author | John Estabrook <jestabro@vyos.io> | 2021-12-16 08:32:23 -0600 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2021-12-16 12:12:54 -0600 |
commit | e671c4b33157836e73a3efc91e691215bc5b144b (patch) | |
tree | 974fbd759e67a1dafac2785f7466880a1b1bfe80 /src/services | |
parent | 98396247fcba8b7793e62c483288fdda34c77f3c (diff) | |
download | vyos-1x-e671c4b33157836e73a3efc91e691215bc5b144b.tar.gz vyos-1x-e671c4b33157836e73a3efc91e691215bc5b144b.zip |
http-api: T4076: allow setting CORS option 'Access-Control-Allow-Origin'
(cherry picked from commit 55f8ede2d09a9ad095f9ec5c2a729f8c5fb6aafa)
Diffstat (limited to 'src/services')
-rwxr-xr-x | src/services/vyos-http-api-server | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server index f79058683..06871f1d6 100755 --- a/src/services/vyos-http-api-server +++ b/src/services/vyos-http-api-server @@ -32,6 +32,7 @@ from fastapi.responses import HTMLResponse from fastapi.exceptions import RequestValidationError from fastapi.routing import APIRoute from pydantic import BaseModel, StrictStr, validator +from starlette.middleware.cors import CORSMiddleware from starlette.datastructures import FormData from starlette.formparsers import FormParser, MultiPartParser from multipart.multipart import parse_options_header @@ -610,13 +611,19 @@ def show_op(data: ShowModel): # GraphQL integration ### -from api.graphql.bindings import generate_schema +def graphql_init(fast_api_app): + from api.graphql.bindings import generate_schema -api.graphql.state.init() + api.graphql.state.init() + api.graphql.state.settings['app'] = app -schema = generate_schema() + schema = generate_schema() -app.add_route('/graphql', GraphQL(schema, debug=True)) + if app.state.vyos_origins: + origins = app.state.vyos_origins + app.add_route('/graphql', CORSMiddleware(GraphQL(schema, debug=True), allow_origins=origins, allow_methods=("GET", "POST", "OPTIONS"))) + else: + app.add_route('/graphql', GraphQL(schema, debug=True)) ### @@ -642,8 +649,9 @@ if __name__ == '__main__': app.state.vyos_debug = server_config['debug'] app.state.vyos_strict = server_config['strict'] + app.state.vyos_origins = server_config.get('cors', {}).get('origins', []) - api.graphql.state.settings['app'] = app + graphql_init(app) try: if not server_config['socket']: |