-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(docker): add API endpoint with docker
- Loading branch information
Showing
6 changed files
with
110 additions
and
0 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,2 @@ | ||
Dockerfile | ||
README.md |
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,6 @@ | ||
FROM kitware/trame:py3.9 | ||
|
||
COPY --chown=trame-user:trame-user . /deploy | ||
|
||
ENV TRAME_CLIENT_TYPE=vue3 | ||
RUN /opt/trame/entrypoint.sh build |
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 @@ | ||
# Trame app with custom endpoint | ||
|
||
This allow you to register special HTTP endpoint for your app. | ||
While it works by default locally, when using docker bundling, you need to add special handling. | ||
|
||
## Run locally without docker | ||
|
||
```bash | ||
python ./app.py | ||
``` | ||
|
||
## Build the image | ||
|
||
```bash | ||
docker build -t trame-app-api . | ||
``` | ||
|
||
## Run the image on port 8080 | ||
|
||
```bash | ||
docker run -it --rm -p 8080:80 trame-app-api | ||
``` |
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,65 @@ | ||
from trame.app import get_server | ||
from trame.ui.vuetify3 import SinglePageLayout | ||
from trame.widgets import vuetify3 as v3, html | ||
from trame.decorators import TrameApp, controller | ||
|
||
import time | ||
from pathlib import Path | ||
from aiohttp import web | ||
|
||
|
||
@TrameApp() | ||
class App: | ||
def __init__(self, server=None): | ||
self.server = get_server(server, client_type="vue3") | ||
self.server.cli.add_argument("--session") | ||
self._build_ui() | ||
|
||
# Use CLI to know when used inside docker | ||
args, _ = self.server.cli.parse_known_args() | ||
session = args.session | ||
|
||
# simple default state | ||
self.state.response = {} | ||
self.state.session = session | ||
self.state.url = "/trame-endpoint" | ||
|
||
# Need to adjust URL when within docker | ||
if session: | ||
self.state.url = f"/api/{session}/trame-endpoint" | ||
|
||
@property | ||
def state(self): | ||
return self.server.state | ||
|
||
@controller.add("on_server_bind") | ||
def _bind_routes(self, wslink_server): | ||
wslink_server.app.add_routes( | ||
[web.get("/trame-endpoint", self.on_trame_endpoint)] | ||
) | ||
|
||
def on_trame_endpoint(self, request): | ||
return web.json_response({"url": self.state.url, "time": time.time()}) | ||
|
||
def _build_ui(self): | ||
with SinglePageLayout(self.server) as layout: | ||
with layout.toolbar: | ||
v3.VSpacer() | ||
v3.VBtn( | ||
"Make request", | ||
click="utils.get('fetch')(url).then(v => v.json()).then(v => (response = v))", | ||
) | ||
with layout.content: | ||
html.Div("URL: {{ url }}") | ||
html.Div("Session: {{ session }}") | ||
html.Div("Response") | ||
html.Pre("{{ JSON.stringify(response, null, 2) }}") | ||
|
||
|
||
def main(): | ||
app = App() | ||
app.server.start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 @@ | ||
trame: | ||
cmd: | ||
- python | ||
- /deploy/app.py | ||
- --host | ||
- ${host} | ||
- --port | ||
- ${port} | ||
- --authKey | ||
- ${secret} | ||
- --server | ||
- --session | ||
- ${id} |
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,2 @@ | ||
trame | ||
trame-vuetify |