1
+ from fastapi import APIRouter , BackgroundTasks
2
+ from fastapi .responses import HTMLResponse
3
+
4
+ import requests
5
+ import os
6
+
7
+ from app .constants import VIEWER_URL , RESTAPI_URL
8
+
9
+ router = APIRouter ()
10
+
11
+ def ping_viewer ():
12
+ requests .get (VIEWER_URL )
13
+
14
+ @router .get ("/" , response_class = HTMLResponse )
15
+ async def root (background_tasks : BackgroundTasks ):
16
+ # Wake up viewer service by pinging it
17
+ background_tasks .add_task (ping_viewer )
18
+
19
+ url_example = f"{ os .environ [RESTAPI_URL ]} /examples/TJoint"
20
+ url_docs = f"{ os .environ [RESTAPI_URL ]} /docs"
21
+ url_viewer = f"{ os .environ [VIEWER_URL ]} "
22
+ html_content = f"""
23
+ <html>
24
+ <head>
25
+ <title>Joint Meshing REST API</title>
26
+ <!--using same theme as Dash Flatly-->
27
+ <link href="https://cdn.jsdelivr.net/npm/[email protected] /dist/flatly/bootstrap.min.css" rel="stylesheet">
28
+ <style>
29
+ body {{
30
+ padding: 80px
31
+ }}
32
+ h1 {{
33
+ padding-bottom: 40px
34
+ }}
35
+ .padbutton {{
36
+ padding-top: 5px;
37
+ padding-bottom: 5px;
38
+ }}
39
+ .credits {{
40
+ display: block;
41
+ position: absolute;
42
+ right: 0px;
43
+ bottom: 0px;
44
+ padding: 40px;
45
+ }}
46
+ </style>
47
+ </head>
48
+ <body>
49
+ <h1 sty>Joint Meshing REST API</h1>
50
+ <div class="row">
51
+ <div class="col-lg-4">
52
+ <div class="card text-white bg-primary mb-3" style="max-width: 20rem; height: 100%;">
53
+ <div class="card-header">RestAPI</div>
54
+ <div class="card-body">
55
+ <h4 class="card-title">Create joint meshes</h4>
56
+ <p class="card-text">A REST API is available to generate joint meshes from json input.</p>
57
+ <p class="card-text">This REST API is developed using <a href='https://fastapi.tiangolo.com'>FastAPI</a>.</p>
58
+ <div class="padbutton">
59
+ <button type="button" onclick="location.href='{ url_docs } '" class="btn btn-success btn-lg">Docs</button>
60
+ </div>
61
+ <div class="padbutton">
62
+ <button type="button" onclick="location.href='{ url_example } '" class="btn btn-success btn-lg">Example response</button>
63
+ </div>
64
+ </div>
65
+ </div>
66
+ </div>
67
+ <div class="col-lg-4">
68
+ <div class="card text-white bg-primary mb-3" style="max-width: 20rem; height: 100%;">
69
+ <div class="card-header">Viewer</div>
70
+ <div class="card-body">
71
+ <h4 class="card-title">View joint meshes</h4>
72
+ <p class="card-text">You can create <b>and</b> view joint meshes using a user interface. This uses the REST API under the hood.</p>
73
+ <p class="card-text">This viewer is developed using <a href='https://dash.plotly.com/vtk'>Dash VTK</a>.</p>
74
+ <div class="padbutton">
75
+ <button type="button" onclick="location.href='{ url_viewer } '" class="btn btn-success btn-lg">Viewer</button>
76
+ </div>
77
+ </div>
78
+ </div>
79
+ </div>
80
+ </div>
81
+ <div class="credits">
82
+ <span class="badge bg-light"><i>Templates by <a href='https://bootswatch.com/flatly'>https://bootswatch.com/flatly</a> :)</span>
83
+ </div>
84
+ </body>
85
+ </html>
86
+ """
87
+ return HTMLResponse (content = html_content , status_code = 200 )
0 commit comments