Initial commit. Working login, example UI with prototype and demo, connections to DB and DAV, local and prod setups.
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
//go:build dev
|
||||
// +build dev
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var DB *pgxpool.Pool
|
||||
|
||||
func Connect() error {
|
||||
// Connect to Postgres inside Docker network
|
||||
dsn := fmt.Sprintf(
|
||||
"postgres://%s:%s@localhost:5432/%s?sslmode=disable",
|
||||
getEnv("POSTGRES_USER"),
|
||||
getEnv("POSTGRES_PASSWORD"),
|
||||
getEnv("POSTGRES_DB"),
|
||||
)
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
DB = pool
|
||||
|
||||
err = testDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func testDB() error {
|
||||
ctx := context.Background()
|
||||
conn, err := DB.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
row := conn.QueryRow(ctx, "SELECT 1")
|
||||
var result int
|
||||
err = row.Scan(&result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getEnv(key string) string {
|
||||
if val := os.Getenv(key); val != "" {
|
||||
return val
|
||||
}
|
||||
log.Fatal("FATAL: Environment variable not set:", key)
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user