I have simple spring boot java backend application which get element from the list (endpoint /get/{id}
) and add element to the list (endpoint /add/{product}
):
@RestController
public class DemoController {
List<String> products = Arrays.asList("test");
@PostMapping(path="/add/{product}")
public int addProduct(@PathVariable final String product){
products.add(product);
return products.size()-1;
}
@GetMapping(path="/get/{id}")
public String getValue(@PathVariable final int id){
return products.get(id);
}
}
As frontend I have simple python application like:
from flask import Flask, request
import requests as r
import os
app = Flask(__name__)
@app.route("/")
def renderProduct():
return """
<html>
<head>
<title>""" + os.environ["title"] + """</title>
</head>
<form id="1" method="POST">
<input name="getID"/>
<br>
<input name="addID">
<input type="submit">
</form>
</html>
"""
@app.route("/", methods=["POST"])
def queryAndRender():
builded = "<html>"
if request.form["getID"] is not None:
resp = r.get("http://localhost:8080/get/" + request.form["getID"])
builded = builded + "PRODUCT:" + resp.text + "<br>"
if request.form["addID"] is not None:
resp = r.get("http://localhost:8080/add/" + request.form["addID"])
builded = builded + "ADDED ID:" + resp.text + "<br>"
builded = builded + """<html>
<head>
<title>""" + os.environ["title"] + """</title>
</head>
<form id="1" method="POST">
<input name="getID"/>
<br>
<input name="addID">
<input type="submit">
</form>
</html>
"""
return builded;
if __name__ == "__main__":
app.run()
And Dockerfile:
ARG version=3.8.5-alpine3.11
FROM python:${version}
ENV title="Hello world"
ENV test testspacja
ENV FLASK_APP=/main.py
RUN pip install Flask==1.1.2
RUN pip install requests==2.22.0
COPY main.py /
EXPOSE 80/tcp
ENTRYPOINT ["flask", "run"]
CMD ["-h", "0.0.0.0", "-p", "80"]
Now I can run my frontend in docker container:
docker run -p 8081:80 frontend
and it is visible under http://localhost:8081/
Now I would like to start my backend in intelliJ. Why my frontend doesn't "see" backend under endpoints http://localhost:8080/get/{id}
and http://localhost:8080/add/{product}
? I can call them from browser but why frontend can't? I see only:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.