Skip to content

Commit

Permalink
add ability to enable/disable CORS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
seanbreckenridge committed Feb 15, 2021
1 parent 3e6111b commit da7fabb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ $ curl 'localhost:5050/my/zsh/history?sort=command&order_by=desc&limit=2'

To change the port this runs on, you can use the `--port` flag to change the port this runs on (default is `5050`) when running with `hpi_api server`.

By default, this allows CORS requests, you can pass the `--no-cors` flag to disable that.

If you want to customize the application further, you can import the generated Flask application to add routes, or run it with another WSGI compatible server, like [`waitress`](https://pypi.org/project/waitress/):

```python
Expand Down
10 changes: 8 additions & 2 deletions my_api/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ def main() -> None:
help="List all the generated routes",
)
@click.option("--port", default=5050, type=int, help="Port to host application on")
def server(print_routes: bool, port: int) -> None:
@click.option(
"--cors/--no-cors",
is_flag=True,
default=True,
help="Specify whether CORS should be allowed",
)
def server(print_routes: bool, port: int, cors: bool) -> None:
"""Run the HPI_API server"""
app: Flask = generate_server()
app: Flask = generate_server(cors=cors)
if print_routes:
for rule in app.url_map.iter_rules():
click.echo(str(rule))
Expand Down
6 changes: 5 additions & 1 deletion my_api/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
from .common import HPIModule, FuncTuple

from my.core.util import modules

try:
from my.core.core_config import config as coreconf

mod_active = coreconf._is_module_active
except (AttributeError, ImportError) as e:
logger.warning("Could not import/use my.core.core_config.config._is_module_active for determining active modules, assuming all modules are active")
logger.warning(
"Could not import/use my.core.core_config.config._is_module_active for determining active modules, assuming all modules are active"
)
logger.error(e)
mod_active = lambda mod: True

Expand Down
12 changes: 10 additions & 2 deletions my_api/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from typing import List, Dict

from flask import Flask
from flask import Flask, Response

from .common import FuncTuple
from .discovery import iter_modules, iter_functions
from .routes import generate_blueprint


def generate_server(app_name: str = "hpi_api") -> Flask:
def generate_server(app_name: str = "hpi_api", cors: bool = True) -> Flask:
app: Flask = Flask(app_name)
fdict: Dict[str, List[FuncTuple]] = {}
for mod in iter_modules():
fdict[mod.name] = list(iter_functions(mod))
app.register_blueprint(generate_blueprint(fdict))

if cors:

@app.after_request
def after_request(response: Response) -> Response:
response.headers["Access-Control-Allow-Origin"] = "*"
return response

return app

0 comments on commit da7fabb

Please sign in to comment.