Skip to content

Commit

Permalink
build: replace URL_SCHEME, API_URL, FRONTEND_MAIN_URL --> FMTM_DOMAIN
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Oct 24, 2023
1 parent 64cc2d3 commit c56704a
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 108 deletions.
6 changes: 2 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ ODK_CENTRAL_PASSWD=${ODK_CENTRAL_PASSWD:-"testuserpassword"}
DEBUG=${DEBUG:-False}
LOG_LEVEL=${LOG_LEVEL:-INFO}
EXTRA_CORS_ORIGINS=${EXTRA_CORS_ORIGINS}
URL_SCHEME=${URL_SCHEME:-http}
API_URL=${API_URL:-"127.0.0.1:8000"}
FRONTEND_MAIN_URL=${FRONTEND_MAIN_URL:-"127.0.0.1:8080"}
FMTM_DOMAIN=${FMTM_DOMAIN:-"fmtm.localhost"}
# Use API_PREFIX if running behind a proxy subpath (e.g. /api)
API_PREFIX=${API_PREFIX:-/}

Expand All @@ -20,7 +18,7 @@ OSM_CLIENT_ID=${OSM_CLIENT_ID}
OSM_CLIENT_SECRET=${OSM_CLIENT_SECRET}
OSM_URL=${OSM_URL:-"https://www.openstreetmap.org"}
OSM_SCOPE=${OSM_SCOPE:-"read_prefs"}
OSM_LOGIN_REDIRECT_URI=${OSM_LOGIN_REDIRECT_URI:-"http://127.0.0.1:8080/osmauth/"}
OSM_LOGIN_REDIRECT_URI=${OSM_LOGIN_REDIRECT_URI:-"http://127.0.0.1:7051/osmauth/"}
OSM_SECRET_KEY=${OSM_SECRET_KEY}

### S3 File Storage ###
Expand Down
10 changes: 1 addition & 9 deletions .github/workflows/build_odk_imgs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 🔧 Build ODK Images
name: 🔧 Build ODK Image

on:
# Push includes PR merge
Expand All @@ -21,11 +21,3 @@ jobs:
"ghcr.io/${{ github.repository }}/odkcentral:latest"
build_args: |
ODK_CENTRAL_TAG=${{ vars.ODK_CENTRAL_TAG }}
build-proxy:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
context: odkcentral/proxy
image_tags: |
"ghcr.io/${{ github.repository }}/odkcentral-proxy:${{ vars.ODK_CENTRAL_TAG }}"
"ghcr.io/${{ github.repository }}/odkcentral-proxy:latest"
2 changes: 1 addition & 1 deletion .github/workflows/pr_label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/labeler@v3
- uses: actions/labeler@v4
# Uses .github/labeler.yml definitions
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
17 changes: 7 additions & 10 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ The FMTM uses OAUTH2 with OSM to authenticate users. To properly configure your

1. [Login to OSM](https://www.openstreetmap.org/login) (_If you do not have an account yet, click the signup button at the top navigation bar to create one_). Click the drop down arrow on the extreme right of the navigation bar and select My Settings.

2. Register your FMTM instance to OAuth 2 applications. Put your login redirect url as `http://127.0.0.1:8080/osmauth/`, For Production replace the URL as production API Url
2. Register your FMTM instance to OAuth 2 applications. Put your login redirect url as `http://127.0.0.1:7051/osmauth/`, For Production replace the URL as production API Url

> Note: `127.0.0.1` is required instead of `localhost` due to OSM restrictions.
Expand All @@ -87,17 +87,14 @@ ODK_CENTRAL_PASSWD=`<password_of_central_user>`
### FMTM ###
# DEBUG=True
# LOG_LEVEL=DEBUG
URL_SCHEME=http
API_URL=127.0.0.1:8000
FRONTEND_MAIN_URL=localhost:8080
# API_PREFIX=/api
FMTM_DOMAIN=fmtm.localhost
### OSM ###
OSM_CLIENT_ID=`<OSM_CLIENT_ID_FROM_ABOVE>`
OSM_CLIENT_SECRET=`<OSM_CLIENT_SECRET_FROM_ABOVE>`
OSM_URL=https://www.openstreetmap.org
OSM_SCOPE=read_prefs
OSM_LOGIN_REDIRECT_URI=http://127.0.0.1:8080/osmauth/
OSM_LOGIN_REDIRECT_URI=http://127.0.0.1:7051/osmauth/
OSM_SECRET_KEY=<random_key_for_development>
### S3 File Storage ###
Expand Down Expand Up @@ -128,7 +125,7 @@ The easiest way to get up and running is by using the FMTM Docker deployment. Do
3. From the command line run: `docker compose pull`.
This will pull the latest container builds from **main** branch.
4. Once everything is pulled, from the command line run: `docker compose up -d api`
5. If everything goes well you should now be able to **navigate to the project in your browser:** `http://127.0.0.1:8000/docs`
5. If everything goes well you should now be able to **navigate to the project in your browser:** `http://api.fmtm.localhost:7050/docs`

> Note: If those link doesn't work, check the logs with `docker logs fmtm_api`.
Expand All @@ -152,14 +149,14 @@ docker-compose exec central odk-cmd --email [email protected] user-promo
Some test data is available to get started quickly.

- Navigate to the `import-test-data` endpoint in the API docs page:
<http://127.0.0.1:8000/docs#/debug/import_test_data_debug_import_test_data_get>
<http://api.fmtm.localhost:7050/docs#/debug/import_test_data_debug_import_test_data_get>
- Click `Try it out`, then `execute`.

## Check Authentication

Once you have deployed, you will need to check that you can properly authenticate.

1. Navigate to `{URL_SCHEME}://{API_URL}/docs`
1. Navigate to `http://api.fmtm.localhost/docs`

Three endpoints are responsible for oauth
<img width="698" alt="image" src="./images/endpoints_responsible_for_auth_screenshot-2023-03-26-092756.png">
Expand Down Expand Up @@ -203,6 +200,6 @@ Make sure to replace `<your-username>` with your GitHub username.
This is essential, as the development container for the frontend is different to production.
4. Once everything is built, from the command line run: `docker compose up -d ui`

5. If everything goes well you should now be able to **navigate to the project in your browser:** <http://127.0.0.1:8080>
5. If everything goes well you should now be able to **navigate to the project in your browser:** <http://fmtm.localhost:7051>

That's it, you have successfully set up the frontend!!
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ services:
- ui
- central
- s3
environment:
FMTM_PORT: ${FMTM_PORT:-7050}
ports:
- ${FMTM_PORT:-7050}:80
networks:
Expand Down
6 changes: 3 additions & 3 deletions docs/dev/Backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The easiest way to get up and running is by using the FMTM Docker deployment. Do
This will pull the latest container builds from **main** branch.
4. Make sure you have a `.env` file with all required variables, see [Getting Started](https://github.com/hotosm/fmtm/blob/main/docs/DEV-1.-Getting-Started.md).
5. Once everything is pulled, from the command line run: `docker compose up -d api`
6. If everything goes well you should now be able to **navigate to the project in your browser:** `http://127.0.0.1:8000/docs`
6. If everything goes well you should now be able to **navigate to the project in your browser:** `http://api.fmtm.localhost:7050/docs`

> Note: If that link doesn't work, check the logs with `docker log fmtm_api`.
Expand All @@ -35,7 +35,7 @@ The easiest way to get up and running is by using the FMTM Docker deployment. Do
Some test data is available to get started quickly.

1. Navigate to the `import-test-data` endpoint in the API docs page:
<http://127.0.0.1:8000/docs#/debug/import_test_data_debug_import_test_data_get>
<http://api.fmtm.localhost:7050/docs#/debug/import_test_data_debug_import_test_data_get>
2. Click `Try it out`, then `execute`.

## 2. Start the API without Docker
Expand Down Expand Up @@ -72,7 +72,7 @@ After starting the database, from the command line:
3. Install backend dependencies with PDM: `pdm install`
4. Run the Fast API backend with: `pdm run uvicorn app.main:api --host 0.0.0.0 --port 8000`

The API should now be accessible at: <http://127.0.0.1:8000/docs>
The API should now be accessible at: <http://api.fmtm.localhost:7050/docs>

## 3. Hybrid Docker/Local

Expand Down
2 changes: 0 additions & 2 deletions docs/dev/Docker-Tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ set up the environment and dependencies for each container.
build:
context: .
dockerfile: Dockerfile
environment:
API_URL: http://api:8000
ports:
- "3000:3000"

Expand Down
2 changes: 1 addition & 1 deletion docs/dev/Frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For details on how to run the API first, please see: [DEV 2. Backend](https://gi
This is essential, as the development container for the frontend is different to production.
4. Once everything is built, from the command line run: `docker compose up -d ui`

5. If everything goes well you should now be able to **navigate to the project in your browser:** <http://127.0.0.1:8080>
5. If everything goes well you should now be able to **navigate to the project in your browser:** <http://fmtm.localhost:7050>

> Note: during development, if you rebuild the frontend, then
> run 'docker compose up -d', the node_modules directory may
Expand Down
9 changes: 5 additions & 4 deletions docs/dev/Production.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ that file to contain the needful (it should look like this):
ODK_CENTRAL_PASSWD=`<CHANGEME>`

# FMTM
API_URL=https://fmtm-api.hotosm.org
FRONTEND_MAIN_URL=https://fmtm.hotosm.org
# API_PREFIX=/api
VITE_API_URL=https://api.fmtm.hotosm.org
FMTM_DOMAIN=https://fmtm.hotosm.org

# OSM
OSM_CLIENT_ID=`<CHANGEME>`
Expand All @@ -80,7 +79,9 @@ that file to contain the needful (it should look like this):
FMTM_DB_PASSWORD=`<CHANGEME>`
FMTM_DB_NAME=fmtm

> Note: It is also possible to use the API_PREFIX variable if the api is served under, e.g. /api on the domain.
> Note: It is also possible to use the API_PREFIX variable if the api
> is served under, e.g. /api on the domain.
> However, this isn't the recommended approach, and testing is minimal.
> Note: You must have an existing version of ODKCentral running, to provide the URL and credentials here.
Expand Down
21 changes: 13 additions & 8 deletions docs/dev/Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ The FMTM uses OAUTH2 with OSM to authenticate users. To properly configure your

1. [Login to OSM](https://www.openstreetmap.org/login) (_If you do not have an account yet, click the signup button at the top navigation bar to create one_). Click the drop down arrow on the extreme right of the navigation bar and select My Settings.

2. Register your FMTM instance to OAuth 2 applications. Put your login redirect url as `http://127.0.0.1:8080/osmauth/`, For Production replace the URL as production API Url
2. Register your FMTM instance to OAuth 2 applications. Put your login redirect url as `http://127.0.0.1:7051/osmauth/`, For Production replace the URL as production API Url

> Note: `127.0.0.1` is required instead of `localhost` due to OSM restrictions.
Expand All @@ -267,17 +267,14 @@ ODK_CENTRAL_PASSWD=`<password_of_central_user>`
### FMTM ###
# DEBUG=True
# LOG_LEVEL=DEBUG
URL_SCHEME=http
API_URL=127.0.0.1:8000
FRONTEND_MAIN_URL=localhost:8080
# API_PREFIX=/api
FMTM_DOMAIN=fmtm.localhost
### OSM ###
OSM_CLIENT_ID=`<OSM_CLIENT_ID_FROM_ABOVE>`
OSM_CLIENT_SECRET=`<OSM_CLIENT_SECRET_FROM_ABOVE>`
OSM_URL=https://www.openstreetmap.org
OSM_SCOPE=read_prefs
OSM_LOGIN_REDIRECT_URI=http://127.0.0.1:8080/osmauth/
OSM_LOGIN_REDIRECT_URI=http://127.0.0.1:7051/osmauth/
OSM_SECRET_KEY=<random_key_for_development>
### S3 File Storage ###
Expand All @@ -297,7 +294,7 @@ FMTM_DB_PASSWORD=fmtm
FMTM_DB_NAME=fmtm'
```

> Note: If extra cors origins are required for testing, the variable `EXTRA_CORS_ORIGINS` is a set of comma separated strings, e.g. <http://localhost:7050,http://localhost:7051>
> Note: If extra cors origins are required for testing, the variable `EXTRA_CORS_ORIGINS` is a set of comma separated strings, e.g. <http://fmtm.localhost:7050,http://some.other.domain>
## Verify Setup

Expand All @@ -309,7 +306,7 @@ For details on how to run this project locally for development, please look at:

Once you have deployed, you will need to check that you can properly authenticate.

1. Navigate to `http://127.0.0.1:8000/docs`
1. Navigate to `http://api.fmtm.localhost:7050/docs`

Three endpoints are responsible for oauth
<img width="698" alt="image" src="../images/endpoints_responsible_for_auth_screenshot-2023-03-26-092756.png">
Expand All @@ -336,3 +333,11 @@ Don't forget to review [Contribution](https://github.com/hotosm/fmtm/wiki/Contri
Happy coding!

The FMTM Developer Team

## Note

To run the local development setup without ODK Central (use external server):

```bash
dc --profile no-odk up -d
```
2 changes: 1 addition & 1 deletion docs/dev/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ If you would rather not do this,
an alternative can be to feed them into the pdm command:

```bash
FRONTEND_MAIN_URL="" \
FMTM_DOMAIN="" \
OSM_CLIENT_ID="" OSM_CLIENT_SECRET="" OSM_SECRET_KEY="" \
S3_ACCESS_KEY="" S3_SECRET_KEY="" \
pdm run uvicorn app.main:api --host 0.0.0.0 --port 8000
Expand Down
62 changes: 12 additions & 50 deletions gen-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,60 +107,23 @@ then
fi

echo
echo "Do you want access FMTM securely over https?"
echo "**If yes, you need to provide valid domain names with certificates later.**"
echo "Enter the domain name you wish to use for FMTM."
while true
do
read -e -p "Enter y for https, anything else for http: " https
read -e -p "Enter d for default fmtm.localhost, else a domain you own: " fmtm_domain

if [ "$https" = "y" ]
if [ "$fmtm_domain" != "d" ]
then
echo "Using https."
URL_SCHEME="https"
fi
break
done

# API
echo
echo "Enter the FMTM API URL."
echo "If you have a valid domain name, enter it here."
while true
do
read -e -p "Enter d for default 127.0.0.1:8000, else your IP/domain: " api_url

if [ "$api_url" != "d" ]
then
echo "Using $API_URL"
break
elif [ "$api_url" = "" ]
then
echo "Invalid input!"
else
echo "Using $api_url"
API_URL="api_url"
break
fi
done

# FRONTEND
echo
echo "Enter the FMTM Frontend URL."
echo "If you have a valid domain name, enter it here."
while true
do
read -e -p "Enter d for default 127.0.0.1:8080, else your IP/domain: " frontend_url

if [ "$frontend_url" != "d" ]
then
echo "Using $FRONTEND_MAIN_URL"
echo "Using $fmtm_domain"
FMTM_DOMAIN="${fmtm_domain}"
# Manually set api subdomain
break
elif [ "$api_url" = "" ]
elif [ "$fmtm_domain" = "" ]
then
echo "Invalid input!"
else
echo "Using $FRONTEND_MAIN_URL"
FRONTEND_MAIN_URL="frontend_url"
echo "Using fmtm.localhost"
FMTM_DOMAIN="fmtm.localhost"
break
fi
done
Expand All @@ -170,7 +133,7 @@ echo "Please enter your OSM authentication details"
read -e -p "Client ID: " OSM_CLIENT_ID
read -e -p "Client Secret: " OSM_CLIENT_SECRET
read -e -p "Secret Key: " OSM_SECRET_KEY
echo "Login redirect URI (default http://127.0.0.1:8080/osmauth/): "
echo "Login redirect URI (default http://127.0.0.1:7051/osmauth/): "
while true
do
read -e -p "Enter a URI, or nothing for default: " auth_redirect_uri
Expand Down Expand Up @@ -199,9 +162,8 @@ echo "DEBUG=${DEBUG}" >> "${DOTENV_NAME}"
echo "LOG_LEVEL=${LOG_LEVEL}" >> "${DOTENV_NAME}"

echo "### FMTM ###"
echo "URL_SCHEME=${URL_SCHEME}" >> "${DOTENV_NAME}"
echo "API_URL=${API_URL}" >> "${DOTENV_NAME}"
echo "FRONTEND_MAIN_URL=${FRONTEND_MAIN_URL}" >> "${DOTENV_NAME}"
echo "FMTM_DOMAIN=${FMTM_DOMAIN}" >> "${DOTENV_NAME}"
# TODO set VITE_API_URL

echo "### OSM ###"
echo "OSM_CLIENT_ID=${OSM_CLIENT_ID}" >> "${DOTENV_NAME}"
Expand Down
2 changes: 1 addition & 1 deletion src/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ CMD ["python", "-m", "debugpy", "--listen", "0.0.0.0:5678", \
FROM debug-no-odk as debug-with-odk
USER root
# Add the SSL cert for debug odkcentral
COPY --from=ghcr.io/hotosm/fmtm/odkcentral-proxy:latest \
COPY --from=ghcr.io/hotosm/fmtm/proxy:debug \
/etc/nginx/central-fullchain.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates
USER appuser
Expand Down
10 changes: 7 additions & 3 deletions src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ def callback(request: Request, osm_auth=Depends(init_osm_auth)):
-------
- access_token (string)
"""
print("Call back api requested", request.url)
log.debug(f"Callback url requested: {request.url}")

access_token = osm_auth.callback(
str(request.url).replace("http", settings.URL_SCHEME)
callback_url = (
str(request.url).replace("http", "https")
if settings.DEBUG
else str(request.url)
)
access_token = osm_auth.callback(callback_url)

log.debug(f"Access token returned: {access_token}")
return JSONResponse(content={"access_token": access_token}, status_code=200)

Expand Down
Loading

0 comments on commit c56704a

Please sign in to comment.