From ab4c41c7f3ad9d2358a4d003e43e0b3903bb9fd5 Mon Sep 17 00:00:00 2001 From: erius Date: Tue, 25 Jun 2024 03:19:53 +0300 Subject: [PATCH] Added init log messages --- cmd/server.go | 3 ++- internal/storage/db/postgres.go | 12 +++++++++++- internal/storage/storage.go | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index e88745b..4b321c0 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -22,8 +22,9 @@ func main() { s, err := storage.InitStorage() if err != nil { - log.Panic("could not connect to database") + log.Fatalf("failed to init storage: %s", err) } + log.Println("finished storage init") res := graph.InitResolver(s) srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: res})) diff --git a/internal/storage/db/postgres.go b/internal/storage/db/postgres.go index 7a50b20..3da0fa8 100644 --- a/internal/storage/db/postgres.go +++ b/internal/storage/db/postgres.go @@ -2,6 +2,7 @@ package db import ( "fmt" + "log" "os" "git.obamna.ru/erius/ozon-task/graph/model" @@ -19,11 +20,20 @@ var ( ) func InitPostgres() (*Database, error) { + log.Printf("connecting to PostgreSQL database at %s...", con) // PrepareStmt is true for caching complex sql statements when adding comments or replies db, err := gorm.Open(postgres.Open(con), &gorm.Config{PrepareStmt: true}) if err != nil { + log.Printf("failed to connect to database: %s", err) return nil, err } - db.AutoMigrate(&model.Post{}, &model.Comment{}) + log.Println("opened connection to PostgreSQL database") + log.Println("migrating model scheme to database...") + err = db.AutoMigrate(&model.Post{}, &model.Comment{}) + if err != nil { + log.Printf("failed to automatically migrate model scheme: %s", err) + return nil, err + } + log.Println("finished migrating model scheme") return &Database{db}, nil } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index dd2f954..800bf32 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -1,6 +1,7 @@ package storage import ( + "log" "os" "git.obamna.ru/erius/ozon-task/graph/model" @@ -21,12 +22,14 @@ type Storage interface { } func InitStorage() (Storage, error) { + log.Printf("initializing storage of type %s...", storage) switch storage { case inMemory: return InitInMemory(), nil case postgres: return db.InitPostgres() default: + log.Printf("storage of type %s doesn't exists, falling back to default in-memory storage", storage) return InitInMemory(), nil } }