Code Monkey home page Code Monkey logo

todo-list-api's Introduction

todo-list-api

ToDo リストアプリのための REST API サーバです。

必要条件

このアプリケーションを実行するには以下の条件を満たしている必要があります。

  • Node.js がインストールされている
  • Yarn がインストールされている
  • MySQL がインストールされている(ローカル環境での実行に必要)
  • AWS SAM CLI がインストール(デプロイに必要)
  • AWS CLI がインストールされている(Cognito ユーザの作成に必要)

インストール

以下のコマンドを実行してください。

yarn

アプリケーションをローカル環境で実行する

このアプリケーションは DB の接続に環境変数を参照します。 .env というファイル名で以下の変数の値を設定したファイルを作成してください。 アプリケーション内部で .env ファイルを自動で読み込み、環境変数として利用します。

DB_HOST=<DB host>
DB_USERNAME=<DB username>
DB_PASSWORD=<Password for the $DB_USERNAME user>
DB_NAME=<DB name to connect>

アプリケーションを起動するには以下のコマンドを実行してください。 http://localhost:3000 からアプリケーションにアクセスできます。

# development
yarn start

# watch mode
yarn start:dev

# production mode
yarn start:prod

API 仕様

./docs/redoc-static.html をブラウザで開くことで API 仕様を確認することができます。 ./docs/swagger.json に OpenAPI 準拠の JSON ファイルを配置しているので、OpenAPI のビュワー等で API 仕様を確認することもできます。

また、yarn start を実行してアプリケーションを起動後、 http://localhost:3000/api にアクセスすることで、 Swagger UI で API 仕様を確認することもできます。

テスト

テストを実行するには以下のコマンドを実行してください。

yarn test

デプロイ

以下のコマンドを実行してください。

# build the application
yarn build

# build the AWS SAM templates
sam build

# deploy the application to AWS
sam deploy --guided

デプロイ完了後、Lambda 関数に対して、データベースプロキシを作成する必要があります。 AWS コンソールから以下の手順を実行してください。

  1. Lambda コンソールで [関数] ページを開きます。
  2. 関数を選択します。
  3. [設定][データベースプロキシ] の順にクリックします。
  4. [データベースプロキシの追加] を選択します。
  5. [既存のデータベースプロキシの選択] をチェックします。
  6. [既存のプロキシ] プルダウンから [todo-rds-proxy-for-db-cluster] を選択します。
  7. [追加] をクリックします。

Cognito ユーザの ID トークンの取得

デプロイしたアプリケーションを実行するには、Cognito でユーザ認証を行う必要があります。 そのために、Cognito ユーザを作成し、そのユーザの ID トークンを取得する必要があるので、その手順を解説します。

以下の Shell 変数が設定されている前提で説明します。

USER_POOL_ID=<Cognito user pool ID>
CLIENT_ID=<Cognito user pool client ID>
USERNAME=<Cognito username>
PASSWORD=<Password for the $USERNAME user>

USER_POOL_ID および CLIENT_ID に設定すべき値は sam deploy コマンドの実行結果で確認できます。

まずは、以下のコマンドで、ユーザをクライアントプールに登録します。

aws cognito-idp sign-up \
  --client-id ${CLIENT_ID} \
  --username ${USERNAME} \
  --password ${PASSWORD}

次に、以下のコマンドで、作成したユーザの確認を行います。

aws cognito-idp admin-confirm-sign-up \
  --user-pool-id ${USER_POOL_ID} \
  --username ${USERNAME}

最後に、以下のコマンドで ID トークンを取得します。

aws cognito-idp admin-initiate-auth \
  --user-pool-id ${USER_POOL_ID} \
  --client-id ${CLIENT_ID} \
  --auth-flow "ADMIN_USER_PASSWORD_AUTH" \
  --auth-parameters USERNAME=${USERNAME},PASSWORD=${PASSWORD} \
  --query "AuthenticationResult.IdToken" | sed s/\"//g

取得した ID トークンを HTTP リクエストの Authorization ヘッダに設定することで、ユーザ認証が行えます。

todo-list-api's People

Contributors

dependabot[bot] avatar tanimon avatar

Watchers

 avatar

todo-list-api's Issues

SAM テンプレートを整理する

  • API Gateway に対する Cognito 認証を復活させる
  • Cognito のテンプレートを切り出す
    - [ ] Serverless 系のテンプレートを切り出す

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.