-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from thegreenwebfoundation/ca-add-webserver
Add web server for serving validator over an API
- Loading branch information
Showing
24 changed files
with
683 additions
and
22 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,5 @@ | ||
# SECURITY WARNING: don't run with the debug turned on in production! | ||
DEBUG=True | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY=secret |
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,43 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
paths-ignore: | ||
- "**.md" | ||
- ".gitignore" | ||
# make our tests run when we have external PRs | ||
pull_request: | ||
paths-ignore: | ||
- "**.md" | ||
- ".gitignore" | ||
|
||
defaults: | ||
run: | ||
working-directory: ./ | ||
|
||
jobs: | ||
run_tests: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
python-version: [3.11] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install tooling for managing dependencies | ||
run: | | ||
python -m pip install --upgrade uv wheel | ||
- name: Run tests | ||
run: | | ||
uv run pytest | ||
env: | ||
SECRET_KEY: "test" |
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,31 +1,49 @@ | ||
# Carbon.txt validator | ||
|
||
This validator reads carbon.txt files, and validates them against a spec defined on http://carbontxt.org. | ||
|
||
|
||
|
||
This validator reads carbon.txt files, and validates them against a spec defined | ||
on http://carbontxt.org. | ||
|
||
# Usage | ||
|
||
## With the CLI | ||
|
||
Run a validation against a given domain, or file, say if the file is valid TOML, and it confirms to the carbon.txt spec | ||
Run a validation against a given domain, or file, say if the file is valid TOML, | ||
and it confirms to the carbon.txt spec. | ||
|
||
The following commands assume you are working in a virtual environment: | ||
|
||
```shell | ||
# parse the carbon.txt file on default paths on some-domain.com | ||
carbontxt validate domain some-domain.com | ||
carbon-txt validate domain some-domain.com | ||
|
||
# parse a remote file available at https://somedomain.com/path-to-carbon.txt | ||
carbontxt validate file https://somedomain.com/path-to-carbon.txt | ||
carbon-txt validate file https://somedomain.com/path-to-carbon.txt | ||
|
||
# parse a local file ./path-to-file.com | ||
carbontxt validate file ./path-to-file.com | ||
carbon-txt validate file ./path-to-file.com | ||
|
||
# pipe the contents of a file into the file validation command as part of a pipeline | ||
cat ./path-to-file.com | carbontxt validate file | ||
|
||
``` | ||
|
||
### Using UV | ||
|
||
If you are not using a virtual environments, but running `uv`, in a project you | ||
can run it with `uv run carbon-txt your args` | ||
|
||
## With the HTTP API | ||
|
||
(Coming up next) | ||
You can also validate carbon.txt files sent over an HTTP API. | ||
|
||
```shell | ||
# run the carbon-txt validator as a server using the default django server. Not for production | ||
carbon-txt serve | ||
``` | ||
|
||
For production, [Granian](https://github.com/emmett-framework/granian), a | ||
performant webserver is bundled. Pass the flag `--server granian` to use it. | ||
|
||
```shell | ||
# run the carbon-txt validator as a server using the production granian server | ||
carbon-txt serve --server granian | ||
``` |
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,19 @@ | ||
|
||
|
||
# load the contents of .env into environment variables | ||
# override by calling `just --dotenv-filename ENV_FILENAME COMMAND` | ||
# where ENV_FILENAME is the file containing env vars you want to use instead | ||
set dotenv-load | ||
|
||
default: | ||
just --list | ||
|
||
test *options: | ||
uv run pytest {{ options }} | ||
|
||
test-watch *options: | ||
uv run pytest-watch -- {{ options }} | ||
|
||
|
||
serve: | ||
uv run python ./src/carbon_txt_validator/web/manage.py runserver |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# pytest.ini | ||
[pytest] | ||
pythonpath = src | ||
addopts = --ds="carbon_txt_validator.web.config.settings.test" | ||
|
||
; list the warnings to ignore | ||
filterwarnings = | ||
ignore::django.utils.deprecation.RemovedInDjango60Warning | ||
ignore::pydantic.warnings.PydanticDeprecatedSince20 |
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
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
Empty file.
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,57 @@ | ||
from ninja import NinjaAPI, Schema | ||
from django.http import HttpRequest, HttpResponse | ||
|
||
from carbon_txt_validator.parsers_toml import CarbonTxtParser | ||
from carbon_txt_validator.schemas import CarbonTxtFile | ||
|
||
# Initialize the NinjaAPI with OpenAPI documentation details | ||
ninja_api = NinjaAPI( | ||
openapi_extra={ | ||
"info": { | ||
"termsOfService": "https://thegreenwebfoundation.org/terms/", | ||
} | ||
}, | ||
title="Carbon.txt Validator API", | ||
description="API for validating carbon.txt files.", | ||
) | ||
|
||
|
||
class CarbonTextSubmission(Schema): | ||
""" | ||
Schema for the submission of carbon.txt file contents. We use | ||
""" | ||
|
||
text_contents: str | ||
|
||
|
||
@ninja_api.post( | ||
"/validate/", description="Accept contents of a carbon.txt file and validate it." | ||
) | ||
def validate_contents( | ||
request: HttpRequest, CarbonTextSubmission: CarbonTextSubmission | ||
) -> HttpResponse: | ||
""" | ||
Endpoint to validate the contents of a carbon.txt file. | ||
Args: | ||
request: The request object. | ||
CarbonTextSubmission: The schema containing the text contents of the carbon.txt file. | ||
Returns: | ||
dict: A dictionary containing the success status and either the validated data or errors. | ||
""" | ||
# Initialize the parser | ||
parser = CarbonTxtParser() | ||
|
||
# Parse the TOML contents from the submission | ||
parsed = parser.parse_toml(CarbonTextSubmission.text_contents) | ||
|
||
# Validate the parsed contents as a carbon.txt file | ||
result = parser.validate_as_carbon_txt(parsed) | ||
|
||
# Check if the result is a valid CarbonTxtFile instance | ||
if isinstance(result, CarbonTxtFile): | ||
return {"success": True, "data": result} | ||
|
||
# Return errors if validation failed | ||
return {"success": False, "errors": result.errors()} |
Oops, something went wrong.