Skip to content

Commit 56ded65

Browse files
author
Pythonicboat
committed
added app routes
1 parent daa5635 commit 56ded65

File tree

9 files changed

+91
-4
lines changed

9 files changed

+91
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
.github

.replit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run = "cd backend && gunicorn app:app"

backend/.config

Whitespace-only changes.

backend/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Use a lightweight Python image
2+
FROM python:3.9-slim-buster
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container at /app
8+
COPY . /app
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --no-cache-dir -r requirements.txt
12+
13+
# Expose the port Flask/Gunicorn will run on
14+
EXPOSE 8000
15+
16+
# Start Gunicorn and run the Flask app
17+
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"]
2.34 KB
Binary file not shown.

backend/app.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,79 @@
22
import os
33

44
from flask import Flask, request, jsonify, render_template, redirect, url_for
5+
from flask_sqlalchemy import SQLAlchemy
56
from web3 import Web3, HTTPProvider
67
import json
78
from sqlalchemy import create_engine
89
from sqlalchemy.orm import scoped_session, sessionmaker
910

10-
1111
app = Flask(__name__)
1212
app.config["TEMPLATES_AUTO_RELOAD"] = True
1313

1414
# Connect to the Ethereum network using Infura
15-
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID'))
15+
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/'))
1616

1717
# Load the contract ABI from a JSON file
1818
with open('contract_abi.json', 'r') as f:
1919
contract_abi = json.load(f)
2020

2121
# Connect to the contract
22-
contract_address = '0x6b175474e89094c44da98b954eedeac495271d0f'
2322
contract = w3.eth.contract(address=contract_address, abi=contract_abi)
2423

2524
# Connect to the database
2625
engine = create_engine(os.getenv("DATABASE_URL"))
2726
db = scoped_session(sessionmaker(bind=engine))
2827

28+
web3auth = web3auth(provider_uri="", contract_address="")
29+
30+
#Landing page
31+
@app.route('/')
32+
def landing_page():
33+
print("Here landing page will be shown in html")
34+
35+
36+
#Endpoint for new user
37+
@app.route('/login')
38+
def signin():
39+
address = request.json.get(address)
40+
if web3.is_signed_in(address):
41+
return jsonify({'error : user already signed in'}), 400
42+
43+
user_id = web3auth.is_sign_in(address)
44+
return jsonify({'user_id':user_id}), 200
45+
46+
#Endpoint for new registration
47+
@app.route('/register')
48+
def register():
49+
address = request.json.get('address')
50+
username = request.json.get('username')
51+
52+
if web3auth.is_signed_in(address):
53+
return jsonify({'error': 'User already signed in'}), 400
54+
55+
user_id = web3auth.register(address, username)
56+
return jsonify({'user_id': user_id}), 200
57+
58+
#View details of a product by ID
59+
@app.route("/products/<string:product_hash>", methods=['GET'])
60+
def get_product(product_hash):
61+
product_id = str(uuid.uuid5(uuid.NAMESPACE_URL, product_hash))
62+
product = contract.functions.getItemDetais(product_id).call()
63+
64+
return jsonify({'product':product})
65+
66+
# Endpoint to view details of a specific user identified by hash
67+
@app.route('/users/<string:user_hash>', methods=['GET'])
68+
def get_user(user_hash):
69+
# Query the smart contract to get the details of the specified user
70+
user_id = str(uuid.uuid5(uuid.NAMESPACE_URL, user_hash))
71+
user = contract.functions.getUserDetails(user_id).call()
72+
73+
# Return the user details as a JSON response
74+
return jsonify({'user': user})
75+
76+
2977

78+
if __name__ == "__main__":
79+
# gunicorn --bind 0.0.0.0:8000 app:app ---> use this to run in cmd
80+
app.run(debug=True)

backend/hash_id.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# Function
1+
# Function to convert string to hash
2+
import hashlib
3+
4+
# user ID would follow different format than products IDs
5+
6+
def sha256_hash(string):
7+
return hashlib.sha256(string.encode()).hexdigest()
8+
9+
# print(sha256_hash("Hello World")) --> a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

backend/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask

replit.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{ pkgs }: {
2+
deps = [
3+
pkgs.python39Packages.pip
4+
pkgs.python39Packages.gunicorn
5+
pkgs.python39Full
6+
];
7+
}

0 commit comments

Comments
 (0)