2022-06-26 18:19:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-06 10:45:07 +00:00
|
|
|
"os"
|
2022-06-26 18:19:46 +00:00
|
|
|
|
|
|
|
redis "github.com/go-redis/redis/v8"
|
2022-12-03 11:58:30 +00:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2022-06-26 18:19:46 +00:00
|
|
|
)
|
|
|
|
|
2022-12-03 11:58:30 +00:00
|
|
|
var data_db *pgxpool.Pool
|
|
|
|
var messages_db *pgxpool.Pool
|
|
|
|
var stats_db *pgxpool.Pool
|
2022-06-26 18:19:46 +00:00
|
|
|
var rdb *redis.Client
|
|
|
|
|
2022-12-03 11:58:30 +00:00
|
|
|
func run_simple_pg_query(c *pgxpool.Pool, sql string) {
|
2022-06-26 18:19:46 +00:00
|
|
|
_, err := c.Exec(context.Background(), sql)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func connect_dbs() {
|
|
|
|
data_db = pg_connect(get_env_var("DATA_DB_URI"))
|
2022-11-23 09:17:19 +00:00
|
|
|
messages_db = pg_connect(get_env_var("MESSAGES_DB_URI"))
|
2022-06-26 18:19:46 +00:00
|
|
|
rdb = redis_connect(get_env_var("REDIS_ADDR"))
|
2022-12-06 10:45:07 +00:00
|
|
|
|
|
|
|
if uri, ok := os.LookupEnv("STATS_DB_URI"); ok {
|
|
|
|
stats_db = pg_connect(uri)
|
|
|
|
}
|
2022-06-26 18:19:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 11:58:30 +00:00
|
|
|
func pg_connect(url string) *pgxpool.Pool {
|
|
|
|
conn, err := pgxpool.Connect(context.Background(), url)
|
2022-06-26 18:19:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func redis_connect(url string) *redis.Client {
|
|
|
|
return redis.NewClient(&redis.Options{
|
|
|
|
Addr: url,
|
|
|
|
DB: 0,
|
|
|
|
})
|
|
|
|
}
|