Spaces:
Sleeping
Sleeping
Xinqi04 commited on
Commit ·
04eb69e
1
Parent(s): bc3d868
baru
Browse files- Dockerfile +6 -6
- app.py +55 -66
Dockerfile
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
|
|
| 1 |
FROM python:3.10-slim
|
| 2 |
|
| 3 |
-
# Set
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
#
|
| 7 |
COPY requirements.txt .
|
| 8 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
|
| 10 |
-
# Copy all files
|
| 11 |
COPY . .
|
| 12 |
|
| 13 |
-
# Expose port
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
-
# Run
|
| 17 |
-
CMD ["
|
|
|
|
| 1 |
+
# Use official Python base image
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
+
# Set working directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy files
|
| 8 |
COPY requirements.txt .
|
| 9 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
|
|
|
|
| 11 |
COPY . .
|
| 12 |
|
| 13 |
+
# Expose the port for HF Spaces
|
| 14 |
EXPOSE 7860
|
| 15 |
|
| 16 |
+
# Run Flask app
|
| 17 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,83 +1,72 @@
|
|
| 1 |
-
import os
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from supabase import create_client, Client
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
|
| 7 |
-
# Muat environment variables dari file .env
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# Inisialisasi aplikasi Flask
|
| 11 |
app = Flask(__name__)
|
| 12 |
-
# Aktifkan CORS untuk mengizinkan request dari frontend (React)
|
| 13 |
-
CORS(app)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
@app.route('/api/barcodes', methods=['POST'])
|
| 26 |
-
def add_barcode():
|
| 27 |
-
data = request.get_json()
|
| 28 |
-
if not data or 'barcode_value' not in data or 'name' not in data or 'price' not in data:
|
| 29 |
-
return jsonify({"error": "Data tidak lengkap"}), 400
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
'price': data['price']
|
| 37 |
-
}).execute()
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
@app.route(
|
| 49 |
-
def
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
@app.route(
|
| 58 |
-
def
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
return jsonify(response.data), 200
|
| 64 |
-
else:
|
| 65 |
-
return jsonify({"error": "Barcode tidak ditemukan"}), 404
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return jsonify({"error": str(e)}), 500
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
@app.route(
|
| 71 |
-
def
|
| 72 |
-
|
| 73 |
-
response = supabase.table('barcodes').delete().eq('barcode_value', barcode_value).execute()
|
| 74 |
-
if response.data:
|
| 75 |
-
return jsonify({"message": "Barcode berhasil dihapus"}), 200
|
| 76 |
-
else:
|
| 77 |
-
# Mungkin data tidak ditemukan atau ada error lain
|
| 78 |
-
return jsonify({"error": "Gagal menghapus atau barcode tidak ditemukan"}), 404
|
| 79 |
-
except Exception as e:
|
| 80 |
-
return jsonify({"error": str(e)}), 500
|
| 81 |
|
| 82 |
-
if __name__ ==
|
| 83 |
-
app.run(
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from supabase import create_client, Client
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
|
|
|
|
| 8 |
app = Flask(__name__)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 11 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 12 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 13 |
+
|
| 14 |
+
TABLE_NAME = "barcodes"
|
| 15 |
+
|
| 16 |
+
# --- CREATE Barcode ---
|
| 17 |
+
@app.route("/barcodes", methods=["POST"])
|
| 18 |
+
def create_barcode():
|
| 19 |
+
data = request.json
|
| 20 |
+
name = data.get("name")
|
| 21 |
+
code = data.get("code")
|
| 22 |
+
price = data.get("price")
|
| 23 |
|
| 24 |
+
if not all([name, code, price]):
|
| 25 |
+
return jsonify({"error": "Missing fields"}), 400
|
| 26 |
|
| 27 |
+
result = supabase.table(TABLE_NAME).insert({
|
| 28 |
+
"name": name,
|
| 29 |
+
"code": code,
|
| 30 |
+
"price": price
|
| 31 |
+
}).execute()
|
| 32 |
|
| 33 |
+
return jsonify(result.data), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# --- READ All Barcodes ---
|
| 36 |
+
@app.route("/barcodes", methods=["GET"])
|
| 37 |
+
def get_barcodes():
|
| 38 |
+
result = supabase.table(TABLE_NAME).select("*").execute()
|
| 39 |
+
return jsonify(result.data), 200
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# --- READ One Barcode by Code ---
|
| 42 |
+
@app.route("/barcodes/<code>", methods=["GET"])
|
| 43 |
+
def get_barcode(code):
|
| 44 |
+
result = supabase.table(TABLE_NAME).select("*").eq("code", code).execute()
|
| 45 |
+
if not result.data:
|
| 46 |
+
return jsonify({"error": "Barcode not found"}), 404
|
| 47 |
+
return jsonify(result.data[0]), 200
|
| 48 |
|
| 49 |
+
# --- UPDATE Barcode ---
|
| 50 |
+
@app.route("/barcodes/<code>", methods=["PUT"])
|
| 51 |
+
def update_barcode(code):
|
| 52 |
+
data = request.json
|
| 53 |
+
result = supabase.table(TABLE_NAME).update(data).eq("code", code).execute()
|
| 54 |
+
if not result.data:
|
| 55 |
+
return jsonify({"error": "Barcode not found"}), 404
|
| 56 |
+
return jsonify(result.data[0]), 200
|
| 57 |
|
| 58 |
+
# --- DELETE Barcode ---
|
| 59 |
+
@app.route("/barcodes/<code>", methods=["DELETE"])
|
| 60 |
+
def delete_barcode(code):
|
| 61 |
+
result = supabase.table(TABLE_NAME).delete().eq("code", code).execute()
|
| 62 |
+
if not result.data:
|
| 63 |
+
return jsonify({"error": "Barcode not found"}), 404
|
| 64 |
+
return jsonify({"message": "Deleted successfully"}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# --- Root Route for Testing ---
|
| 67 |
+
@app.route("/", methods=["GET"])
|
| 68 |
+
def home():
|
| 69 |
+
return "Barcode API is running"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
app.run(host="0.0.0.0", port=7860)
|