init
This commit is contained in:
52
be_signup/signup_app.py
Normal file
52
be_signup/signup_app.py
Normal file
@ -0,0 +1,52 @@
|
||||
# signup_app.py
|
||||
from flask import Flask, request, jsonify
|
||||
import os
|
||||
import json
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DB_DIR = './data'
|
||||
DB_FILE = f'{DB_DIR}/db.json'
|
||||
|
||||
# ensure directory exists
|
||||
os.makedirs(DB_DIR, exist_ok=True)
|
||||
|
||||
# DB 초기화 함수
|
||||
def init_db():
|
||||
if not os.path.exists(DB_FILE):
|
||||
with open(DB_FILE, 'w') as f:
|
||||
json.dump([], f)
|
||||
else:
|
||||
try:
|
||||
with open(DB_FILE, 'r') as f:
|
||||
data = json.load(f)
|
||||
if not isinstance(data, list):
|
||||
raise ValueError
|
||||
except:
|
||||
with open(DB_FILE, 'w') as f:
|
||||
json.dump([], f)
|
||||
|
||||
@app.route('/signup', methods=['POST'])
|
||||
def signup():
|
||||
data = request.get_json()
|
||||
if not data or 'name' not in data:
|
||||
return jsonify({'error': 'Missing name or email'}), 400
|
||||
|
||||
with open(DB_FILE, 'r') as f:
|
||||
users = json.load(f)
|
||||
|
||||
users.append({'name': data['name'], 'email': data.get('email', '')})
|
||||
|
||||
with open(DB_FILE, 'w') as f:
|
||||
# json.dump(users, f)
|
||||
json.dump(users, f, ensure_ascii=False, indent=2)
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
# DB 초기화
|
||||
init_db()
|
||||
|
||||
if __name__ == '__main__':
|
||||
# init_db() # 서버 시작할 때 DB 초기화
|
||||
# app.run(host='127.0.0.1', port=5000)
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user