mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-08-25 06:52:23 +00:00
Rename FastAPI server to EOS. (#355)
Rename FastAPI server to `eos` and FastHTML server to `eosdash`. Make an user easily identify what server is meant. FastAPI and FastHTML are implementation details that may confuse the non-technical user. Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
@@ -289,7 +289,7 @@ class ConfigEOS(SingletonMixin, SettingsEOS):
|
||||
|
||||
Example:
|
||||
>>> config = get_config()
|
||||
>>> new_data = {"prediction_hours": 24, "server_fastapi_port": 8000}
|
||||
>>> new_data = {"prediction_hours": 24, "server_eos_port": 8000}
|
||||
>>> config.merge_settings_from_dict(new_data)
|
||||
"""
|
||||
# Create new settings instance with reset optional fields and merged data
|
||||
|
@@ -103,11 +103,11 @@
|
||||
"pvforecast4_userhorizon": null,
|
||||
"pvforecast_provider": null,
|
||||
"pvforecastimport_file_path": null,
|
||||
"server_fastapi_startup_server_fasthtml": true,
|
||||
"server_fastapi_host": "0.0.0.0",
|
||||
"server_fastapi_port": 8503,
|
||||
"server_fasthtml_host": "0.0.0.0",
|
||||
"server_fasthtml_port": 8504,
|
||||
"server_eos_startup_eosdash": true,
|
||||
"server_eos_host": "0.0.0.0",
|
||||
"server_eos_port": 8503,
|
||||
"server_eosdash_host": "0.0.0.0",
|
||||
"server_eosdash_port": 8504,
|
||||
"weather_provider": null,
|
||||
"weatherimport_file_path": null
|
||||
}
|
||||
|
@@ -129,10 +129,10 @@ def create_error_page(
|
||||
)
|
||||
|
||||
|
||||
def start_fasthtml_server() -> subprocess.Popen:
|
||||
def start_eosdash() -> subprocess.Popen:
|
||||
"""Start the fasthtml server as a subprocess."""
|
||||
server_process = subprocess.Popen(
|
||||
[sys.executable, str(server_dir.joinpath("fasthtml_server.py"))],
|
||||
[sys.executable, str(server_dir.joinpath("eosdash.py"))],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
@@ -144,14 +144,14 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
"""Lifespan manager for the app."""
|
||||
# On startup
|
||||
if (
|
||||
config_eos.server_fastapi_startup_server_fasthtml
|
||||
and config_eos.server_fasthtml_host
|
||||
and config_eos.server_fasthtml_port
|
||||
config_eos.server_eos_startup_eosdash
|
||||
and config_eos.server_eosdash_host
|
||||
and config_eos.server_eosdash_port
|
||||
):
|
||||
try:
|
||||
fasthtml_process = start_fasthtml_server()
|
||||
fasthtml_process = start_eosdash()
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start FastHTML server. Error: {e}")
|
||||
logger.error(f"Failed to start EOSdash server. Error: {e}")
|
||||
sys.exit(1)
|
||||
# Handover to application
|
||||
yield
|
||||
@@ -172,7 +172,7 @@ app = FastAPI(
|
||||
)
|
||||
|
||||
# That's the problem
|
||||
opt_class = optimization_problem(verbose=bool(config_eos.server_fastapi_verbose))
|
||||
opt_class = optimization_problem(verbose=bool(config_eos.server_eos_verbose))
|
||||
|
||||
server_dir = Path(__file__).parent.resolve()
|
||||
|
||||
@@ -812,9 +812,9 @@ async def proxy_put(request: Request, path: str) -> Response:
|
||||
|
||||
|
||||
async def proxy(request: Request, path: str) -> Union[Response | RedirectResponse | HTMLResponse]:
|
||||
if config_eos.server_fasthtml_host and config_eos.server_fasthtml_port:
|
||||
if config_eos.server_eosdash_host and config_eos.server_eosdash_port:
|
||||
# Proxy to fasthtml server
|
||||
url = f"http://{config_eos.server_fasthtml_host}:{config_eos.server_fasthtml_port}/{path}"
|
||||
url = f"http://{config_eos.server_eosdash_host}:{config_eos.server_eosdash_port}/{path}"
|
||||
headers = dict(request.headers)
|
||||
|
||||
data = await request.body()
|
||||
@@ -834,11 +834,11 @@ async def proxy(request: Request, path: str) -> Union[Response | RedirectRespons
|
||||
status_code="404",
|
||||
error_title="Page Not Found",
|
||||
error_message=f"""<pre>
|
||||
Application server not reachable: '{url}'
|
||||
Did you start the application server
|
||||
or set 'server_fastapi_startup_server_fasthtml'?
|
||||
EOSdash server not reachable: '{url}'
|
||||
Did you start the EOSdash server
|
||||
or set 'server_eos_startup_eosdash'?
|
||||
If there is no application server intended please
|
||||
set 'server_fasthtml_host' or 'server_fasthtml_port' to None.
|
||||
set 'server_eosdash_host' or 'server_eosdash_port' to None.
|
||||
</pre>
|
||||
""",
|
||||
error_details=f"{e}",
|
||||
@@ -855,22 +855,22 @@ set 'server_fasthtml_host' or 'server_fasthtml_port' to None.
|
||||
return RedirectResponse(url="/docs")
|
||||
|
||||
|
||||
def start_fastapi_server() -> None:
|
||||
"""Start FastAPI server."""
|
||||
def start_eos() -> None:
|
||||
"""Start EOS server."""
|
||||
try:
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=str(config_eos.server_fastapi_host),
|
||||
port=config_eos.server_fastapi_port,
|
||||
host=str(config_eos.server_eos_host),
|
||||
port=config_eos.server_eos_port,
|
||||
log_level="debug",
|
||||
access_log=True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Could not bind to host {config_eos.server_fastapi_host}:{config_eos.server_fastapi_port}. Error: {e}"
|
||||
f"Could not bind to host {config_eos.server_eos_host}:{config_eos.server_eos_port}. Error: {e}"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
start_fastapi_server()
|
||||
start_eos()
|
@@ -41,20 +41,18 @@ def config_table() -> Table:
|
||||
|
||||
@rt("/")
|
||||
def get(): # type: ignore
|
||||
return Titled("EOS Config App", H1("Configuration"), config_table())
|
||||
return Titled("EOS Dashboard", H1("Configuration"), config_table())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
logger.info(
|
||||
f"Starting {config_eos.server_fasthtml_host}:{config_eos.server_fasthtml_port}."
|
||||
)
|
||||
logger.info(f"Starting {config_eos.server_eosdash_host}:{config_eos.server_eosdash_port}.")
|
||||
uvicorn.run(
|
||||
app, host=str(config_eos.server_fasthtml_host), port=config_eos.server_fasthtml_port
|
||||
app, host=str(config_eos.server_eosdash_host), port=config_eos.server_eosdash_port
|
||||
)
|
||||
except Exception as e:
|
||||
# Error handling for binding issues
|
||||
logger.error(
|
||||
f"Could not bind to host {config_eos.server_fasthtml_host}:{config_eos.server_fasthtml_port}. Error: {e}"
|
||||
f"Could not bind to host {config_eos.server_eosdash_host}:{config_eos.server_eosdash_port}. Error: {e}"
|
||||
)
|
||||
exit(1)
|
@@ -17,24 +17,22 @@ class ServerCommonSettings(SettingsBaseModel):
|
||||
To be added
|
||||
"""
|
||||
|
||||
server_fastapi_host: Optional[IPvAnyAddress] = Field(
|
||||
default="0.0.0.0", description="FastAPI server IP address."
|
||||
server_eos_host: Optional[IPvAnyAddress] = Field(
|
||||
default="0.0.0.0", description="EOS server IP address."
|
||||
)
|
||||
server_fastapi_port: Optional[int] = Field(
|
||||
default=8503, description="FastAPI server IP port number."
|
||||
server_eos_port: Optional[int] = Field(default=8503, description="EOS server IP port number.")
|
||||
server_eos_verbose: Optional[bool] = Field(default=False, description="Enable debug output")
|
||||
server_eos_startup_eosdash: Optional[bool] = Field(
|
||||
default=True, description="EOS server to start EOSdash server."
|
||||
)
|
||||
server_fastapi_verbose: Optional[bool] = Field(default=False, description="Enable debug output")
|
||||
server_fastapi_startup_server_fasthtml: Optional[bool] = Field(
|
||||
default=True, description="FastAPI server to startup application FastHTML server."
|
||||
server_eosdash_host: Optional[IPvAnyAddress] = Field(
|
||||
default="0.0.0.0", description="EOSdash server IP address."
|
||||
)
|
||||
server_fasthtml_host: Optional[IPvAnyAddress] = Field(
|
||||
default="0.0.0.0", description="FastHTML server IP address."
|
||||
)
|
||||
server_fasthtml_port: Optional[int] = Field(
|
||||
default=8504, description="FastHTML server IP port number."
|
||||
server_eosdash_port: Optional[int] = Field(
|
||||
default=8504, description="EOSdash server IP port number."
|
||||
)
|
||||
|
||||
@field_validator("server_fastapi_port", "server_fasthtml_port")
|
||||
@field_validator("server_eos_port", "server_eosdash_port")
|
||||
def validate_server_port(cls, value: Optional[int]) -> Optional[int]:
|
||||
if value is not None and not (1024 <= value <= 49151):
|
||||
raise ValueError("Server port number must be between 1024 and 49151.")
|
||||
|
Reference in New Issue
Block a user