generated from ensaremirerol/nextjs-fastapi-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (107 loc) · 3.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import random
import socket
import threading
# WORKAROUND: Ensures Nuitka includes the necessary plugins
import rdflib.plugins.parsers.hext # noqa: F401
import rdflib.plugins.parsers.jsonld # noqa: F401
import rdflib.plugins.parsers.notation3 # noqa: F401
import rdflib.plugins.parsers.nquads # noqa: F401
import rdflib.plugins.parsers.ntriples # noqa: F401
import rdflib.plugins.parsers.patch # noqa: F401
import rdflib.plugins.parsers.RDFVOC # noqa: F401
import rdflib.plugins.parsers.rdfxml # noqa: F401
import rdflib.plugins.parsers.trig # noqa: F401
import rdflib.plugins.parsers.trix # noqa: F401
import rdflib.plugins.serializers.hext # noqa: F401
import rdflib.plugins.serializers.jsonld # noqa: F401
import rdflib.plugins.serializers.longturtle # noqa: F401
import rdflib.plugins.serializers.n3 # noqa: F401
import rdflib.plugins.serializers.nquads # noqa: F401
import rdflib.plugins.serializers.nt # noqa: F401
import rdflib.plugins.serializers.patch # noqa: F401
import rdflib.plugins.serializers.rdfxml # noqa: F401
import rdflib.plugins.serializers.trig # noqa: F401
import rdflib.plugins.serializers.trix # noqa: F401
import rdflib.plugins.serializers.turtle # noqa: F401
import rdflib.plugins.serializers.xmlwriter # noqa: F401
import rdflib.plugins.sparql.aggregates # noqa: F401
import rdflib.plugins.sparql.algebra # noqa: F401
import rdflib.plugins.sparql.datatypes # noqa: F401
import rdflib.plugins.sparql.evaluate # noqa: F401
import rdflib.plugins.sparql.evalutils # noqa: F401
import rdflib.plugins.sparql.operators # noqa: F401
import rdflib.plugins.sparql.parser # noqa: F401
import rdflib.plugins.sparql.parserutils # noqa: F401
import rdflib.plugins.sparql.processor # noqa: F401
import rdflib.plugins.sparql.sparql # noqa: F401
import rdflib.plugins.sparql.update # noqa: F401
import rdflib.plugins.stores.memory # noqa: F401
# WORKAROUND: Ensures Nuitka includes the necessary plugins
import webview
from dotenv import load_dotenv
from kink.container import di
from pydantic import parse_obj_as
from server.logger import setup_logging
from server.server import app
load_dotenv()
DEBUG = os.getenv("DEBUG", False)
os.environ["DD_TRACE_ENABLED"] = os.getenv(
"DD_TRACE_ENABLED", "false"
)
thread = None
LOG_JSON_FORMAT = parse_obj_as(
bool, os.getenv("LOG_JSON_FORMAT", False)
)
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
setup_logging(
json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL
)
def get_free_port():
while True:
port = random.randint(
1024, 65535
) # Ports below 1024 are reserved
with socket.socket(
socket.AF_INET, socket.SOCK_STREAM
) as s:
try:
s.bind(
("", port)
) # Bind to the port on all network interfaces
s.listen(
1
) # Start listening to check if the port is available
return port # If binding succeeds, the port is free
except OSError:
continue # If binding fails, try another port
port = 8000 if DEBUG else get_free_port()
di["PORT"] = port
def start_fastapi():
import uvicorn
uvicorn.run(
app, host="0.0.0.0", port=port, log_config=None
)
def on_closing():
global thread
if thread:
thread.join(timeout=0)
os._exit(0)
if __name__ == "__main__":
if DEBUG:
start_fastapi()
else:
# Run server in separate thread
thread = threading.Thread(target=start_fastapi)
thread.start()
# Create window
window = webview.create_window(
"RDFCraft",
f"http://localhost:{port}",
width=800,
height=600,
resizable=True,
)
window.events.closing += on_closing
webview._settings["debug"] = True
webview.start()