Infra update

This commit is contained in:
Dmitry Anderson 2024-10-24 16:39:42 +02:00
parent 43bf90ebcc
commit d545318aed
8 changed files with 72 additions and 9 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
.secrets
dev_postgres_data
postgres_data
config.json

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
config.json
.secrets
postgres_data
postgres_data
dev_postgres_data

View File

@ -25,14 +25,23 @@ and it will find images for you.
Before running **MIC** create file `./config.json` that contains
```json
{
// BOT_TOKEN env variable has more priority
// that the config value
"bot_token": "<your_token>"
// to run in Docker set the value to
// "/run/secrets/db_password"
// If wasn't set -- POSTGRES_PASSWORD env
// variable will be used instead
"db_password_file": "~/.secrets/mic",
// BOT_TOKEN env variable has more priority
// that the config value
"bot_token": "<your_token>"
// other optional db config parameters
// Defaults can be seen/changed inside the config.ts file
"db_user": string,
"db_name": string,
"db_port": number,
// if you are running under docker -- set host to postgres
"db_host": string,
"db_tls": boolean,
}
```
@ -78,4 +87,4 @@ deno compile -o ./bin/mic ./main.ts
deno run ./main.ts
# Dev run with autorestart
deno run ./main.ts
```
```

View File

@ -89,6 +89,9 @@ export const getConfig = (): Config => {
const decoder = new TextDecoder("utf-8")
const data = Deno.readFileSync(fileCfg.db_password_file)
db_password = decoder.decode(data).toString()
if (db_password[db_password.length - 1] === '\n') {
db_password = db_password.slice(0, -1)
}
} else if (envCfg.db_password) {
db_password = envCfg.db_password
} else throw new BadConfigErr(
@ -119,4 +122,4 @@ export const getConfig = (): Config => {
bot_token,
}
}
}

5
dev.Dockerfile Normal file
View File

@ -0,0 +1,5 @@
FROM deno:2.0.3
WORKDIR /app
ENTRYPOINT [ "deno", "run", "--watch=.", "--allow-all", "main.ts" ]

38
dev.docker-compose.yml Normal file
View File

@ -0,0 +1,38 @@
secrets:
db_password:
file: .secrets/db_password.txt
networks:
mic_bot_dev:
driver: bridge
services:
bot:
build: .
container_name: mic_bot_dev
restart: always
depends_on:
- postgres
environment:
- MIC_APPLY_MIGRATIONS=y
networks:
- mic_bot_dev
secrets:
- db_password
volumes:
- ./:/app
postgres:
container_name: mic_bot_postgres_dev
image: postgres:latest
restart: always
volumes:
- ./dev_postgres_data:/var/lib/postgresql/data:rw
environment:
POSTGRES_USER: mic
POSTGRES_DB: mic
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
user: "${USER_ID:-1000}:${GID:-1000}"
networks:
- mic_bot_dev
secrets:
- db_password

View File

@ -19,6 +19,8 @@ services:
- mic_bot
secrets:
- db_password
volumes:
- ./config.json:/app/config.json
postgres:
container_name: mic_bot_postgres
image: postgres:latest
@ -33,4 +35,4 @@ services:
networks:
- mic_bot
secrets:
- db_password
- db_password

View File

@ -15,7 +15,7 @@ export type SetupDbOutput = {
db: Kysely<Database>,
}
export const setupDB = async (cfg: CompiledConfig): Promise<SetupDbOutput> => {
console.log("creating db instance")
console.log("creating db instance: ", cfg.pgPoolCfg)
const pool = new pg.Pool(cfg.pgPoolCfg)
const dialect = new PostgresDialect({ pool })
const db = new Kysely<Database>({ dialect })
@ -60,4 +60,4 @@ export const setupDB = async (cfg: CompiledConfig): Promise<SetupDbOutput> => {
}
return { pool, db }
}
}