2024-06-24 23:34:10 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-06-25 00:19:53 +00:00
|
|
|
"log"
|
2024-06-24 23:34:10 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.obamna.ru/erius/ozon-task/graph/model"
|
|
|
|
"git.obamna.ru/erius/ozon-task/internal/storage/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
inMemory = "inmemory"
|
|
|
|
postgres = "postgres"
|
|
|
|
)
|
|
|
|
|
|
|
|
var storage = os.Getenv("APP_STORAGE")
|
|
|
|
|
|
|
|
type Storage interface {
|
|
|
|
AddPost(input *model.PostInput) (*model.AddResult, error)
|
|
|
|
AddComment(input *model.CommentInput) (*model.AddResult, error)
|
|
|
|
GetPosts() ([]*model.Post, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitStorage() (Storage, error) {
|
2024-06-25 00:19:53 +00:00
|
|
|
log.Printf("initializing storage of type %s...", storage)
|
2024-06-24 23:34:10 +00:00
|
|
|
switch storage {
|
|
|
|
case inMemory:
|
|
|
|
return InitInMemory(), nil
|
|
|
|
case postgres:
|
|
|
|
return db.InitPostgres()
|
|
|
|
default:
|
2024-06-25 00:19:53 +00:00
|
|
|
log.Printf("storage of type %s doesn't exists, falling back to default in-memory storage", storage)
|
2024-06-24 23:34:10 +00:00
|
|
|
return InitInMemory(), nil
|
|
|
|
}
|
|
|
|
}
|