Skip to content

Commit

Permalink
Merge pull request #1 from twin-te/develop
Browse files Browse the repository at this point in the history
実装した
  • Loading branch information
takonasu committed Mar 23, 2021
2 parents baf0d5b + 4753e96 commit 12a3004
Show file tree
Hide file tree
Showing 26 changed files with 863 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/microsoft/vscode-dev-containers/tree/v0.154.0/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
"name": "TwinteTimetableServiceDevContainer",
"name": "TwinteInformationServiceDevContainer",

// Update the 'dockerComposeFile' list if you have more compose files or use different names.
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
Expand Down
14 changes: 7 additions & 7 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ services:

# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"
# postgres:
# image: postgres:13.1
# container_name: postgres-container
# environment:
# POSTGRES_PASSWORD: postgres
# volumes:
# - ./initdb/init.sql:/docker-entrypoint-initdb.d/init.sql
postgres:
image: postgres:13.1
container_name: postgres-container
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: twinte_information_service

2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
username: ${{ secrets.CR_USER }}
password: ${{ secrets.CR_PAT }}
- name: Set Tag Name
run: echo "TAG_NAME=ghcr.io/twin-te/grpc-ts-template:${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
run: echo "TAG_NAME=ghcr.io/twin-te/information-service:${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Build
run: docker build . -t $TAG_NAME
- name: Push
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
run: docker-compose exec -T node bash -c "yarn && yarn proto"
- name: run test
working-directory: .devcontainer
run: docker-compose exec -T node bash -c "yarn test"
run:
docker-compose exec -T node bash -c "yarn test"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
generated
.coverage
dist
dist
.env
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN yarn build
FROM node:14-alpine
WORKDIR /usr/src/app

LABEL org.opencontainers.image.source https://github.com/twin-te/grpc-ts-template
LABEL org.opencontainers.image.source https://github.com/twin-te/information-service

COPY --from=build-env /usr/src/app/dist ./dist
COPY --from=build-env /usr/src/app/protos ./protos
Expand Down
88 changes: 26 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# grpc-ts-template

typescript & grpc のテンプレート。
# informationService
お知らせの情報を管理します。
typescript & grpc

# 利用方法
| 環境変数名 | 説明 | default |
|------------|----------------------------------|-----------------------|
| ADMIN_USER | 権限を持たせるユーザーID | なし |
| PG_HOST | Postgres接続先のホスト名 | postgres |
| PG_PORT | Postgres接続先のポート番号 | 5432 |
| PG_DATABASE | Postgres接続先のデータベース名 | twinte_information_service |
| PG_USER | Postgres接続に使用するユーザー名 | postgres |
| PG_PASSWORD | Postgres接続に使用するパスワード | postgres |
| LOG_LEVEL | ログレベル fatal / error / warn / info / debug / trace / off | info

# 推奨開発環境
docker + vscode を使うことで簡単に開発可能。
Expand All @@ -10,62 +21,15 @@ docker + vscode を使うことで簡単に開発可能。
3. 右下に `Folder contains a Dev Container configuration file. Reopen folder to develop in a container` と案内が表示されるので`Reopen in Container`を選択する。(表示されない場合はコマンドパレットを開き`open folder in container`と入力する)
4. node14の開発用コンテナが立ち上がりVSCodeで開かれる。また、`.devcontainer/docker-compose.yml` に任意のサービスを追加するとvscode起動時に一緒に起動できる(データベース等)。

# npmコマンド一覧

|コマンド|説明|
|:--|:--|
|dev| 開発起動|
|proto|protoファイルから型定義を生成(proto-gen.shを実行している)|
|client|grpcリクエストが送れるCLIを起動|
|test|テストを実行|
|build|distにビルド結果を出力|

# とりあえず動かす
```bash
# 準備
yarn && yarn proto

# 開発鯖立ち上げ
yarn dev


## ----以下別窓---- ##

# gRPCリクエストを送るCLIを立ち上げる
yarn client

# CLIが立ち上がったらリクエストを送る
HelloService@localhost:50051> client.greet({name: 'Twin:te'}, pr)

# レスポンスが返ってくれば成功
HelloService@localhost:50051>
{
"text": "hello! SIY1121"
}

```

# GitHub Actions
- `.github/workflows/test.yml` pushされるとテストを実行する
- `.github/workflows/release.yml` GitHub上でリリースをPublishするとDockerImageをビルドし、GHCRにプッシュする。

# 必要変更箇所

1. Dockerfile L17
```dockerfile
LABEL org.opencontainers.image.source https://github.com/twin-te/grpc-ts-template
```
後ろのurlを自分のレポジトリのurlに変更する。(DockerImageとレポジトリの紐付けを行う)

2. .github/workflows/release.yml L17
```yml
run: echo "TAG_NAME=ghcr.io/twin-te/grpc-ts-template:${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
```
TAG_NAME=ghcr.io/twin-te/{自分のレポジトリ名} に変更する(GitHubContainerRegistryにプッシュするときに使う)
3. GitHubのSettings > Secrets > New repository secret で以下の環境変数を登録(GitHubAction用)
|名前|説明|
|:---|:---|
|CR_PAT| GitHubContainerRegistry:write の権限を持ったPersonalAccessToken|
|CR_USER|PersonalAccessTokenを作ったユーザー名|
# v3バックエンドサービス一覧
- API Gateway
- Auth Callback
- User Service
- Session Service
- Timetable Service
- Course Service
- Search Service
- Donation Service
- School Calendar Service
- Information Service (here)
- Task Service
97 changes: 97 additions & 0 deletions __tests__/grpc/Information.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { startGrpcServer, stopGrpcServer } from '../../src/grpc'
import * as protoLoader from '@grpc/proto-loader'
import path from 'path'
import * as grpc from '@grpc/grpc-js'
import { InformationService } from '../../generated'
import { ServiceClientConstructor } from '@grpc/grpc-js/build/src/make-client'
import { GrpcClient } from '../../src/grpc/type'
import { Status } from '@grpc/grpc-js/build/src/constants'
import { connectDatabase } from '../../src/database'

const def = protoLoader.loadSync(
path.resolve(__dirname, `../../protos/InformationService.proto`)
)
const pkg = grpc.loadPackageDefinition(def)
const ClientConstructor = pkg.InformationService as ServiceClientConstructor
let client: GrpcClient<InformationService>

beforeAll(async () => {
await connectDatabase()
await startGrpcServer()
client = (new ClientConstructor(
'localhost:50051',
grpc.ChannelCredentials.createInsecure()
) as unknown) as GrpcClient<InformationService>
})

// ランダム文字列
let id: string = ""
const title = Math.random().toString(32).substring(2)
const content = Math.random().toString(32).substring(2)
const user = 'te_twin'

test('お知らせがDBに登録できるか', (done) => {
const publishedAt = "2019-10-14 1:54:45"
client.addInformation({ user, title, content, publishedAt }, (err, res) => {
expect(err).toBeNull()
done()
})
})

test('未来のお知らせがDBに登録できるか', (done) => {
const publishedAt = "2125-10-14 1:54:45"
client.addInformation({ user, title, content, publishedAt }, (err, res) => {
expect(err).toBeNull()
done()
})
})

test('DBに登録したお知らせが未来公開分以外すべて読み込めるか', (done) => {
client.listInformation({ }, (err, res) => {
expect(res?.Informations[0].title).toEqual(title)
expect(res?.Informations[0].content).toEqual(content)
done()
})
})



test('DBに登録したお知らせが指定した数読み込めるか', (done) => {
const limit = 1;
client.getInformation({ limit }, (err, res) => {
expect(err).toBeNull()
expect(res?.Informations.length).toEqual(limit)
expect(res?.Informations[0].title).toEqual(title)
expect(res?.Informations[0].content).toEqual(content)
expect(res?.Informations.length).toEqual(1)
done()
})
})

test('DBに登録したお知らせが未来のも含めて読み込めるか', (done) => {
client.adminListInformation({ user }, (err, res) => {
id = res?.Informations[0].id!;
expect(err).toBeNull()
expect(res?.Informations.length).toEqual(2)
expect(res?.Informations[0].title).toEqual(title)
expect(res?.Informations[0].content).toEqual(content)
expect(res?.Informations[1].title).toEqual(title)
expect(res?.Informations[1].content).toEqual(content)
done()
})
})

test('指定したIDのお知らせを消去できるか', (done) => {
client.removeInformation({ user, id }, (err, res) => {
expect(err).toBeNull()
done()
})
})
test('正しく消去できているか', (done) => {
client.adminListInformation({ user }, (err, res) => {
expect(err).toBeNull()
expect(res?.Informations.length).toEqual(1)
done()
})
})
afterAll(stopGrpcServer)
42 changes: 0 additions & 42 deletions __tests__/grpc/hello.service.test.ts

This file was deleted.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"dev": "cross-env LOG_LEVEL=trace ts-node --files src/index.ts",
"proto": "./proto-gen.sh",
"client": "grpcc -i -a localhost:50051 -p protos/HelloService.proto",
"test": "cross-env LOG_LEVEL=off jest --runInBand --coverage",
"client": "grpcc -i -a localhost:50051 -p protos/InformationService.proto",
"test": "cross-env LOG_LEVEL=off ADMIN_USER=te_twin jest --runInBand --coverage",
"build": "rm -rf ./dist && tsc && rm -rf ./dist/__tests__ && mv ./dist/src/* ./dist",
"lint": "eslint src/**/*.ts"
},
Expand Down Expand Up @@ -37,9 +37,15 @@
"dependencies": {
"@grpc/grpc-js": "^1.2.2",
"@grpc/proto-loader": "^0.5.6",
"@types/pg": "^7.14.11",
"clone": "^2.1.2",
"cross-env": "^7.0.3",
"dayjs": "^1.10.4",
"dotenv": "^8.2.0",
"log4js": "^6.3.0",
"protobufjs": "github:iFwu/protobuf.js"
"pg": "^8.5.1",
"protobufjs": "github:iFwu/protobuf.js",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.31"
}
}
2 changes: 1 addition & 1 deletion proto-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn pbjs \
--no-decode \
--path ../../ \
--out ./generated/index.js \
./protos/HelloService.proto \
./protos/InformationService.proto \

yarn pbts \
--out ./generated/index.d.ts \
Expand Down
13 changes: 0 additions & 13 deletions protos/HelloService.proto

This file was deleted.

Loading

0 comments on commit 12a3004

Please sign in to comment.