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

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
config.json config.json
.secrets .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 Before running **MIC** create file `./config.json` that contains
```json ```json
{ {
// BOT_TOKEN env variable has more priority
// that the config value
"bot_token": "<your_token>"
// to run in Docker set the value to // to run in Docker set the value to
// "/run/secrets/db_password" // "/run/secrets/db_password"
// If wasn't set -- POSTGRES_PASSWORD env // If wasn't set -- POSTGRES_PASSWORD env
// variable will be used instead // variable will be used instead
"db_password_file": "~/.secrets/mic", "db_password_file": "~/.secrets/mic",
// BOT_TOKEN env variable has more priority // other optional db config parameters
// that the config value
"bot_token": "<your_token>" // 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,
} }
``` ```

View File

@ -89,6 +89,9 @@ export const getConfig = (): Config => {
const decoder = new TextDecoder("utf-8") const decoder = new TextDecoder("utf-8")
const data = Deno.readFileSync(fileCfg.db_password_file) const data = Deno.readFileSync(fileCfg.db_password_file)
db_password = decoder.decode(data).toString() 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) { } else if (envCfg.db_password) {
db_password = envCfg.db_password db_password = envCfg.db_password
} else throw new BadConfigErr( } else throw new BadConfigErr(

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 - mic_bot
secrets: secrets:
- db_password - db_password
volumes:
- ./config.json:/app/config.json
postgres: postgres:
container_name: mic_bot_postgres container_name: mic_bot_postgres
image: postgres:latest image: postgres:latest

View File

@ -15,7 +15,7 @@ export type SetupDbOutput = {
db: Kysely<Database>, db: Kysely<Database>,
} }
export const setupDB = async (cfg: CompiledConfig): Promise<SetupDbOutput> => { 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 pool = new pg.Pool(cfg.pgPoolCfg)
const dialect = new PostgresDialect({ pool }) const dialect = new PostgresDialect({ pool })
const db = new Kysely<Database>({ dialect }) const db = new Kysely<Database>({ dialect })