incubator-answer
incubator-answer copied to clipboard
Feat/base url config
Problem
I would love to use the second-level domain name of nginx to deploy Answer
. However, this requires the webapp to work on relative url path (e.g. /foo/questions) or support setting a base url via configuration.
Solution
Add support for setting a base/root url in the configuration or make the webapp work on relativ urls. Having this option is very useful in a variety of deployment scenarios (e.g. behind a reverse proxy).
Adds the config option REACT_APP_BASE_URL_PATH
. When set to a value like /foo
, Anwser will be served from hostname:port/foo
.
Usage
- edit the
ui/.env.production
file
...
REACT_APP_BASE_URL_PATH=/foo
...
- Recompile
make ui
make build
- Initialization and Run
# Initialization
INSTALL_PORT=9080 REACT_APP_BASE_URL_PATH=/foo ./answer init -C ./answer-data/
# Run
REACT_APP_BASE_URL_PATH=/foo ./answer run -C ./answer-data/
Note:
- If the value of
REACT_APP_BASE_URL_PATH
is the default""
, there is no need to set the environment variable.- The value of
REACT_APP_BASE_URL_PATH
must not end with/
. For example,PUBLIC_URL=/foo
is correct, whilePUBLIC_URL=/foo/
is incorrect.
Thank you @liuyuanzhi-cn, this PR seems to be related to #335
react-router-dom supports adding basename, which will save a lot of files. by adjusting like this, there should be less code modifications.
const router = createBrowserRouter(routes, {
basename: process.env.REACT_APP_BASE_URL_PATH,
});
However, by adding basename, no matter how you adjust it, there will be a certain degree of code intrusion, and a lot of testing is required, because it may also include some callback addresses returned by the API, or the oauth address in the plug-in, etc.
Another way is to directly use nginx as a proxy without changing the code in the business. How effective is this method?
@liuyuanzhi-cn
I looked at the changes you submitted. There will be a lot of code intrusion and modification. I tried to achieve it by adding basename in combination with react-router-dom. This can be achieved with only a small amount of changes. You can refer to here.
Of course, I only modified the front-end related routing configuration, and some routes returned through the API have not yet been adjusted. This modification should be reasonable.
@liuyuanzhi-cn
I looked at the changes you submitted. There will be a lot of code intrusion and modification. I tried to achieve it by adding basename in combination with react-router-dom. This can be achieved with only a small amount of changes. You can refer to here.
Of course, I only modified the front-end related routing configuration, and some routes returned through the API have not yet been adjusted. This modification should be reasonable.
Indeed, this looks great, it's much more elegant this way, with a lot less code changes.
@liuyuanzhi-cn I looked at the changes you submitted. There will be a lot of code intrusion and modification. I tried to achieve it by adding basename in combination with react-router-dom. This can be achieved with only a small amount of changes. You can refer to here. Of course, I only modified the front-end related routing configuration, and some routes returned through the API have not yet been adjusted. This modification should be reasonable.
Indeed, this looks great, it's much more elegant this way, with a lot less code changes.
We will confirm support for this requirement in subsequent versions as soon as possible! Of course, if you are interested, you are welcome to continue to submit PR according to this idea.
We will support this feature in version 1.3.5. The PR you contributed here is currently not applicable, so I will close the PR first.