{"id":28217,"date":"2025-02-10T09:19:09","date_gmt":"2025-02-10T00:19:09","guid":{"rendered":"https:\/\/www.skyarch.net\/blog\/?p=28217"},"modified":"2025-02-10T19:35:06","modified_gmt":"2025-02-10T10:35:06","slug":"chalice%e3%81%a8mysql%e3%82%92%e3%83%ad%e3%83%bc%e3%82%ab%e3%83%ab%e7%92%b0%e5%a2%83%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/chalice%e3%81%a8mysql%e3%82%92%e3%83%ad%e3%83%bc%e3%82%ab%e3%83%ab%e7%92%b0%e5%a2%83%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f\/","title":{"rendered":"Chalice\u3068MySQL\u3092\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u52d5\u304b\u3057\u3066\u307f\u305f"},"content":{"rendered":"<h1>\u306f\u3058\u3081\u306b<\/h1>\n<p>\u696d\u52d9\u306e\u4e2d\u3067\u3001Chalice\u3068MySQL\u3092\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u691c\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002<br \/>\n\u4eca\u56de\u306fSQLAlchemy+PyMySQL\u3092\u5229\u7528\u3057\u305f\u30c7\u30e2\u7528\u30b3\u30fc\u30c9\u3092\u7528\u3044\u3066\u3001\u305d\u306e\u969b\u306e\u624b\u9806\u3092\u3054\u5171\u6709\u51fa\u6765\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u76ee\u6b21<\/h2>\n<ul>\n<li><a href=\"#env\">\u691c\u8a3c\u74b0\u5883<\/a><\/li>\n<li><a href=\"#file\">\u4eca\u56de\u306e\u30d5\u30a1\u30a4\u30eb\u69cb\u6210<\/a><\/li>\n<li><a href=\"#whtchalice\">Chalice\u3068\u306f\uff1f<\/a><\/li>\n<li><a href=\"#mysql\">\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u306bMySQL\u30b3\u30f3\u30c6\u30ca\u3092\u7acb\u3066\u308b<\/a><\/li>\n<li><a href=\"#implement\">Chalice\u5074\u306e\u5b9f\u88c5<\/a><\/li>\n<li><a href=\"#vertification\">\u52d5\u4f5c\u691c\u8a3c<\/a><\/li>\n<li><a href=\"#conclusion\">\u307e\u3068\u3081<\/a><\/li>\n<\/ul>\n<h2 id=\"env\">\u691c\u8a3c\u74b0\u5883<\/h2>\n<ul>\n<li>macOS\uff1aM2<\/li>\n<li>Docker\uff1a27.4.0<\/li>\n<li>Docker Compose\uff1av2.31.0-desktop.2<\/li>\n<li>Chalice\uff1a1.31.3<\/li>\n<li>Python\uff1a3.12.8<\/li>\n<\/ul>\n<h2 id=\"file\">\u4eca\u56de\u306e\u30d5\u30a1\u30a4\u30eb\u69cb\u6210<\/h2>\n<p>\u30d5\u30a1\u30a4\u30eb\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<pre><code>.\n\u251c\u2500\u2500 docker-compose.yaml\n\u251c\u2500\u2500 local-test\n\u2502   \u251c\u2500\u2500 .chalice\n\u2502   \u2502   \u2514\u2500\u2500 config.json\n\u2502   \u251c\u2500\u2500 app.py\n\u2502   \u251c\u2500\u2500 database.py\n\u2502   \u251c\u2500\u2500 models\n\u2502   \u2502   \u2514\u2500\u2500 user.py\n\u2502   \u251c\u2500\u2500 repositories\n\u2502   \u2502   \u2514\u2500\u2500 user_repository.py\n\u2502   \u251c\u2500\u2500 requirements.txt\n\u2502   \u251c\u2500\u2500 services\n\u2502   \u2502   \u2514\u2500\u2500 user_service.py\n\u2502   \u2514\u2500\u2500 util.py\n\u2514\u2500\u2500 mysql\n    \u251c\u2500\u2500 DB\n    \u2502   \u2514\u2500\u2500 init.sql\n    \u2514\u2500\u2500 my.cnf\n<\/code><\/pre>\n<h2 id=\"whtchalice\">Chalice\u3068\u306f\uff1f<\/h2>\n<p>AWS\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<br \/>\n\u4eca\u56de\u306fChalice\u3092\u7528\u3044\u3066\u3001\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067API\u3092\u69cb\u7bc9\u3057\u307e\u3059\u3002<\/p>\n<h2 id=\"mysql\">\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u306bMySQL\u30b3\u30f3\u30c6\u30ca\u3092\u7acb\u3066\u308b<\/h2>\n<p>Docker Compose\u3092\u4f7f\u7528\u3057\u3066\u3001MySQL\u30b3\u30f3\u30c6\u30ca\u3092\u7acb\u3066\u307e\u3059\u3002<\/p>\n<h3>MySQL\u521d\u671f\u30c7\u30fc\u30bf\u3092\u914d\u7f6e<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4f5c\u6210\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/mysql\/DB\/init.sql<\/code><\/p>\n<pre><code>-- \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u4f5c\u6210\nCREATE DATABASE IF NOT EXISTS local_test;\nUSE local_test;\n\n-- users \u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\nCREATE TABLE IF NOT EXISTS users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL UNIQUE,\n    email VARCHAR(100) NOT NULL UNIQUE,\n    password_hash VARCHAR(255) NOT NULL,\n    created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);<\/code><\/pre>\n<h3>MySQL\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u914d\u7f6e<\/h3>\n<p>Path\uff1a<code>.\/mysql\/my.cnf<\/code><\/p>\n<pre>\n[mysqld]\ncharacter-set-server=utf8mb4\ncollation_server = utf8mb4_general_ci\ntransaction-isolation = READ-COMMITTED\n\n[mysql]\ndefault-character-set=utf8mb4\n\n[client]\ndefault-character-set=utf8mb4\n<\/pre>\n<h3>MySQL\u30b3\u30f3\u30c6\u30ca\u3092\u4f5c\u6210<\/h3>\n<h4>docker-compose.yaml\u3092\u4f5c\u6210<\/h4>\n<p>\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u3067\u3001\u30b3\u30f3\u30c6\u30ca\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/docker-compose.yaml<\/code><\/p>\n<pre><code>volumes:\n  mysql_data:\nservices:\n  mysql:\n    image: mysql:8.0.41\n    container_name: mysql_container\n    environment:\n      MYSQL_ROOT_PASSWORD: root\n      MYSQL_DATABASE: local_test\n      MYSQL_USER: local_test_user\n      MYSQL_PASSWORD: local_test_password\n    ports:\n      - &quot;3306:3306&quot;\n    volumes:\n      - mysql_data:\/var\/lib\/mysql\n      - .\/mysql\/my.cnf:\/etc\/mysql\/conf.d\/my.cnf\n      - .\/mysql\/DB:\/docker-entrypoint-initdb.d<\/code><\/pre>\n<h4>MySQL\u30b3\u30f3\u30c6\u30ca\u306e\u7acb\u3061\u4e0a\u3052<\/h4>\n<pre><code>docker-compose up -d<\/code><\/pre>\n<h2 id=\"implement\">Chalice\u5074\u306e\u5b9f\u88c5<\/h2>\n<h3>Chalice\u3068\u305d\u306e\u4ed6\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<h4>requirements.txt\u3092\u4f5c\u6210<\/h4>\n<p>\u4eca\u56de\u4f5c\u6210\u3059\u308brequirements.txt\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/requirements.txt<\/code><\/p>\n<pre>\nchalice==1.31.3\nsqlalchemy==2.0.37\npymysql==1.1.1\ncryptography==44.0.0\nwerkzeug==3.1.3\n<\/pre>\n<h4>\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h4>\n<p>requirements.txt\u304b\u3089\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre><code>pip install -r requirements.txt<\/code><\/pre>\n<h3>Chalice\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210<\/h3>\n<p>\u65b0\u898f\u306eChalice\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306flocal-test\u3068\u3044\u3046\u540d\u524d\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code>chalice new-project local-test<\/code><\/pre>\n<h4>tips<\/h4>\n<p>Chalice\u306e\u30b3\u30fc\u30c9\u4e2d\u306bboto3\u3092\u4f7f\u7528\u3059\u308b\u8a18\u8ff0\u304c\u3042\u308b\u3068\u3001Chalice\u3092\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u4f7f\u7528\u3059\u308b\u969b\u306b\u3082\u3001<br \/>\n<code>chalice local<\/code>\u5b9f\u884c\u6642\u306b~\/.aws\/credentials\u3092\u78ba\u8a8d\u3057\u3066\u3001AWS\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u3067\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3060\u3051\u3067Chalice\u3092\u4f7f\u7528\u3059\u308b\u306e\u3067\u3001\u8a8d\u8a3c\u60c5\u5831\u306e\u8a2d\u5b9a\u306f\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>config.json\u3092\u8a2d\u5b9a<\/h3>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306bconfig.json\u3092\u66f8\u304d\u63db\u3048\u307e\u3059\u3002<br \/>\n\u74b0\u5883\u5909\u6570\u3068\u3057\u3066\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u60c5\u5831\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/.chalice\/config.json<\/code><\/p>\n<pre>\n{\n  \"version\": \"2.0\",\n  \"app_name\": \"local-test\",\n  \"stages\": {\n    \"local\": {\n      \"environment_variables\": {\n        \"DB_HOST\": \"localhost\",\n        \"DB_DATABASE\": \"local_test\",\n        \"DB_USERNAME\": \"local_test_user\",\n        \"DB_PASSWORD\": \"local_test_password\"\n      }\n    }\n  }\n}\n<\/pre>\n<h3>\u691c\u8a3c\u7528\u306e\u30b3\u30fc\u30c9\u3092\u4f5c\u6210<\/h3>\n<p>\u4eca\u56de\u306e\u691c\u8a3c\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u306a\u3069\u3092\u60f3\u5b9a\u3057\u305f\u30c7\u30e2\u7528\u30b3\u30fc\u30c9\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<h4>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a<\/h4>\n<p>Path\uff1a<code>.\/local-test\/database.py<\/code><\/p>\n<pre>\nimport os\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import scoped_session, sessionmaker\n\n# \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u60c5\u5831\u3092chalice\u306econfig.json\u304b\u3089\u53d6\u5f97\nDB_USERNAME = os.environ[\"DB_USERNAME\"]\nDB_PASSWORD = os.environ[\"DB_PASSWORD\"]\nDB_HOST = os.environ[\"DB_HOST\"]\nDB_DATABASE = os.environ[\"DB_DATABASE\"]\n\n# \u63a5\u7d9a\u5148DB\u306e\u8a2d\u5b9a\nDATABASE = f\"mysql+pymysql:\/\/{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}\/{DB_DATABASE}?charset=utf8mb4\"\n\nEngine = create_engine(DATABASE, pool_pre_ping=True, echo=True, pool_recycle=28500)\nBase = declarative_base()\n\n# Session\u306e\u4f5c\u6210\nsession = scoped_session(\n    sessionmaker(\n        autoflush=False, bind=Engine, expire_on_commit=False\n    )\n)\n<\/pre>\n<h4>\u30e2\u30c7\u30eb\u5b9a\u7fa9<\/h4>\n<p>User\u30e2\u30c7\u30eb\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/models\/user.py<\/code><\/p>\n<pre>\nfrom sqlalchemy import Column, Integer, String, DateTime\nfrom database import Base\nfrom datetime import datetime\nfrom werkzeug.security import generate_password_hash, check_password_hash\n\nclass User(Base):\n    __tablename__ = 'users'\n\n    id = Column(Integer, primary_key=True, autoincrement=True)\n    username = Column(String(50), nullable=False, unique=True)\n    email = Column(String(100), nullable=False, unique=True)\n    password_hash = Column(String(255), nullable=False)\n    created_at = Column(DateTime, default=datetime.utcnow)\n\n    def set_password(self, password):\n        #\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30cf\u30c3\u30b7\u30e5\u5316\n        self.password_hash = generate_password_hash(password)\n\n    def check_password(self, password):\n        #\u5165\u529b\u3055\u308c\u305f\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u30cf\u30c3\u30b7\u30e5\u3068\u4e00\u81f4\u3059\u308b\u304b\u78ba\u8a8d\n        return check_password_hash(self.password_hash, password)\n<\/pre>\n<h4>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u60c5\u5831\u306e\u53d6\u5f97\u30fb\u66f4\u65b0\u51e6\u7406<\/h4>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u3084\u308a\u3068\u308a\u306b\u3064\u3044\u3066\u3001\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/repositories\/user_repository.py<\/code><\/p>\n<pre>\nfrom database import session\nfrom models.user import User\n\ndef get_user_by_email(email: str):\n    #\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3067\u30e6\u30fc\u30b6\u30fc\u3092\u691c\u7d22\n    user_by_email = session.query(\n        User\n    ).filter(\n        User.email == email\n    ).first()\n\n    return user_by_email\n\ndef create_user(username: str, email: str, password: str):\n    #\u65b0\u3057\u3044\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\uff08commit \u306f\u3057\u306a\u3044)\n    new_user = User(username=username, email=email)\n    new_user.set_password(password)\n    session.add(new_user)\n    session.flush()  \n    session.refresh(new_user)\n    return new_user\n\ndef get_user_by_id(user_id: int):\n    #ID \u3067\u30e6\u30fc\u30b6\u30fc\u3092\u691c\u7d22\n    user_by_id = session.query(\n        User\n    ).filter(\n        User.id == user_id\n    ).first()\n\n    return user_by_id\n<\/pre>\n<h4>\u30ed\u30b8\u30c3\u30af<\/h4>\n<p>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/services\/user_service.py<\/code><\/p>\n<pre>\nfrom repositories.user_repository import get_user_by_email, create_user, get_user_by_id\nfrom util import transactional, session_managed\nfrom chalice import Response\nimport json\n\n@transactional\ndef register_user(username: str, email: str, password: str):\n    if not username or not email or not password:\n        return Response(\n            body=json.dumps({\"error\": \"\u3059\u3079\u3066\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\"}, ensure_ascii=False),\n            status_code=400,\n            headers={'Content-Type': 'application\/json; charset=utf-8'}\n        )\n\n    if get_user_by_email(email):\n        return Response(\n            body=json.dumps({\"error\": \"\u3053\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306f\u65e2\u306b\u767b\u9332\u3055\u308c\u3066\u3044\u307e\u3059\"}, ensure_ascii=False),\n            status_code=400,\n            headers={'Content-Type': 'application\/json; charset=utf-8'}\n        )\n\n    new_user = create_user(username, email, password)\n    return Response(\n        body=json.dumps({\"message\": \"\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\", \"user_id\": new_user.id}, ensure_ascii=False),\n        status_code=201,\n        headers={'Content-Type': 'application\/json; charset=utf-8'}\n    )\n\n@session_managed\ndef login_user(email: str, password: str):\n    if not email or not password:\n        return Response(\n            body=json.dumps({\"error\": \"\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\"}, ensure_ascii=False),\n            status_code=400,\n            headers={'Content-Type': 'application\/json; charset=utf-8'}\n        )\n\n    user = get_user_by_email(email)\n    if user is None or not user.check_password(password):\n        return Response(\n            body=json.dumps({\"error\": \"\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u307e\u305f\u306f\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\"}, ensure_ascii=False),\n            status_code=401,\n            headers={'Content-Type': 'application\/json; charset=utf-8'}\n        )\n\n    return Response(\n        body=json.dumps({\"message\": \"\u30ed\u30b0\u30a4\u30f3\u6210\u529f\", \"user_id\": user.id}, ensure_ascii=False),\n        status_code=200,\n        headers={'Content-Type': 'application\/json; charset=utf-8'}\n    )\n\n@session_managed\ndef get_user_details(user_id: int):\n    user = get_user_by_id(user_id)\n\n    if not user:\n        return Response(\n            body=json.dumps({\"error\": \"\u30e6\u30fc\u30b6\u30fc\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\"}, ensure_ascii=False),\n            status_code=404,\n            headers={'Content-Type': 'application\/json; charset=utf-8'}\n        )\n\n    return Response(\n        body=json.dumps({\n            \"id\": user.id,\n            \"username\": user.username,\n            \"email\": user.email,\n            \"created_at\": user.created_at.isoformat()\n        }, ensure_ascii=False),\n        status_code=200,\n        headers={'Content-Type': 'application\/json; charset=utf-8'}\n    )\n<\/pre>\n<h4>\u30bb\u30c3\u30b7\u30e7\u30f3\u7ba1\u7406<\/h4>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u3001\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u30ed\u30b8\u30c3\u30af\u306e\u90e8\u5206\u3067\u4f7f\u7528\u3057\u307e\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/util.py<\/code><\/p>\n<pre>\nfrom database import session\nfrom functools import wraps\n\ndef transactional(func):\n    \"\"\"\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068 session \u3092\u81ea\u52d5\u7684\u306b\u7ba1\u7406\u3059\u308b\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\"\"\"\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        try:\n            result = func(*args, **kwargs)  # \u95a2\u6570\u306e\u5b9f\u884c\n            session.commit()  # \u6b63\u5e38\u7d42\u4e86\u6642\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u30b3\u30df\u30c3\u30c8\n            return result\n        except Exception as e:\n            session.rollback()  # \u4f8b\u5916\u767a\u751f\u6642\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\n            raise e\n        finally:\n            session.remove()  # \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\n    return wrapper\n\ndef session_managed(func):\n    \"\"\"\u8aad\u307f\u53d6\u308a\u5c02\u7528\u64cd\u4f5c\u306e\u305f\u3081\u306e session \u7ba1\u7406\u30c7\u30b3\u30ec\u30fc\u30bf\u30fc\"\"\"\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        try:\n            return func(*args, **kwargs)\n        finally:\n            session.remove()  # \u8aad\u307f\u53d6\u308a\u64cd\u4f5c\u3067\u3082\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u89e3\u653e\n    return wrapper\n\n<\/pre>\n<h4>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u51e6\u7406<\/h4>\n<p>\u3069\u306e\u3088\u3046\u306a\u30ea\u30af\u30a8\u30b9\u30c8\u306b\u5bfe\u3057\u3066\u3001\u3069\u306e\u3088\u3046\u306a\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u8fd4\u3059\u304b\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nChalice\u3067\u5b9f\u884c\u3055\u308c\u308b\u30e1\u30a4\u30f3\u3068\u306a\u308b\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002<br \/>\nPath\uff1a<code>.\/local-test\/app.py<\/code><\/p>\n<pre>\nfrom chalice import Chalice\nfrom services.user_service import register_user, login_user, get_user_details\n\napp = Chalice(app_name=\"local-test\")\n\n@app.route('\/register', methods=['POST'])\ndef register():\n\n    #\u30ea\u30af\u30a8\u30b9\u30c8\u60c5\u5831\u53d6\u5f97\n    request = app.current_request.json_body\n    username = request.get('username')\n    email = request.get('email')\n    password = request.get('password')\n\n    #\u30ec\u30b9\u30dd\u30f3\u30b9\u53d6\u5f97\n    response_object = register_user(username, email, password)\n\n    return response_object\n\n@app.route('\/login', methods=['POST'])\ndef login():\n\n    #\u30ea\u30af\u30a8\u30b9\u30c8\u60c5\u5831\u53d6\u5f97\n    request = app.current_request.json_body\n    email = request.get('email')\n    password = request.get('password')\n\n    #\u30ec\u30b9\u30dd\u30f3\u30b9\u53d6\u5f97\n    response_object = login_user(email, password)\n\n    return response_object\n\n@app.route('\/user\/{user_id}', methods=['GET'])\ndef get_user(user_id):\n\n    #\u30e6\u30fc\u30b6\u30fcID\u53d6\u5f97\n    user_id = int(user_id)\n\n    #\u30ec\u30b9\u30dd\u30f3\u30b9\u53d6\u5f97\n    response_object = get_user_details(user_id)\n\n    return response_object\n\n<\/pre>\n<h2 id=\"vertification\">\u52d5\u4f5c\u691c\u8a3c<\/h2>\n<h3>\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067Chalice\u3092\u8d77\u52d5<\/h3>\n<p>Chalice\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002\u30b9\u30c6\u30fc\u30b8\u3092\u6307\u5b9a\u305b\u305a\u306b<code>chalice local<\/code>\u3060\u3051\u3067\u5b9f\u884c\u3059\u308b\u3068\u3001<br \/>\nconfig.json\u3067\u6307\u5b9a\u3057\u305f\u74b0\u5883\u5909\u6570\u304c\u8aad\u307f\u8fbc\u307e\u308c\u306a\u3044\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<br \/>\n\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u3001<code>http:\/\/127.0.0.1:8000<\/code>\u3067\u30db\u30b9\u30c8\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>\nchalice local --stage local\n<\/pre>\n<h3>curl\u30b3\u30de\u30f3\u30c9\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u3063\u3066\u307f\u308b<\/h3>\n<h4>\u30e6\u30fc\u30b6\u30fc\u306e\u767b\u9332<\/h4>\n<p><strong>\u30ea\u30af\u30a8\u30b9\u30c8<\/strong><\/p>\n<pre>\ncurl -X POST http:\/\/localhost:8000\/register \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\n        \"username\": \"johndoe\",\n        \"email\": \"johndoe@example.com\",\n        \"password\": \"securepassword\"\n      }'\n<\/pre>\n<p><strong>\u30ec\u30b9\u30dd\u30f3\u30b9<\/strong><\/p>\n<pre>{\"message\": \"\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\", \"user_id\": 1}<\/pre>\n<h4>\u30ed\u30b0\u30a4\u30f3<\/h4>\n<p><strong>\u30ea\u30af\u30a8\u30b9\u30c8<\/strong><\/p>\n<pre>\ncurl -X POST http:\/\/localhost:8000\/login \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\n        \"email\": \"johndoe@example.com\",\n        \"password\": \"securepassword\"\n      }'\n<\/pre>\n<p><strong>\u30ec\u30b9\u30dd\u30f3\u30b9<\/strong><\/p>\n<pre>{\"message\": \"\u30ed\u30b0\u30a4\u30f3\u6210\u529f\", \"user_id\": 1}<\/pre>\n<h4>\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u53d6\u5f97<\/h4>\n<p><strong>\u30ea\u30af\u30a8\u30b9\u30c8<\/strong><\/p>\n<pre>\n curl -X GET http:\/\/localhost:8000\/user\/1\n<\/pre>\n<p><strong>\u30ec\u30b9\u30dd\u30f3\u30b9<\/strong><\/p>\n<pre>{\"id\": 1, \"username\": \"johndoe\", \"email\": \"johndoe@example.com\", \"created_at\": \"2025-02-02T04:07:30\"}<\/pre>\n<h2 id=\"conclusion\">\u307e\u3068\u3081<\/h2>\n<p>\u4ee5\u4e0a\u3001\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067Chalice\u3068MySQL\u3092\u9023\u643a\u3059\u308b\u30c7\u30e2\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<br \/>\n\u30ab\u30b8\u30e5\u30a2\u30eb\u306b\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067API\u3092\u69cb\u7bc9\u3057\u3066\u3001\u8a66\u884c\u932f\u8aa4\u3067\u304d\u308b\u70b9\u306f\u3068\u3066\u3082\u4fbf\u5229\u3060\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u307f\u306a\u3055\u307e\u306e\u958b\u767a\u30a2\u30a4\u30c7\u30a2\u3092\u5f62\u306b\u3059\u308b\u969b\u306b\u3001\u672c\u8a18\u4e8b\u304c\u5c11\u3057\u3067\u3082\u304a\u5f79\u306b\u7acb\u3066\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u696d\u52d9\u306e\u4e2d\u3067\u3001Chalice\u3068MySQL\u3092\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u691c\u8a3c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002 \u4eca\u56de\u306fSQLAlchemy+PyMySQL\u3092\u5229\u7528\u3057\u305f\u30c7\u30e2\u7528\u30b3\u30fc\u30c9\u3092\u7528\u3044\u3066\u3001\u305d\u306e\u969b\u306e\u624b\u9806\u3092\u3054\u5171\u6709\u51fa\u6765\u308c\u3070\u3068\u601d\u3044\u307e\u3059\u3002 \u76ee\u6b21 \u691c\u8a3c\u74b0&#8230;<\/p>\n","protected":false},"author":232,"featured_media":28278,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/www.skyarch.net\/blog\/?p=28217","footnotes":""},"categories":[20,276],"tags":[1327,94,103],"class_list":{"0":"post-28217","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-aws","8":"category-serverless","9":"tag-chalice","10":"tag-docker","11":"tag-mysql","12":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/28217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/232"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=28217"}],"version-history":[{"count":68,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/28217\/revisions"}],"predecessor-version":[{"id":28294,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/28217\/revisions\/28294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/28278"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=28217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=28217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=28217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}