์ํ์นด๊ฐ ๋ชจ์ฌ ๊ณต๊ฐํด์ฃผ๊ณ ์ฆ๊ฒ๊ฒ ์๊ธฐํ๋ ๊ณต๊ฐ
- macOS 11.5
- Git 2.32
- Node 16 Alpine
- Yarn berry
- Visual Studio Code 1.63
- PostgreSQL 14 Alpine
- Docker 20.10
- Docker Compose 1.29
git --version
node --version
yarn --version
code --version
docker --version
docker-compose --version
์ ๋ช ๋ น์ด๋ฅผ ํตํด ํ๋ก์ ํธ์ ํ์ํ ๋ชจ๋ ํ๋ก๊ทธ๋จ์ด ์ค์น๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
git clone https://github.com/rmfpdlxmtidl/alpacasalon-backend.git
cd alpacasalon-backend
git checkout main
yarn
ํ๋ก์ ํธ๋ฅผ ๋ค์ด๋ก๋ ๋ฐ๊ณ ํด๋น ํด๋๋ก ์ด๋ํ ํ ์ ์ ํ ๋ธ๋์น(main
๋ฑ)๋ก ์ด๋ํ๊ณ ํ๋ก์ ํธ์ ํ์ํ ์ธ๋ถ ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ํ๋ก์ ํธ ํด๋์์ VSCode๋ฅผ ์คํํ๋ฉด ์ค๋ฅธ์ชฝ ์๋์ '๊ถ์ฅ ํ์ฅ ํ๋ก๊ทธ๋จ ์ค์น' ์๋ฆผ์ด ๋จ๋๋ฐ, ํ๋ก์ ํธ์์ ๊ถ์ฅํ๋ ํ์ฅ ํ๋ก๊ทธ๋จ(ESLint, Prettier ๋ฑ)์ ๋ชจ๋ ์ค์นํฉ๋๋ค.
๋ฃจํธ ํด๋์ .env
, .env.development
, .env.development.local
, .env.local
, .env.test
ํ์ผ์ ์์ฑํ๊ณ ํ๋ก์ ํธ์์ ์ฌ์ฉ๋๋ ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํฉ๋๋ค.
yarn import ์ต์
๊ทธ๋ฆฌ๊ณ import
์คํฌ๋ฆฝํธ๋ฅผ ์คํํด database/initialization.sql
์ CSV ํ์ผ๋ก ๋์ด ์๋ ๋๋ฏธ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ค๋๋ค.
$ yarn dev
TypeScript ํ์ผ์ ๊ทธ๋๋ก ์ฌ์ฉํด Nodemon์ผ๋ก ์๋น์ค๋ฅผ ์คํํฉ๋๋ค.
or
$ yarn build && yarn start
TypeScript ํ์ผ์ JavaScript๋ก ํธ๋์คํ์ผํ ํ Node.js๋ก ์๋น์ค๋ฅผ ์คํํฉ๋๋ค.
or
$ docker-compose up --detach --build --force-recreate
(Cloud Run ํ๊ฒฝ๊ณผ ๋์ผํ) Docker ํ๊ฒฝ์์ Node.js ์๋ฒ๋ฅผ ์คํํฉ๋๋ค.
Cloud Run + Cloud Build๋ฅผ ํตํด GitHub์ commit์ด push๋ ๋๋ง๋ค Cloud Run์ ์๋์ผ๋ก ๋ฐฐํฌํฉ๋๋ค.
CREATE USER alpacasalon CREATEDB;
-- \c postgres alpacasalon
CREATE DATABASE alpacasalon OWNER alpacasalon TEMPLATE template0 LC_COLLATE "C" LC_CTYPE "ko_KR.UTF-8";
-- \c alpacasalon postgres
-- ALTER SCHEMA public OWNER TO alpacasalon;
PROJECT_NAME=ํ๋ก์ ํธID
CONNECTION_NAME=$PROJECT_NAME:๋ฆฌ์ :์ธ์คํด์คID
gcloud auth login
gcloud config set project $PROJECT_NAME
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
chmod +x cloud_sql_proxy
./cloud_sql_proxy -instances=$CONNECTION_NAME=tcp:54321
psql "host=127.0.0.1 port=54321 sslmode=disable dbname=$POSTGRES_DB user=$POSTGRES_USER"
yarn export-db .env
initialization.sql
CSV ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์
yarn import-db .env
GCP_ID=GCP๊ณ์ ์ด๋ฆ
GCE_ID=GCE์ธ์คํด์ค์ด๋ฆ
gcloud init
gcloud components update
gcloud compute ssh $GCP_ID@$GCE_ID
# Set variables
DOCKER_VOLUME_NAME=๋์ปค๋ณผ๋ฅจ์ด๋ฆ
POSTGRES_HOST=DB์๋ฒ์ฃผ์
POSTGRES_USER=DB๊ณ์ ์ด๋ฆ
POSTGRES_PASSWORD=DB๊ณ์ ์ํธ
POSTGRES_DB=DB์ด๋ฆ
# generate the server.key and server.crt https://www.postgresql.org/docs/14/ssl-tcp.html
openssl req -new -nodes -text -out root.csr \
-keyout root.key -subj "/CN=Alpacasalon"
chmod og-rwx root.key
openssl x509 -req -in root.csr -text -days 3650 \
-extfile /etc/ssl/openssl.cnf -extensions v3_ca \
-signkey root.key -out root.crt
openssl req -new -nodes -text -out server.csr \
-keyout server.key -subj "/CN=$POSTGRES_HOST"
openssl x509 -req -in server.csr -text -days 365 \
-CA root.crt -CAkey root.key -CAcreateserial \
-out server.crt
# set postgres (alpine) user as owner of the server.key and permissions to 600
sudo chown 0:70 server.key
sudo chmod 640 server.key
# set client connection policy
echo "
# TYPE DATABASE USER ADDRESS METHOD
# 'local' is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
hostssl all all all scram-sha-256
" > pg_hba.conf
# start a postgres docker container, mapping the .key and .crt into the image.
sudo docker volume create $DOCKER_VOLUME_NAME
sudo docker run \
-d \
-e POSTGRES_USER=$POSTGRES_USER \
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \
-e POSTGRES_DB=$POSTGRES_DB \
-e LANG=ko_KR.UTF8 \
-e LC_COLLATE=C \
-e POSTGRES_INITDB_ARGS=--data-checksums \
--name postgres \
-p 5432:5432 \
--restart=always \
--shm-size=256MB \
-v "$PWD/server.crt:/var/lib/postgresql/server.crt:ro" \
-v "$PWD/server.key:/var/lib/postgresql/server.key:ro" \
-v "$PWD/pg_hba.conf:/var/lib/postgresql/pg_hba.conf" \
-v $DOCKER_VOLUME_NAME:/var/lib/postgresql/data \
postgres:14-alpine \
-c ssl=on \
-c ssl_cert_file=/var/lib/postgresql/server.crt \
-c ssl_key_file=/var/lib/postgresql/server.key \
-c hba_file=/var/lib/postgresql/pg_hba.conf
๋์ปค๋ฅผ ํตํด PostgreSQL ์ปจํ ์ด๋์ ๋์ปค ๋ณผ๋ฅจ์ ์์ฑํ๊ณ , OpenSSL์ ์ด์ฉํด ์์ฒด ์๋ช ๋ ์ธ์ฆ์๋ฅผ ์์ฑํด์ SSL ์ฐ๊ฒฐ์ ํ์ฑํํฉ๋๋ค.
# Set variables
POSTGRES_HOST=DB์๋ฒ์ฃผ์
POSTGRES_USER=DB๊ณ์ ์ด๋ฆ
POSTGRES_DB=DB์ด๋ฆ
psql "host=$POSTGRES_HOST port=5432 dbname=$POSTGRES_DB user=$POSTGRES_USER sslmode=verify-ca"
# https://github.com/rmfpdlxmtidl/google-cloud-build-slack
export SLACK_WEBHOOK_URL=
export PROJECT_ID=
./setup.sh
์คํ ์ค์ธ GraphQL ์๋ฒ์ ํ
์คํธ์ฉ GraphQL ์ฟผ๋ฆฌ๋ฅผ ์์ฒญํ๊ณ ์๋ต์ ๊ฒ์ฌํฉ๋๋ค. ์ด ์คํฌ๋ฆฝํธ๋ฅผ ์คํ ํ๊ธฐ ์ ์ localhost
๋๋ ์๊ฒฉ์์ GraphQL API ์๋ฒ๋ฅผ ์คํํด์ผ ํฉ๋๋ค.
$ yarn generate-db {ํ๊ฒฝ ๋ณ์ ํ์ผ ์์น}
PostgreSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ตฌ์กฐ๋ฅผ ๋ฐํ์ผ๋ก TypeScript ๊ธฐ๋ฐ ์๋ฃํ์ด ๋ด๊ธด ํ์ผ์ ์์ฑํฉ๋๋ค.
$ yarn export ์ต์
PostgreSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ ๋ชจ๋ ์คํค๋ง์ ๋ชจ๋ ํ ์ด๋ธ์ CSV ํ์ผ๋ก ์ ์ฅํฉ๋๋ค. ๋๋ฏธ ๋ฐ์ดํฐ CSV ํ์ผ์ ๋ณ๊ฒฝํ๊ธฐ ์ ์ ์ํํฉ๋๋ค.
$ yarn import ์ต์
CSV ํ์ผ์ PostgreSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฝ์ ํฉ๋๋ค.
https://slack.github.com/
# https://github.com/integrations/slack#subscribing-and-unsubscribing
/github subscribe rmfpdlxmtidl/alpacasalon-backend commits:* reviews comments
/github unsubscribe rmfpdlxmtidl/alpacasalon-backend deployments