CTSM-SQL 為 Catch the stars 的後端系統,包括 Apollo Express、PostgreSQL、Nginx 和 Docker 配置。
- /backend 內的資料,為 Catch the stars 的後端原始程式。
- /frontend 放置Catch the stars打包後的檔案。
- /nginx 主要作為 docker 佈署後的路由導向內部 port 的配置。
- Apollo 相關,Apollo Server 為 GraphQL 架構支援 NodeJS 的工具:
- apollo-server-express
- apollo-server-core
- graphql
- graphql-tools
- graphql-upload
- 後端伺服器
- express
- stream-promise
- http
- https
- JWT 相關
- jsonwebtoken - 能夠快速製作符合 JSON Web Token 的驗證資料。
- bcrypt - 將明碼加密的工具。
- 環境變數相關
- cross-env - 解決跨系統時,環境變數的問題。
- dotenv - 可在一開始載入所有環境變數。
- 資料庫相關
- knex
- pg-promise
- aws-sdk
- 確定連接資料庫 /backend/psql/adapter 的環境變數都有設置好,本地端已開啟 Postgres 的 Server。
- 運行 Apollo Server:
# 進入後端目錄
cd .\backend\
# 安裝 node 依賴
npm install
# 本機運行
npm run dev
# 顯示 Now browse to http://localhost:4000/graphql
- 瀏覽器進入 http://localhost:4000/graphql,可在 CodeSandbox 的介面進行 query 和 mutation。
-
新增 Query 和 Mutation 的詳細設定,需在 /backend/psql/resolver 進行撰寫,並在 /backend/psql/schema 進行設定。
- SSH 連接至遠端伺服器。
- 安裝並啟動 Docker。
# 如果在 ubuntu 直接安裝 docker 會抓不到,所以要先更新 apt
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y docker.io
sudo apt install -y docker-compose
# 檢查版本
docker -v
docker-compose version
# 啟動 docker
sudo service docker start
- 將倉庫的檔案都上傳至伺服器。
- 參照 docker-compose.yml 的設定,啟動多個容器。
docker-compose up -d
- 將 sql 檔匯入至 postgres 的容器內
# 將 ctsm-db.sql 複製到 postgres 的容器內
psql -U postgres postgres < ctsm-db.sql
docker cp ctsm-db.sql 983358f034a9:/tmp
# 進入 postgres 容器
docker exec -it postgres bash
# 匯入 db
psql -U postgres postgres < ctsm-db.sql
# 匯入完後進入 psql cli 介面
psql --host=0.0.0.0 --username=postgres
# 查看資料庫是否匯入
\l
# 查看資料表是否匯入
\z
- 手動更新憑證
參考原 Docker 映像檔 - docker-nginx-certbot
# 進入 nginx 容器內
ssh -i "key.pem" ec2.url......
# 針對指定 html 路徑和網域發證書 (先加上 --dry-run 測試)
certbot certonly --webroot -w /etc/nginx/html/ -d my.site.com --dry-run
certbot certonly --webroot -w /etc/nginx/html/ -d my.site.com
# 驗證 (先加上 --dry-run 測試)
certbot renew --dry-run
certbot renew
# 退回容器外
exit
# 刪除 nginx 容器
docker kill --signal=HUP <container_name>
# 重新設定容器
docker-compose up -d
# 最後輸入網路確認鎖頭日期即可