Skip to content

Commit 0a79ff1

Browse files
committed
remove microservices
1 parent 6e33bf4 commit 0a79ff1

21 files changed

+174
-614
lines changed

.github/workflows/build-and-deploy.yml

+17-63
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ on:
1010

1111
env:
1212
REGISTRY: "ghcr.io/zero-plus-x"
13-
BACKEND_IMAGE_NAME: "helpua-backend"
14-
BOT_IMAGE_NAME: "helpua-bot"
13+
IMAGE_NAME: "helpua-backend"
1514

1615
jobs:
17-
build_and_push_backend:
16+
build_and_push:
1817
runs-on: ubuntu-latest
1918
steps:
2019
- name: Checkout the repo
2120
uses: actions/checkout@v2
2221

2322
- name: Build container image
24-
run: docker build -t $(echo $REGISTRY)/$(echo $BACKEND_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) -f packages/backend/Dockerfile .
23+
run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) -f packages/backend/Dockerfile .
2524

2625
- name: Login to GitHub Container Registry
2726
uses: docker/login-action@v1
@@ -31,48 +30,29 @@ jobs:
3130
password: ${{ secrets.GITHUB_TOKEN }}
3231

3332
- name: Push image to GitHub Container Registry
34-
run: docker push $(echo $REGISTRY)/$(echo $BACKEND_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
35-
36-
build_and_push_bot:
37-
runs-on: ubuntu-latest
38-
steps:
39-
- name: Checkout the repo
40-
uses: actions/checkout@v2
41-
42-
- name: Build container image
43-
run: docker build -t $(echo $REGISTRY)/$(echo $BOT_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) -f packages/bot/Dockerfile .
44-
45-
- name: Login to GitHub Container Registry
46-
uses: docker/login-action@v1
47-
with:
48-
registry: ghcr.io
49-
username: ${{ github.actor }}
50-
password: ${{ secrets.GITHUB_TOKEN }}
51-
52-
- name: Push image to GitHub Container Registry
53-
run: docker push $(echo $REGISTRY)/$(echo $BOT_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
33+
run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
5434

5535
migrate_postgresql:
5636
runs-on: ubuntu-latest
57-
needs: build_and_push_backend
37+
needs: build_and_push
5838
steps:
5939
- name: Migrate the DB
6040
uses: appleboy/[email protected]
6141
with:
6242
host: ${{ secrets.HOST }}
6343
username: ${{ secrets.USERNAME }}
6444
key: ${{ secrets.SSH_KEY }}
65-
envs: BACKEND_IMAGE_NAME,REGISTRY,GITHUB_SHA
45+
envs: IMAGE_NAME,REGISTRY,GITHUB_SHA
6646
script: |
6747
echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
6848
docker run \
6949
--env DATABASE_URL=${{ secrets.DATABASE_URL }} \
70-
--name $(echo $BACKEND_IMAGE_NAME)-migrate \
71-
$(echo $REGISTRY)/$(echo $BACKEND_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) \
50+
--name $(echo $IMAGE_NAME)-migrate \
51+
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) \
7252
pnpm migrate:deploy
73-
docker rm $(echo $BACKEND_IMAGE_NAME)-migrate
53+
docker rm $(echo $IMAGE_NAME)-migrate
7454
75-
deploy_backend:
55+
deploy:
7656
runs-on: ubuntu-latest
7757
needs: migrate_postgresql
7858

@@ -83,50 +63,24 @@ jobs:
8363
host: ${{ secrets.HOST }}
8464
username: ${{ secrets.USERNAME }}
8565
key: ${{ secrets.SSH_KEY }}
86-
envs: BACKEND_IMAGE_NAME,REGISTRY,GITHUB_SHA
87-
script: |
88-
echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
89-
# Stop running container
90-
docker stop $(echo $BACKEND_IMAGE_NAME)
91-
# Remove old container
92-
docker rm $(echo $BACKEND_IMAGE_NAME)
93-
# Run a new container from a new image
94-
docker run -d \
95-
--env DATABASE_URL=${{ secrets.DATABASE_URL }} \
96-
--network net \
97-
--restart always \
98-
--name $(echo $BACKEND_IMAGE_NAME) \
99-
$(echo $REGISTRY)/$(echo $BACKEND_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
100-
101-
deploy_bot:
102-
runs-on: ubuntu-latest
103-
needs: [build_and_push_bot, deploy_backend]
104-
105-
steps:
106-
- name: Deploy to Digital Ocean droplet via SSH action
107-
uses: appleboy/[email protected]
108-
with:
109-
host: ${{ secrets.HOST }}
110-
username: ${{ secrets.USERNAME }}
111-
key: ${{ secrets.SSH_KEY }}
112-
envs: BOT_IMAGE_NAME,REGISTRY,GITHUB_SHA,BACKEND_IMAGE_NAME
66+
envs: IMAGE_NAME,REGISTRY,GITHUB_SHA
11367
script: |
11468
echo ${{ secrets.GITHUB_TOKEN }} | docker login -u ${{ github.actor }} --password-stdin ghcr.io
11569
# Stop running container
116-
docker stop $(echo $BOT_IMAGE_NAME)
70+
docker stop $(echo $IMAGE_NAME)
11771
# Remove old container
118-
docker rm $(echo $BOT_IMAGE_NAME)
72+
docker rm $(echo $IMAGE_NAME)
11973
# Run a new container from a new image
12074
docker run -d \
12175
--env TELEGRAM_BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} \
122-
--env BACKEND_HOST=http://$(echo $BACKEND_IMAGE_NAME):3000 \
76+
--env DATABASE_URL=${{ secrets.DATABASE_URL }} \
12377
--network net \
12478
--restart always \
125-
--name $(echo $BOT_IMAGE_NAME) \
126-
$(echo $REGISTRY)/$(echo $BOT_IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
79+
--name $(echo $IMAGE_NAME) \
80+
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
12781
cleanup:
12882
runs-on: ubuntu-latest
129-
needs: [deploy_bot, deploy_backend]
83+
needs: [deploy]
13084

13185
steps:
13286
- name: Remove unused images

docker-compose.yml

-13
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,3 @@ services:
3939
environment:
4040
- DATABASE_URL=postgresql://postgres:postgres@postgres/dev
4141
command: pnpm dev:backend
42-
bot:
43-
build:
44-
context: .
45-
dockerfile: packages/bot/dev.Dockerfile
46-
volumes:
47-
- ./packages/bot/src:/home/node/app/packages/bot/src
48-
ports:
49-
- 8080:8080
50-
links:
51-
- backend
52-
env_file:
53-
- packages/bot/.env
54-
command: pnpm dev:bot

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"main": "index.js",
66
"private": true,
77
"scripts": {
8-
"start:bot": "pnpm --filter=@help-ua/bot start",
9-
"dev:bot": "node-dev packages/bot/src/index.ts",
108
"start:backend": "pnpm --filter=@help-ua/backend start",
119
"dev:backend": "node-dev packages/backend/src/index.ts",
1210
"lint": "eslint '**/*.ts' --max-warnings 0",
@@ -32,8 +30,7 @@
3230
"typescript": "^4.6.2"
3331
},
3432
"devDependencies": {
35-
"@types/node": "^17.0.21",
36-
"ts-node": "^10.7.0"
33+
"@types/node": "^17.0.21"
3734
},
3835
"prettier": {
3936
"singleQuote": true,

packages/backend/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"dependencies": {
2323
"@prisma/client": "^3.10.0",
2424
"dotenv": "^16.0.0",
25-
"express": "^4.17.3",
25+
"debug": "^4.3.3",
26+
"telegraf": "^4.7.0",
27+
"telegraf-session-local": "^2.1.0",
2628
"prisma": "^3.10.0"
2729
},
2830
"devDependencies": {

packages/backend/prisma/schema.prisma

-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ enum SpokenLanguage {
2323
GERMAN
2424
}
2525

26-
enum Role {
27-
HELPEE
28-
HELPER
29-
}
30-
3126
enum Category {
3227
URGENT_CARE
3328
TRANSPORTATION

packages/backend/sample.env

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
DATABASE_URL=postgresql://postgres:postgres@localhost/dev
2+
TELEGRAM_BOT_TOKEN=

packages/bot/src/answers/index.ts renamed to packages/backend/src/bot/answers/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Telegraf } from 'telegraf';
22
import { HelpUAContext, Selection } from '../shared/types';
33
import { askForCategory, askForRole, askToRestart } from '../questions';
44
import { register } from '../db';
5+
import {isUILanguage} from '../../translations';
56

67
const initialSelection: Selection = {
78
uiLanguage: null,
@@ -25,7 +26,7 @@ const initAnswerListeners = (bot: Telegraf<HelpUAContext>) => {
2526

2627
const uiLanguage = ctx.match[1];
2728

28-
if (uiLanguage != null) {
29+
if (uiLanguage != null && isUILanguage(uiLanguage)) {
2930
ctx.session.selection = withInitialSession({ selection: ctx.session.selection, options: { uiLanguage } });
3031
const userId = ctx.update.callback_query.from.id
3132

@@ -47,9 +48,9 @@ const initAnswerListeners = (bot: Telegraf<HelpUAContext>) => {
4748
const uiLanguage = ctx.session.selection.uiLanguage;
4849
const role = ctx.match[1];
4950

50-
if (role && uiLanguage != null && ctx.session.selection) {
51+
if (role && uiLanguage != null && ctx.session.selection && isUILanguage(uiLanguage)) {
5152
ctx.session.selection.role = role;
52-
askForCategory(bot, ctx.chat.id, uiLanguage, role);
53+
askForCategory(bot, ctx.chat.id, uiLanguage);
5354
} else {
5455
askToRestart(ctx);
5556
}

packages/backend/src/bot/db/index.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { UILanguage } from '@prisma/client';
2+
import { Answer, User } from '../shared/types';
3+
import { prisma } from '../../db'
4+
import { CategoryTranslations, RoleTranslations, UILanguageLabels } from '../../translations';
5+
6+
export const register = async ({ userId, chatId, uiLanguage}: { userId: number, chatId: number, uiLanguage: UILanguage }): Promise<User> => {
7+
try {
8+
// @NOTE: Dev only, this removes the user before storing to db, so it's easier to test locally
9+
// @TODO: Remove later
10+
await prisma.user.delete({ where: { telegramUserId: userId } });
11+
} catch (e) {
12+
console.log('First time user.');
13+
}
14+
15+
const user = await prisma.user.create({
16+
data: { chatId, telegramUserId: userId, uiLanguage }
17+
});
18+
19+
return user
20+
};
21+
22+
export const getUILanguages = (): Answer[] => {
23+
return Object.entries(UILanguageLabels).map(([key, label]) => {
24+
return {key, label}
25+
})
26+
};
27+
28+
export const getRoles = (uiLanguage: UILanguage): Answer[] => {
29+
return Object.entries(RoleTranslations).map(([key, labels]) => {
30+
return {
31+
key,
32+
label: labels[uiLanguage]
33+
}
34+
})
35+
};
36+
37+
export const getCategories = (uiLanguage: UILanguage): Answer[] => {
38+
return Object.entries(CategoryTranslations).map(([key, labels]) => {
39+
return {
40+
key,
41+
label: labels[uiLanguage]
42+
}
43+
})
44+
};

packages/bot/src/questions/index.ts renamed to packages/backend/src/bot/questions/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import {UILanguage} from '@prisma/client';
12
import { Telegraf } from 'telegraf';
23
import { getCategories, getRoles, getUILanguages } from '../db';
3-
import { THelpUAContext } from '../shared/types';
4+
import { HelpUAContext } from '../shared/types';
45

5-
export const askForLanguage = async (bot: Telegraf<THelpUAContext>, chatId: number) => {
6-
const uiLanguages = await getUILanguages();
6+
export const askForLanguage = async (bot: Telegraf<HelpUAContext>, chatId: number) => {
7+
const uiLanguages = getUILanguages();
78
const rows = uiLanguages.map(({ key, label }) => ({
89
text: label,
910
callback_data: `ui-language:${key}`
@@ -16,8 +17,8 @@ export const askForLanguage = async (bot: Telegraf<THelpUAContext>, chatId: numb
1617
});
1718
};
1819

19-
export const askForRole = async (bot: Telegraf<THelpUAContext>, chatId: number, uiLanguage: string) => {
20-
const roles = await getRoles(uiLanguage);
20+
export const askForRole = async (bot: Telegraf<HelpUAContext>, chatId: number, uiLanguage: UILanguage) => {
21+
const roles = getRoles(uiLanguage);
2122
const rows = roles.map(role => ({
2223
text: role.label,
2324
callback_data: `role:${role.key}`
@@ -30,12 +31,11 @@ export const askForRole = async (bot: Telegraf<THelpUAContext>, chatId: number,
3031
};
3132

3233
export const askForCategory = async (
33-
bot: Telegraf<THelpUAContext>,
34+
bot: Telegraf<HelpUAContext>,
3435
chatId: number,
35-
uiLanguage: string,
36-
role: string
36+
uiLanguage: UILanguage,
3737
) => {
38-
const categories = await getCategories(uiLanguage, role);
38+
const categories = getCategories(uiLanguage);
3939
const rows = categories.map(category => ({
4040
text: category.label,
4141
callback_data: `help-type:${category.key}`
@@ -47,6 +47,6 @@ export const askForCategory = async (
4747
});
4848
};
4949

50-
export const askToRestart = (ctx: THelpUAContext) => {
50+
export const askToRestart = (ctx: HelpUAContext) => {
5151
ctx.reply('Cannot process response, try /start again');
5252
};

0 commit comments

Comments
 (0)