问题描述
我目前正在开发一个带有单独的前端(Vue)和后端(quarkus REST API)项目的简单Web应用程序.现在,我已经设置了一个MVP,前端在其中显示一些简单的数据,这些数据是从后端调用的.要获得有效的MVP,我需要设置CORS支持.但是,首先,我想解释一下我的设置:
I'm currently developing a simple webapp with seperated frontend (Vue) and backend (quarkus REST API) project. For now, I've setup a MVP, where the frontend is displaying some simple data which is called from the backend. To get a working MVP i need to setup CORS support. However, first i want to explain my setup:
我正在使用 npm run serve
来开发前端环境,并使用 ./mvnw quarkus:dev
来开发后端环境.前端在 localhost:8081
上运行,后端在 localhost:8080
上运行.Heroku允许使用命令 heroku local web
在本地运行您的应用程序.前端在端口 0.0.0.0:5001
上运行,后端在 0.0.0.0:5000
上运行.
I'm starting developing environment of my frontend with npm run serve
and of my backend with ./mvnw quarkus:dev
. Frontend is running on localhost:8081
and backend running on localhost:8080
. Heroku allows to run your apps locally aswell with the command heroku local web
. Frontend is running on port 0.0.0.0:5001
and backend on 0.0.0.0:5000
.
要实现此设置,我在前端设置了两个 .env
文件,它们指向我的后端api.如果我想在开发模式下工作,将加载文件 .env.development
:
To achieve this setup i setup two .env
files on my frontend which are pointing to my backend api. If i want to work in development mode the file .env.development
is loaded:
VUE_APP_ROOT_API=http://localhost:8080
,如果我运行 heroku本地网络
,则文件
VUE_APP_ROOT_API=0.0.0.0:5000
已加载.
在后端,我设置了 quarkus.http.cors = true
在我的 application.properties
中.
In my backend I've set quarkus.http.cors=true
in my application.properties
.
现在,我想将这两个项目部署到heroku并在生产中使用它.因此,我设置了两个heroku项目,并在前端项目中使用以下值设置了一个配置变量:
Now I want to deploy those two projects to heroku and use it in production. Therefore I setup two heroku projects and set a config variable in my frontend project with the following value:
VUE_APP_ROOT_API:https://mybackend.herokuapp.com
从我的前端拨打的电话成功运行!
Calls from my frontend are successfully working!
下一步,我想进一步限制它,只需启用前端即可调用我的API.我知道我可以设置类似的内容
For the next step, I want to restrict it more and just enable my frontend to call my API. I know i can set something like
quarkus.http.cors.origins=myfrontend.herokuapp.com
但是,我不知道如何在具有不同环境(开发,本地和生产)的quarkus上执行此操作?我已经找到了这个链接,但是我不知道如何配置heroku和我的后端应用正确.我是否需要设置适用于不同环境的不同配置文件?还是有其他解决方案?我需要Herokus配置变量吗?
However, I dont know how i could do this on quarkus with different environments (development, local and production)? I've found this link but I don't know how to configure heroku and my backend app correctly. Do i need to setup different profiles which are applied on my different environments? Or is there another solution? Do i need Herokus Config Variables?
感谢您的帮助!
推荐答案
quarkus.http.cors.origins
在运行时可重写,因此您有几种可能.
quarkus.http.cors.origins
is overridable at runtime so you have several possibilities.
您可以使用配置文件,并使用%prod.quarkus.http.cors.origins = ...
在您的 application.properties
中进行所有设置.然后,在启动应用程序时使用 -Dquarkus.profile = prod
或将 QUARKUS_PROFILE = prod
用作环境变量.
You could use a profile and have everything set up in your application.properties
with %prod.quarkus.http.cors.origins=...
. Then you either use -Dquarkus.profile=prod
when launching your application or you use QUARKUS_PROFILE=prod
as an environment variable.
另一种选择是将环境变量用于 quarkus.http.cors.origins
.那将是 QUARKUS_HTTP_CORS_ORIGINS = ...
.
Another option is to use an environment variable for quarkus.http.cors.origins
. That would be QUARKUS_HTTP_CORS_ORIGINS=...
.
我的建议是使用个人资料.这样一来,您就可以安全地检查所有配置是否一目了然.
My recommendation would be to use a profile. That way you can safely check that all your configuration is consistent at a glance.
这篇关于Quarkus,Heroku和其他环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!