11 Commits

11 changed files with 109 additions and 30 deletions
+2
View File
@@ -0,0 +1,2 @@
KEY= # Key for JWT token
POSTGRES_URL=postgresql://
+28
View File
@@ -0,0 +1,28 @@
from flask import Blueprint, request, jsonify
from model.user import User
auth = Blueprint("auth", __name__)
@auth.route('/login', methods = ['POST'])
def login():
if request.is_json:
req = request.json
email = req.get('email')
password = req.get('password')
if not email or not password:
return "Request must have email and password", 400
if len(email.strip()) < 4 or '@' not in email or '.' not in email:
return "Email is incorrect", 400
if len(password.strip()) < 8:
return "Password is too short", 400
user = User(email, password)
return jsonify(user.toJson())
else:
return "Request is not a json", 400
-12
View File
@@ -1,12 +0,0 @@
from flask import Blueprint, request, jsonify
from model.user import user
loginBP = Blueprint("loginapi", __name__)
@loginBP.route('/api/login', methods = ['POST'])
def login():
email = request.form['email']
password = request.form['password']
#if(isvalid(email, password)):
us = user(email, password)
return jsonify(us.toDictionary())
+24
View File
@@ -0,0 +1,24 @@
from flask import Blueprint, request, jsonify
from model.user import User
#from module.db.repositories.robot_reposytory import update_robot get_by_id
from loguru import logger as log
robots = Blueprint("robots", __name__)
robots.route('/data', methods = ['POST'])
def data():
if request.headers.get("Authorization"):
if request.is_json:
req = request.json
id = req.get('robot_id')
tms = req.get('timestamp')
loc = req.get('location')
scanRes = req.get('scan_results')
battery = req.get('battery_level')
if update_robot(id, "received", battery, loc["zone"], loc["row"], loc["self"]):
res = {"status": "received", "message_id": "123"} #for that moment i don`t now what is message
return jsonify(res)
else:
log.error('failed to update robot data')
return("Server error")
+26
View File
@@ -0,0 +1,26 @@
from app import app
from flask_socketio import SocketIO, emit
from loguru import logger as log
#from module.db.repositories.robot_reposytory import update_robot get_by_id get_all
#from module.db.repositories.inventory_repository import get_all as getRecords
ws = socketIO(app)
@ws.on('connect')
def is_connect():
log.info('client is connected')
@ws.on('disconnect')
def is_disconnect():
log.info('client is disconnected')
@ws.on('robot_update')
def robotUpdate():
robots = get_all()
emit('response', jsonify({"type": "robot_update", "data": {robots.toJSON()}})
@ws.on('inventory_alert')
def robotUpdate():
records = getRecords()
emit('response', jsonify({"type": "inventory_alert", "data": {records.toJSON()}})
+9 -3
View File
@@ -1,9 +1,15 @@
from sys import exit
from flask import Flask
from api.auth.loginapi import loginBP
from api.auth import auth
from utils.loadDotEnv import initializeENV
from utils.PostgressConnect import PSQLConnect, PSQLCursor
state = initializeENV()
if not initializeENV():
exit(-1)
#conn = PSQLConnect()
#cur = PSQLCursor(conn)
app = Flask(__name__)
app.register_blueprint(loginBP)
app.register_blueprint(auth, url_prefix='/api/auth')
+3 -1
View File
@@ -1,4 +1,6 @@
flask==3.1.2
flask-socketio
python-dotenv
psycopg-binary
psycopg2-binary
pyjwt
loguru
+2 -2
View File
@@ -3,7 +3,7 @@ import json
from utils.token import generateKey
@dataclass
class user:
class User:
id: int
name: str
role: str
@@ -16,5 +16,5 @@ class user:
self.role = 'Backend'#us['role']
self.token = generateKey(email, passwd)
def toDictionary(self):
def toJson(self):
return {"user": {"id": self.id, "name": self.name, "role": self.role}, "token": self.token}
+3 -3
View File
@@ -1,10 +1,10 @@
import psycopg
import psycopg2
import os
def PSQLConnect():
conn = psycopg.connect(os.getenv('POSTDRESS_CONNECTION'))
conn = psycopg2.connect(os.getenv('POSTGRES_URL'))
return conn
def PSQLCursor(conn):
cur = conn.cursor()
cur = conn.cursor()
return cur
+10 -8
View File
@@ -1,12 +1,14 @@
import os
from dotenv import load_dotenv
from loguru import logger as log
def initializeENV():
dotenv_path = '../.env'
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)
print('.env is loaded')
return 1
DOTENV_PATH = '.env'
def initializeENV() -> bool:
if os.path.exists(DOTENV_PATH):
load_dotenv(DOTENV_PATH)
log.info('.env is loaded')
return True
else:
print('.env isn`t loaded')
return 0
log.error('.env isn`t loaded')
return False
+2 -1
View File
@@ -1,7 +1,8 @@
import jwt
import os
from time import time
def generateKey(email, passwd):
key = os.getenv('KEY')
encoded = jwt.encode({email: passwd}, key, algorithm="HS256")
encoded = jwt.encode({email: passwd, 'iat': time()}, key, algorithm="HS256")
return encoded