Skip to content

Commit

Permalink
feat: added fastapi and connected danger zones to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzohasashi33 committed Sep 24, 2023
1 parent a36ae3a commit 0cfeabc
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 88 deletions.
18 changes: 12 additions & 6 deletions geoparky-redis-api/redis_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""
Hosting the nearest neighbours' functionality in a Flask API
"""
from flask import Flask, request
from flask import Flask, request, jsonify
from redis_lib import create_redis_client, add_geospatial_point, find_geospatial_points_within_radius
from flask_cors import CORS, cross_origin
import os

# Create the Redis client
r = create_redis_client(
host_url = os.environ["REDIS_URL"],
host_password = os.environ["REDIS_PASSWORD"]
host_url = env.host_url,
host_password = env.host_password
)

# Get number of nearest neighbours functionality
Expand All @@ -21,6 +22,8 @@ def get_number_risk_points(latitude, longitude):

# Set up flask app
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

# Base route
@app.route('/')
Expand All @@ -29,6 +32,7 @@ def hello_world():

# Nearest points' route
@app.route('/nearby', methods = ['POST'])
@cross_origin()
def nearby_points():
global r

Expand All @@ -37,13 +41,15 @@ def nearby_points():
longitude = str(input_json["longitude"])

num_points = get_number_risk_points(latitude, longitude)
output = {
response = jsonify({
"latitude" : latitude,
"longitude" : longitude,
"points" : num_points
}
})

return output
response.headers.add('Access-Control-Allow-Origin', '*')

return response

if __name__ == "__main__":
app.run()
56 changes: 56 additions & 0 deletions geoparky-redis-api/redis_api2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from fastapi import FastAPI, status, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from redis_lib import create_redis_client, add_geospatial_point, find_geospatial_points_within_radius


# Create the Redis client
r = create_redis_client(
host_url = env.host_url,
host_password = env.host_password
)

# Get number of nearest neighbours functionality
def get_number_risk_points(latitude, longitude):
latitude = float(latitude)
longitude = float(longitude)

points = find_geospatial_points_within_radius(r, latitude, longitude, radius = 1000, collection_name="geoparky")
return len(points)


app = FastAPI()

origins = ["*"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/")
async def root():
return {"message": "Hello World"}



# Nearest points' route
@app.post('/nearby')
async def nearby_points(request: Request):
global r

input_json = await request.json()
latitude = str(input_json["latitude"])
longitude = str(input_json["longitude"])

num_points = get_number_risk_points(latitude, longitude)
response = {
"latitude" : latitude,
"longitude" : longitude,
"points" : num_points
}
return response
33 changes: 26 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.3.2",
"dotenv": "^16.3.1",
"georedis": "^3.1.3",
"react": "^18.2.0",
"react-bootstrap": "^2.8.0",
Expand Down
8 changes: 8 additions & 0 deletions pyvenv.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
home = C:\Python310
implementation = CPython
version_info = 3.10.0.final.0
virtualenv = 20.24.5
include-system-site-packages = false
base-prefix = C:\Python310
base-exec-prefix = C:\Python310
base-executable = C:\Python310\python.exe
Loading

0 comments on commit 0cfeabc

Please sign in to comment.