Skip to content

Commit 257246a

Browse files
Merge pull request #29 from developmentseed/templatesinEndpointsFactory
add template configuration in Endpoint Factory
2 parents c375746 + 69525ed commit 257246a

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

tifeatures/factory.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
JSONResponse,
3131
SchemaJSONResponse,
3232
)
33-
from tifeatures.settings import APISettings
3433

3534
from fastapi import APIRouter, Depends, Path, Query
3635

@@ -39,20 +38,9 @@
3938
from starlette.responses import StreamingResponse
4039
from starlette.templating import Jinja2Templates, _TemplateResponse
4140

42-
settings = APISettings()
43-
44-
# custom template directory
45-
templates_location: List[Any] = (
46-
[jinja2.FileSystemLoader(settings.template_directory)]
47-
if settings.template_directory
48-
else []
49-
)
50-
# default template directory
51-
templates_location.append(jinja2.PackageLoader(__package__, "templates"))
52-
53-
templates = Jinja2Templates(
54-
directory="", # we need to set a dummy directory variable, see https://github.com/encode/starlette/issues/1214
55-
loader=jinja2.ChoiceLoader(templates_location),
41+
DEFAULT_TEMPLATES = Jinja2Templates(
42+
directory="",
43+
loader=jinja2.ChoiceLoader([jinja2.PackageLoader(__package__, "templates")]),
5644
)
5745

5846

@@ -96,6 +84,10 @@ class Endpoints:
9684
# e.g if you mount the route with `/foo` prefix, set router_prefix to foo
9785
router_prefix: str = ""
9886

87+
title: str = "TiFeatures"
88+
89+
templates: Jinja2Templates = DEFAULT_TEMPLATES
90+
9991
def __post_init__(self):
10092
"""Post Init: register route and configure specific options."""
10193
self.register_landing()
@@ -135,7 +127,7 @@ def _create_html_response(
135127
if self.router_prefix:
136128
baseurl += self.router_prefix
137129

138-
return templates.TemplateResponse(
130+
return self.templates.TemplateResponse(
139131
f"{template_name}.html",
140132
{
141133
"request": request,
@@ -173,7 +165,7 @@ def landing(
173165
):
174166
"""Get conformance."""
175167
data = model.Landing(
176-
title=settings.name,
168+
title=self.title,
177169
links=[
178170
model.Link(
179171
title="Landing Page",

tifeatures/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""tifeatures app."""
22

3+
from typing import Any, List
4+
5+
import jinja2
6+
37
from tifeatures import __version__ as tifeatures_version
48
from tifeatures.db import close_db_connection, connect_to_db, register_table_catalog
59
from tifeatures.errors import DEFAULT_STATUS_CODES, add_exception_handlers
@@ -11,6 +15,7 @@
1115
from fastapi import FastAPI
1216

1317
from starlette.middleware.cors import CORSMiddleware
18+
from starlette.templating import Jinja2Templates
1419
from starlette_cramjam.middleware import CompressionMiddleware
1520

1621
settings = APISettings()
@@ -22,8 +27,22 @@
2227
docs_url="/api.html",
2328
)
2429

30+
# custom template directory
31+
templates_location: List[Any] = (
32+
[jinja2.FileSystemLoader(settings.template_directory)]
33+
if settings.template_directory
34+
else []
35+
)
36+
# default template directory
37+
templates_location.append(jinja2.PackageLoader(__package__, "templates"))
38+
39+
templates = Jinja2Templates(
40+
directory="", # we need to set a dummy directory variable, see https://github.com/encode/starlette/issues/1214
41+
loader=jinja2.ChoiceLoader(templates_location),
42+
)
43+
2544
# Register endpoints.
26-
endpoints = Endpoints()
45+
endpoints = Endpoints(title=settings.name, templates=templates)
2746
app.include_router(endpoints.router)
2847

2948
# We add the function registry to the application state

0 commit comments

Comments
 (0)