Skip to content

Commit eb30e20

Browse files
committed
Add cli option besides env based configuration
1 parent 4518a10 commit eb30e20

File tree

7 files changed

+72
-29
lines changed

7 files changed

+72
-29
lines changed

Containerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
2626
COPY sparql_file.py ./sparql_file.py
2727
COPY README.md ./README.md
2828

29-
CMD ["uvicorn", "sparql_file:app", "--host", "", "--port", "8080"]
29+
CMD ["uvicorn", "sparql_file_env:app", "--host", "", "--port", "8080"]

Taskfile.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ tasks:
1515
serve:dev:
1616
desc: Start a development server
1717
cmds:
18-
- poetry run fastapi dev sparql_file.py
18+
- poetry run fastapi dev sparql_file_env.py
1919

2020
serve:uvicorn:
2121
desc: Run with uvicorn
2222
cmds:
23-
- poetry run uvicorn sparql_file:app --host '' --port 8080
23+
- poetry run uvicorn sparql_file_env:app --host '' --port 8080
2424

2525
serve:container:
2626
desc: Run the container with podman

poetry.lock

Lines changed: 20 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "sparql-file"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = ""
55
authors = [
66
{name = "Natanael Arndt",email = "[email protected]"}
@@ -13,6 +13,9 @@ dependencies = [
1313
"uvicorn (>=0.34.0,<0.35.0)",
1414
]
1515

16+
[project.optional-dependencies]
17+
cli = [ "typer>=0.16.1,<0.17.0" ]
18+
1619

1720
[build-system]
1821
requires = ["poetry-core>=2.0.0,<3.0.0"]
@@ -21,3 +24,6 @@ build-backend = "poetry.core.masonry.api"
2124
[tool.poetry.group.dev.dependencies]
2225
fastapi = {extras = ["standard"], version = "^0.115.8"}
2326
ruff = "^0.9.6"
27+
28+
[project.scripts]
29+
sparql-file = "sparql_file_cli:app"

sparql_file.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
from rdflib import Graph
22
from rdflib_endpoint import SparqlEndpoint
3-
from textwrap import dedent
43

5-
import os
4+
DEFAULT_EXAMPLE_QUERY = "select * { ?s ?p ?o } limit 10"
65

7-
GRAPH_FILE = os.getenv("GRAPH_FILE")
8-
EXAMPLE_QUERY = os.getenv("EXAMPLE_QUERY", dedent("""
9-
select * { ?s ?p ?o } limit 10
10-
""").strip())
116

12-
g = Graph()
7+
def sparql_file(graph_file: str, example_query: str = DEFAULT_EXAMPLE_QUERY):
8+
g = Graph()
139

14-
with open(GRAPH_FILE, "r") as graph_file_io:
15-
g.parse(source=graph_file_io)
10+
with open(graph_file, "r") as graph_file_io:
11+
g.parse(source=graph_file_io)
1612

17-
# Start the SPARQL endpoint based on the RDFLib Graph
18-
app = SparqlEndpoint(
19-
graph=g,
20-
example_query=EXAMPLE_QUERY,
21-
)
13+
# Return the SPARQL endpoint based on the RDFLib Graph
14+
return SparqlEndpoint(
15+
graph=g,
16+
example_query=example_query,
17+
)

sparql_file_cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import typer
2+
import uvicorn
3+
4+
from sparql_file import DEFAULT_EXAMPLE_QUERY, sparql_file
5+
6+
app = typer.Typer()
7+
8+
9+
@app.command()
10+
def cli(
11+
graph_file: str,
12+
host: str = "0.0.0.0",
13+
port: str = "8000",
14+
example_query: str = DEFAULT_EXAMPLE_QUERY,
15+
):
16+
"""Start the sparql file server"""
17+
endpoint = sparql_file(graph_file, example_query)
18+
uvicorn.run(endpoint, host=host, port=port)
19+
20+
21+
if __name__ == "__main__":
22+
app()

sparql_file_env.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import os
2+
3+
from sparql_file import sparql_file
4+
5+
GRAPH_FILE = os.getenv("GRAPH_FILE")
6+
EXAMPLE_QUERY = os.getenv(
7+
"EXAMPLE_QUERY",
8+
)
9+
10+
app = sparql_file(GRAPH_FILE, EXAMPLE_QUERY)

0 commit comments

Comments
 (0)