Skip to content

Commit

Permalink
docs(docker): add API endpoint with docker
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jun 20, 2024
1 parent 8be9873 commit 089e84a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/09_advanced/custom_endpoint/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dockerfile
README.md
6 changes: 6 additions & 0 deletions examples/09_advanced/custom_endpoint/Dockerfile
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
22 changes: 22 additions & 0 deletions examples/09_advanced/custom_endpoint/README.md
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
```
65 changes: 65 additions & 0 deletions examples/09_advanced/custom_endpoint/app.py
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()
13 changes: 13 additions & 0 deletions examples/09_advanced/custom_endpoint/setup/apps.yml
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}
2 changes: 2 additions & 0 deletions examples/09_advanced/custom_endpoint/setup/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trame
trame-vuetify

0 comments on commit 089e84a

Please sign in to comment.