Added init log messages

This commit is contained in:
Egor 2024-06-25 03:19:53 +03:00
parent 1161a56ec9
commit ab4c41c7f3
3 changed files with 16 additions and 2 deletions

View file

@ -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}))

View file

@ -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
}

View file

@ -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
}
}