refactoring
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type KVHandler interface {
|
||||
Put(k string, v any) error
|
||||
Get(k string) (any, error)
|
||||
Delete(k string) error
|
||||
}
|
||||
|
||||
func NewKVHandler(cfg *viper.Viper) (KVHandler, error) {
|
||||
return NewRedisHandler(cfg)
|
||||
}
|
||||
88
storage/postgresql.go
Normal file
88
storage/postgresql.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PostgresqlStorage struct {
|
||||
DbConnection *sql.DB
|
||||
Schema string
|
||||
Tables map[string]string
|
||||
}
|
||||
|
||||
func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
|
||||
var (
|
||||
host = cfg.GetString("storage.db.psql.host")
|
||||
port = cfg.GetString("storage.db.psql.port")
|
||||
user = cfg.GetString("storage.db.psql.user")
|
||||
password = cfg.GetString("storage.db.psql.password")
|
||||
dbname = cfg.GetString("storage.db.psql.dbname")
|
||||
sslmode = cfg.GetString("storage.db.psql.sslmode")
|
||||
pg_schema = cfg.GetString("storage.db.psql.schema")
|
||||
pgtables_users_firebase = cfg.GetString("storage.db.psql.tables.users_firebase")
|
||||
)
|
||||
portInt, _ := strconv.Atoi(port)
|
||||
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, portInt, user, password, dbname, sslmode)
|
||||
db, err := sql.Open("postgres", psqlconn)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("opening connection to postgresql failed")
|
||||
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql failed")
|
||||
}
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("ping to postgresql failed")
|
||||
return PostgresqlStorage{}, fmt.Errorf("connection to postgresql database failed")
|
||||
}
|
||||
return PostgresqlStorage{
|
||||
DbConnection: db,
|
||||
Schema: pg_schema,
|
||||
Tables: map[string]string{
|
||||
"users_firebase": fmt.Sprintf("%s.%s", pg_schema, pgtables_users_firebase),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s PostgresqlStorage) CreateFirebaseToken(user_id string, fcm_token string, device_platform string) (err error) {
|
||||
_, err = uuid.Parse(user_id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Postgresql Storage CreateFirebaseToken invalid User ID")
|
||||
return err
|
||||
}
|
||||
_, err = s.DbConnection.Exec(fmt.Sprintf("INSERT INTO %s (user_id , fcm_token , device_platform) VALUES($1,$2,$3)", s.Tables["users_firebase"]),
|
||||
user_id,
|
||||
fcm_token,
|
||||
device_platform)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
_ = s.UpdateFirebaseToken(user_id, device_platform, fcm_token)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (s PostgresqlStorage) GetFirebaseToken(user_id string) (fcm string, device_platform string, err error) {
|
||||
err = s.DbConnection.QueryRow(fmt.Sprintf("SELECT fcm_token , device_platform FROM %s WHERE user_id = $1", s.Tables["users_firebase"]), user_id).
|
||||
Scan(
|
||||
&fcm,
|
||||
&device_platform,
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return fcm, device_platform, nil
|
||||
}
|
||||
func (s PostgresqlStorage) UpdateFirebaseToken(user_id string, fcm_token string, device_platform string) error {
|
||||
query := fmt.Sprintf("UPDATE %s SET fcm_token = $1 device_platform = $2 WHERE user_id = $3", s.Tables["users_firebase"])
|
||||
_, err := s.DbConnection.Exec(query, fcm_token, device_platform, user_id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
21
storage/postgresql/schema.hcl
Normal file
21
storage/postgresql/schema.hcl
Normal file
@@ -0,0 +1,21 @@
|
||||
schema "silvermobi_backend" {}
|
||||
|
||||
table "users_firebase" {
|
||||
schema = schema.silvermobi_backend
|
||||
column "user_id" {
|
||||
null = false
|
||||
type = uuid
|
||||
}
|
||||
column "fcm_token" {
|
||||
null = false
|
||||
type = varchar(300)
|
||||
}
|
||||
columns "device_platform" {
|
||||
null = false
|
||||
type = text
|
||||
}
|
||||
primary_key {
|
||||
columns = [column.user_id]
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v9"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type RedisHandler struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewRedisHandler(cfg *viper.Viper) (*RedisHandler, error) {
|
||||
redisClient := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.GetString("storage.kv.redis.addr"),
|
||||
DB: cfg.GetInt("storage.kv.redis.db"),
|
||||
})
|
||||
|
||||
_, err := redisClient.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &RedisHandler{
|
||||
client: redisClient,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (rh *RedisHandler) Put(k string, v any) error {
|
||||
return rh.client.Set(context.Background(), k, v, 0).Err()
|
||||
}
|
||||
|
||||
func (rh *RedisHandler) Get(k string) (any, error) {
|
||||
value, err := rh.client.Get(context.Background(), k).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (rh *RedisHandler) Delete(k string) error {
|
||||
return rh.client.Del(context.Background(), k).Err()
|
||||
}
|
||||
25
storage/storage.go
Normal file
25
storage/storage.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
CreateFirebaseToken(user_id string, fcm_token string, device_platform string) (err error)
|
||||
UpdateFirebaseToken(user_id string, fcm_token string, device_platform string) error
|
||||
GetFirebaseToken(user_id string) (fcm string, device_platform string, err error)
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
var (
|
||||
storage_type = cfg.GetString("storage.db.type")
|
||||
)
|
||||
switch storage_type {
|
||||
case "psql":
|
||||
s, err := NewPostgresqlStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user