Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,754 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
|
||
[*.py] | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
__pycache__/ | ||
.pytest_cache/ | ||
.venv/ | ||
.vscode/ | ||
*.egg-info/ | ||
build/ | ||
dist/ | ||
venv/ | ||
|
||
.env | ||
*.pyc | ||
__pycache__/ | ||
|
||
dist/ | ||
build/ | ||
*.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Datawheel, LLC. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<a href="https://github.com/Datawheel/logiclayer"><img src="https://flat.badgen.net/github/release/Datawheel/logiclayer" /></a> | ||
<a href="https://github.com/Datawheel/logiclayer/blob/master/LICENSE"><img src="https://flat.badgen.net/github/license/Datawheel/logiclayer" /></a> | ||
<a href="https://github.com/Datawheel/logiclayer/issues"><img src="https://flat.badgen.net/github/issues/Datawheel/logiclayer" /></a> | ||
|
||
|
||
> A simple framework to quickly compose and use multiple functionalities as endpoints. | ||
LogicLayer is built upon FastAPI to provide a simple way to group functionalities into reusable modules. | ||
|
||
## Usage | ||
|
||
To generate a new instance of LogicLayer, create a python file and execute this snippet: | ||
|
||
```python | ||
# example.py | ||
|
||
import requests | ||
from logiclayer import LogicLayer | ||
from logiclayer.echo import EchoModule # Example module | ||
|
||
echo = EchoModule() | ||
|
||
def is_online() -> bool: | ||
res = requests.get("http://clients3.google.com/generate_204") | ||
return (res.status_code == 204) and (res.headers.get("Content-Length") == "0") | ||
|
||
layer = LogicLayer() | ||
layer.add_check(is_online) | ||
layer.add_module(echo, prefix="/echo") | ||
``` | ||
|
||
The `layer` object is an ASGI-compatible application, that can be used with uvicorn/gunicorn to run a server, the same way as you would with a FastAPI instance. | ||
|
||
```bash | ||
$ pip install uvicorn[standard] | ||
$ uvicorn example:layer | ||
``` | ||
|
||
> Note: The `example:layer` parameter refers to `full.module.path:asgi_object`, and will change according to how you set the file. | ||
## License | ||
|
||
© 2022 Datawheel, LLC. | ||
This project is licensed under [MIT](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,40 @@ | ||
# logiclayer | ||
# LogicLayer | ||
|
||
The missing piece for all your data processing needs. | ||
> A simple framework to quickly compose and use multiple functionalities as endpoints. | ||
<a href=""><img src="https://flat.badgen.net/github/license/Datawheel/logiclayer" /></a> | ||
<a href=""><img src="https://flat.badgen.net/github/issues/Datawheel/logiclayer" /></a> | ||
<a href=""><img src="https://flat.badgen.net/pypi/v/logiclayer" /></a> | ||
|
||
## Installation | ||
|
||
### Development environment | ||
This package is available in PyPI under the name `logiclayer`. You can use `pip` or `poetry` to use it in your project: | ||
|
||
```bash | ||
pip install logiclayer | ||
``` | ||
|
||
## Development environment | ||
|
||
Create a virtual environment and install all the requirements. | ||
To manage its dependencies, this project uses `poetry`. The [pyproject.toml](pyproject.toml) file contains all the needed dependencies an devDependencies needed. To install just run: | ||
|
||
```bash | ||
$ python3 -m venv venv | ||
$ . venv/bin/activate | ||
(venv)$ pip install -r requirements.txt | ||
(venv)$ pip install -r dev-requirements.txt | ||
$ poetry install | ||
``` | ||
|
||
### Production environment | ||
### Use with VSCode + Pylance | ||
|
||
If you intend to use Visual Studio Code to work on this project, make sure poetry creates the virtual environment within the project folder, so VSCode can find the virtual environment. To use this mode run *before* the `install` command: | ||
|
||
TODO | ||
```bash | ||
$ poetry config virtualenvs.in-project true | ||
``` | ||
|
||
## Development guidelines | ||
|
||
Please read the [design docs](docs/DESIGN.md) before you start. | ||
Please read the [design docs](docs/DESIGN.md) before doing contributions. | ||
|
||
## License | ||
|
||
TODO | ||
© 2022 Datawheel, LLC. | ||
This project is licensed under [MIT](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import httpx | ||
|
||
from logiclayer import LogicLayer | ||
from logiclayer import __version__ as logiclayer_version | ||
from logiclayer_olap import OlapModule | ||
from logiclayer_geoservice import GeoserviceModule | ||
|
||
|
||
# DEFINE A CHECK | ||
def online_check(): | ||
res = httpx.get("http://clients3.google.com/generate_204") | ||
return (res.status_code == 204) and (res.headers.get("Content-Length") == 0) | ||
|
||
|
||
# DEFINE A SIMPLE ROUTE | ||
def status_route(): | ||
return {"status": "ok", "software": "LogicLayer", "version": logiclayer_version} | ||
|
||
|
||
# DEFINE A MODULE INSTANCE | ||
geoservice = GeoserviceModule(schema="./geoservice.xml", | ||
server="postgresql://user:pass@localhost:5432/mexico_geo") | ||
|
||
olap = OlapModule("https://api.oec.world/tesseract/") | ||
|
||
|
||
def run(): | ||
# CREATE A LOGICLAYER INSTANCE | ||
layer = LogicLayer() | ||
|
||
# ADD PLUGINS | ||
layer.add_check(online_check) | ||
layer.add_route("/", status_route) | ||
layer.add_module(olap, prefix="/tesseract") | ||
|
||
return layer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[loggers] | ||
keys=root | ||
|
||
[handlers] | ||
keys=fileHandler | ||
|
||
[formatters] | ||
keys=simpleFormatter | ||
|
||
[logger_root] | ||
level=DEBUG | ||
handlers=fileHandler | ||
|
||
[handler_fileHandler] | ||
class=FileHandler | ||
level=DEBUG | ||
formatter=simpleFormatter | ||
args=("logiclayer.log",) | ||
|
||
[formatter_simpleFormatter] | ||
format=%(asctime)s %(name)s - %(levelname)s:%(message)s |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Feature draft | ||
|
||
* Log all requests using `app.logger` on their applicable levels | ||
This is an instance of python's logging.Logger class, so check documentation on how to implement custom targets: | ||
* https://www.datadoghq.com/blog/python-logging-best-practices/ | ||
* https://flask.palletsprojects.com/en/1.1.x/quickstart/#logging | ||
|
||
* Implement a JWT middleware to authenticate and control permissions | ||
This would probably require tweaking the routes themselves | ||
The olap-proxy module would also have to pass the header |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
"""LogicLayer module. | ||
""" | ||
|
||
from .logiclayer import LogicLayer | ||
from .module import LogicLayerModule | ||
|
||
__all__ = ( | ||
"LogicLayer", | ||
"LogicLayerModule", | ||
) | ||
|
||
__version_info__ = ('0', '1', '0') | ||
__version__ = '.'.join(__version_info__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from fastapi.params import Query | ||
|
||
from .module import LogicLayerModule | ||
|
||
|
||
class EchoModule(LogicLayerModule): | ||
"""Echo Module for LogicLayer | ||
This is just a test module, to check basic functionality. | ||
""" | ||
|
||
data = "eyJlbmNvZGluZyI6ImJhc2U2NCJ9" | ||
|
||
def setup(self, router): | ||
|
||
@router.get("/") | ||
def route_index(message: str = Query(..., alias="msg")): | ||
return { | ||
"status": "ok", | ||
"data": self.data, | ||
"echo": message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""LogicLayer errors and exceptions module. | ||
Contains the errors and exceptions the code can raise at some point during | ||
execution. | ||
""" | ||
|
||
|
||
class BaseError(Exception): | ||
"""Base Error class for all errors in the module.""" | ||
|
||
|
||
class HealthCheckError(BaseError): | ||
"""At least one of the healthchecks set in the LogicLayer instance failed.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import logging | ||
import logging.config | ||
import os | ||
|
||
# https://www.datadoghq.com/blog/python-logging-best-practices/ | ||
config_filepath = os.environ.get("LOGICLAYER_LOGGING_CONFIG", "logging.ini") | ||
logging.config.fileConfig(config_filepath, disable_existing_loggers=False) | ||
|
||
logger = logging.getLogger("logiclayer") |
Oops, something went wrong.