Skip to content

Commit

Permalink
fix: Development environment proxy settings
Browse files Browse the repository at this point in the history
Vite should redirect API requests from port 5173 to port 5000 to reach flask. We have to manually specify each root API endpoint.
  • Loading branch information
Joe-Heffer-Shef committed Jul 26, 2024
1 parent 07e7c03 commit cc86d2b
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 22 deletions.
4 changes: 3 additions & 1 deletion backend/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ pip install --editable ./backend
To run the app in development mode:

```bash
flask --app backend.btviewer.app --debug run
flask --app backend.btviewer.app --debug run --port 5000
```

We must always use port 5000 because the front-end is configured to use that port.

To run the frontend user interface in development mode, see the [frontend contribution guide](../frontend/CONTRIBUTING.md).

## Deployment
Expand Down
1 change: 1 addition & 0 deletions backend/btviewer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
ROOT_DIRECTORY = os.getenv('ROOT_DIRECTORY', Path(__file__).parent.parent.joinpath('tests/data'))

app = create_app(root_directory=ROOT_DIRECTORY)
"DEVELOPMENT Flask app"
1 change: 1 addition & 0 deletions backend/btviewer/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union

import flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware


def create_app(root_directory: Union[Path, str] = None, **kwargs) -> flask.Flask:
Expand Down
4 changes: 2 additions & 2 deletions backend/btviewer/blueprints/photo/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import io
import json
from pathlib import Path
from typing import Iterable, Mapping, Union, Generator
from typing import Iterable, Mapping, Union, Generator, Tuple

import PIL.Image
import flask
import numpy

app: flask.Flask = flask.current_app

Coordinate = tuple[int, int]
Coordinate = Tuple[int, int]
"x, y position in a 2D image pixel grid"


Expand Down
7 changes: 3 additions & 4 deletions frontend/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ npm install
The development environment is defined by the `dev` configuration in `package.json` and uses the [Vite](https://vitejs.dev/guide/#index-html-and-project-root) tool.

```
# Run the development environment
npm run dev
```



## Vite

[Vite](https://vitejs.dev/) provides Hot Module Replacement (HMR) and some code analysis using ESLint rules. It uses `index.html` as the [home page](https://vitejs.dev/guide/#index-html-and-project-root).
[Vite](https://vitejs.dev/) provides automatic code reloading using Hot Module Replacement (HMR) and some code analysis using ESLint rules. It uses `index.html` as the [home page](https://vitejs.dev/guide/#index-html-and-project-root).

There is a configuration file for Vite at `frontend/vite.config.js` that specifies some proxy settings so that we can access the backend via the frontend testing app (which defaults to port 5173.)
2 changes: 1 addition & 1 deletion frontend/src/components/CameraList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (typeof(data)==="string"){ //when there is nth in the data, especially when t
function changeHandler(e) {
let selectedCamera = e.target.value;
// Update the list of photo filenames available in that session
let url = `/api/sessions/list/?path=${subdirectory}/${selectedCamera}`;
let url = '/sessions/list/?path=${subdirectory}/${selectedCamera}';
console.log(url)
fetch(url)
.then((response) => response.json())
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/DeviceList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (typeof(data)==="string"){ //when there is nth in the data, especially when t
let selectedDevice = e.target.value;
console.log(selectedDevice)
// Update the list of photo filenames available in that session
let url = `/api/sessions/list/?path=${subdirectory}/${selectedDevice}`;
let url = `/sessions/list/?path=${subdirectory}/${selectedDevice}`;
console.log(url)
fetch(url)
.then((response) => response.json())
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/components/PhotoSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ function PhotoSelection({ photoFilenames, subdirectory }) {
console.log('subdirectoryJoined')
console.log(subdirectoryJoined)
//SC:Did we fetch photo, it does not seems so, and I only submit the image source to the image.jsx
let urlJpeg = "/api/photos/" + subdirectoryJoined.replace("np", "jpeg");
let urlJpeg = "/photos/" + subdirectoryJoined.replace("np", "jpeg");
setCurrentPhoto(urlJpeg);
console.log('urlJpeg' + urlJpeg);

//Get photo dimension
let urlDimension = "/api/photos/dimension?path=" + subdirectoryJoined;
let urlDimension = "/photos/dimension?path=" + subdirectoryJoined;
fetch(urlDimension)
.then((response) => response.json())
.then((data)=>{
Expand All @@ -63,7 +63,7 @@ function PhotoSelection({ photoFilenames, subdirectory }) {
})

// Get the json for the human/retrodetect label coordinates if it exists
let urlLabel = "/api/labels/detail?path=" + subdirectoryJoined;
let urlLabel = "/labels/detail?path=" + subdirectoryJoined;
console.log("urlLabel");
console.log(urlLabel);
fetch(urlLabel)
Expand All @@ -76,18 +76,18 @@ function PhotoSelection({ photoFilenames, subdirectory }) {
function handleNextPrevPhoto(current_path, skipnumber) {
const currentPath = current_path;
const skip = skipnumber;
let url = `/api/photos/next?path=${currentPath}&skip=${skip}`;
let url = `/photos/next?path=${currentPath}&skip=${skip}`;
console.log("urlcurret photo in next");
console.log(url);

fetch(url)
.then((response) => response.json())
.then((data) => {
setPhotoPath(data);
let urlJpeg = "/api/photos/" + data.replace("np", "jpeg");
let urlJpeg = "/photos/" + data.replace("np", "jpeg");
setCurrentPhoto(urlJpeg);

let urlDimension = "/api/photos/dimension?path=" + data;
let urlDimension = "/photos/dimension?path=" + data;
fetch(urlDimension)
.then((response) => response.json())
.then((data)=>{
Expand All @@ -97,7 +97,7 @@ function PhotoSelection({ photoFilenames, subdirectory }) {
});
})

let urlLabel = "/api/labels/detail?path=" + data;
let urlLabel = "/labels/detail?path=" + data;
fetch(urlLabel)
.then((response) => response.json())
.then((data) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/SessionList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function SessionList({ data }) {
setSelectedSubdirectory(selected)

// Update the list of photo filenames available in that session
let url = `/api/sessions/list/?path=${selected}`;
let url = `/sessions/list/?path=${selected}`;
console.log('sessionslist')
console.log(url)
fetch(url)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Sessions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The list of bee tracking photo data capture sessions.
*/
function Sessions() {
// TODO use global variable for backend URL
const url = "/api/sessions/list";
const url = "/sessions/list/";

console.log('sessions')
console.log(url)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/SetList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if (typeof(data)==="string"){ //when there is nth in the data, especially when t
let selectedSet = e.target.value;
console.log(selectedSet)
// Update the list of photo filenames available in that session
let url = `/api/sessions/list/?path=${subdirectory}/${selectedSet}`;
let url = `/sessions/list/?path=${subdirectory}/${selectedSet}`;
console.log('set')
console.log(url)
console.log(url)
Expand Down
13 changes: 10 additions & 3 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ export default defineConfig({
server: {
// Create a proxy endpoint to the backend to avoid cross-origin (CORS) issues
proxy: {
'/api': {
'/sessions': {
target: 'http://localhost:5000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
changeOrigin: true
},
'/photos': {
target: 'http://localhost:5000',
changeOrigin: true
},
'/labels': {
target: 'http://localhost:5000',
changeOrigin: true
},
},
},
Expand Down

0 comments on commit cc86d2b

Please sign in to comment.