From f9a86b0962030e858eee29c96f5dd5e3d0705798 Mon Sep 17 00:00:00 2001 From: Adi Elisha Date: Sat, 18 Apr 2020 17:10:19 +0300 Subject: [PATCH 1/2] abit of refactor --- .gitignore | 4 ++++ .idea/Mafia.iml | 4 ++++ .idea/misc.xml | 3 +++ backend/lib/api.py | 8 ++++++++ backend/lib/models/__init__.py | 0 backend/lib/server.py | 7 ------- frontend/package.json | 4 +++- 7 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 backend/lib/api.py create mode 100644 backend/lib/models/__init__.py diff --git a/.gitignore b/.gitignore index 6a1037f..f68cb52 100644 --- a/.gitignore +++ b/.gitignore @@ -125,3 +125,7 @@ dist npm-debug.log* yarn-debug.log* yarn-error.log* +/venv +/__pycache__ +venv +__pycache__ \ No newline at end of file diff --git a/.idea/Mafia.iml b/.idea/Mafia.iml index d0876a7..8b8c395 100644 --- a/.idea/Mafia.iml +++ b/.idea/Mafia.iml @@ -5,4 +5,8 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index a2e120d..ba24381 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/backend/lib/api.py b/backend/lib/api.py new file mode 100644 index 0000000..3e20d65 --- /dev/null +++ b/backend/lib/api.py @@ -0,0 +1,8 @@ +import time +from flask import Flask + +app = Flask(__name__) + +@app.route('/time') +def get_current_time(): + return {'time': time.time()} \ No newline at end of file diff --git a/backend/lib/models/__init__.py b/backend/lib/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/lib/server.py b/backend/lib/server.py index a7941a9..e69de29 100644 --- a/backend/lib/server.py +++ b/backend/lib/server.py @@ -1,7 +0,0 @@ -from flask import Flask -app = Flask(__name__) - - -@app.route('/') -def hello_world(): - return 'Hello, World!' diff --git a/frontend/package.json b/frontend/package.json index 3dc0855..045fcc0 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -16,6 +16,7 @@ }, "scripts": { "start": "react-scripts start", + "start-api": "cd ../backend/lib && venv/bin/flask run --no-debugger", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" @@ -34,5 +35,6 @@ "last 1 firefox version", "last 1 safari version" ] - } + }, + "proxy": "http://localhost:5000" } From 1ca0b4215e06786c174f7eb52aa065912d07be33 Mon Sep 17 00:00:00 2001 From: Adi Elisha Date: Sat, 18 Apr 2020 18:01:05 +0300 Subject: [PATCH 2/2] api now works --- backend/lib/api.py | 6 ++---- backend/lib/routes.py | 16 ++++++++++++++++ backend/lib/server.py | 0 frontend/src/APIUtils.js | 22 ++++++++++------------ frontend/src/shared_code/consts.js | 19 ++++++++++--------- 5 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 backend/lib/routes.py delete mode 100644 backend/lib/server.py diff --git a/backend/lib/api.py b/backend/lib/api.py index 3e20d65..a5eea58 100644 --- a/backend/lib/api.py +++ b/backend/lib/api.py @@ -1,8 +1,6 @@ -import time from flask import Flask +from lib.routes import api app = Flask(__name__) -@app.route('/time') -def get_current_time(): - return {'time': time.time()} \ No newline at end of file +app.register_blueprint(api) diff --git a/backend/lib/routes.py b/backend/lib/routes.py new file mode 100644 index 0000000..9331628 --- /dev/null +++ b/backend/lib/routes.py @@ -0,0 +1,16 @@ +import time +from random import randrange + +from flask import request, jsonify, Blueprint + +api = Blueprint('api', 'api', url_prefix='/api') + + +@api.route('/room/create', methods=['POST']) +def createRoom(): + creator_id = request.get_json().get('creatorID') + room_id = randrange(999999999) + creation_date = time.time() + room_socket_port = randrange(30000, 50000) + + return jsonify({'roomID': room_id, 'creationDate': creation_date, 'roomSocketPort': room_socket_port}) diff --git a/backend/lib/server.py b/backend/lib/server.py deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/src/APIUtils.js b/frontend/src/APIUtils.js index 2b9e6c3..0967684 100644 --- a/frontend/src/APIUtils.js +++ b/frontend/src/APIUtils.js @@ -3,23 +3,21 @@ import { SERVER_PORT, ROOM_ROUTES, ROOM_ACTIONS, -} from './shared_code/consts'; -import axios from 'axios'; + API_ROUTES, +} from "./shared_code/consts"; +import axios from "axios"; export const createRoom = async (roomDispatch, creatorID) => { const req = { creatorID }; try { - const res = await axios.post( - `${SERVER_ADDRESS}:${SERVER_PORT}/${ROOM_ROUTES}/create`, - req, - ); + const res = await axios.post(`/${API_ROUTES}/${ROOM_ROUTES}/create`, req); roomDispatch( ROOM_ACTIONS.initRoom( res.data.roomID, res.data.creationDate, res.data.roomSocketPort, - [], - ), + [] + ) ); } catch (err) { window.alert(`Server giving us something wrong! ${err}`); @@ -30,7 +28,7 @@ export const registerToRoom = async ( playerID, playerName, roomID, - roomDispatch, + roomDispatch ) => { try { const res = await axios.get( @@ -41,7 +39,7 @@ export const registerToRoom = async ( playerName, roomID, }, - }, + } ); const { error, errorMessage } = res.data; if (error) { @@ -52,8 +50,8 @@ export const registerToRoom = async ( roomID, res.data.creationDate, res.data.roomSocketPort, - res.data.roomPlayersList, - ), + res.data.roomPlayersList + ) ); } } catch (err) { diff --git a/frontend/src/shared_code/consts.js b/frontend/src/shared_code/consts.js index 9118a61..b69b5a4 100644 --- a/frontend/src/shared_code/consts.js +++ b/frontend/src/shared_code/consts.js @@ -1,15 +1,16 @@ -export const SERVER_ADDRESS = 'http://localhost'; -export const SERVER_PORT = '8080'; -export const ROOM_ROUTES = 'room'; +export const SERVER_ADDRESS = "http://localhost"; +export const SERVER_PORT = "3000"; +export const API_ROUTES = "api"; +export const ROOM_ROUTES = "room"; export const PLAYER_ACTIONS = { - generateUniqueID: { type: 'generateUniqueID' }, + generateUniqueID: { type: "generateUniqueID" }, }; export const ROOM_ACTIONS = { initRoom: (id, creationDate, socketPort, playersList) => { return { - type: 'initRoom', + type: "initRoom", data: { id, creationDate, @@ -18,17 +19,17 @@ export const ROOM_ACTIONS = { }, }; }, - updatePlayerList: (playersList) => { + updatePlayerList: playersList => { return { - type: 'updatePlayerList', + type: "updatePlayerList", data: { playersList, }, }; }, - setRoomID: (id) => { + setRoomID: id => { return { - type: 'setRoomId', + type: "setRoomId", data: { id }, }; },