Xinqi04 commited on
Commit
04eb69e
·
1 Parent(s): bc3d868
Files changed (2) hide show
  1. Dockerfile +6 -6
  2. app.py +55 -66
Dockerfile CHANGED
@@ -1,17 +1,17 @@
 
1
  FROM python:3.10-slim
2
 
3
- # Set work directory
4
  WORKDIR /app
5
 
6
- # Install dependencies
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 (Hugging Face default is 7860)
14
  EXPOSE 7860
15
 
16
- # Run app using gunicorn
17
- CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
 
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
- from flask_cors import CORS
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
- # Ambil URL dan Key Supabase dari environment
16
- url: str = os.environ.get("SUPABASE_URL")
17
- key: str = os.environ.get("SUPABASE_KEY")
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Buat koneksi ke Supabase
20
- supabase: Client = create_client(url, key)
21
 
22
- # --- ROUTES API ---
 
 
 
 
23
 
24
- # [CREATE] Menambah barcode baru
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
- try:
32
- # Masukkan data ke tabel 'barcodes'
33
- response = supabase.table('barcodes').insert({
34
- 'barcode_value': data['barcode_value'],
35
- 'name': data['name'],
36
- 'price': data['price']
37
- }).execute()
38
 
39
- # Cek jika ada error dari Supabase
40
- if response.data:
41
- return jsonify(response.data[0]), 201
42
- else:
43
- return jsonify({"error": "Gagal menyimpan data"}), 500
44
- except Exception as e:
45
- return jsonify({"error": str(e)}), 500
46
 
47
- # [READ] Mendapatkan semua data barcode
48
- @app.route('/api/barcodes', methods=['GET'])
49
- def get_all_barcodes():
50
- try:
51
- response = supabase.table('barcodes').select('*').order('created_at', desc=True).execute()
52
- return jsonify(response.data), 200
53
- except Exception as e:
54
- return jsonify({"error": str(e)}), 500
55
 
56
- # [READ] Mendapatkan satu barcode berdasarkan nilainya
57
- @app.route('/api/barcodes/<string:barcode_value>', methods=['GET'])
58
- def get_barcode_by_value(barcode_value):
59
- try:
60
- # Cari barcode yang cocok
61
- response = supabase.table('barcodes').select('*').eq('barcode_value', barcode_value).single().execute()
62
- if response.data:
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
- # [DELETE] Menghapus barcode berdasarkan nilainya
70
- @app.route('/api/barcodes/<string:barcode_value>', methods=['DELETE'])
71
- def delete_barcode(barcode_value):
72
- try:
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__ == '__main__':
83
- app.run(debug=True, port=5000)
 
 
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)