I have an Api developed in FastApi framework. I have one Angular application that calls the FastApi. Due to several reasons, it would be great (network names, parameters, docker images, etc) if I could deploy the angular application in the FastApi as a simple web application. This way I could access the Api as "localhost" and the Api can be "private".
Is this possible?
thank you, José Cruz
I don't know if it can help you but it worked for me.
I warn you that this is not a good practice but in the context I applied it solved my problem.
I have a small Angular application that login and authenticated redirection to FastAPI's Redoc and Swagger documentation.
I set up a directory with static files and put the angular build inside. In FastAPI I made an endpoint reading and serving the index.html file.
For the angular to see the files, I build Angular by passing the --base-href flag pointing to the mounted static directory.
The codes look like this:
project/
static/
index.html
... (files app angular)
main.py
main.py
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get('/')
def get_app_angular():
with open('static/index.html', 'r') as file_index:
html_content = file_index.read()
return HTMLResponse(html_content, status_code=200)
Build APP Angular:
ng build --base-href static/ --prod
No, it is not possible as far as I know. It would be like talking two different languages. With JSON files you may be able to share configurations, but not more.
Maybe going on the low level (both python and nodejs are written in C or C++), you could bind them, but you would get much more of an overhead to do this than to simply spawn two separate containers.
Also, separating API and frontend is a good practice, which allows you to perform changes/updates in a much simpler form and to scale one service in case of sudden peaks. In addition, the security is improved, as the two containers are virtually separated and secured by the containers. Breaking into one, would not necessarily compromise the second (I said would because it depends on what access information you store and permissions you give to the former).