mirror of
https://github.com/aykhans/oh-my-url.git
synced 2025-07-02 00:56:47 +00:00
🎉 first commit
This commit is contained in:
48
app/db/base.go
Normal file
48
app/db/base.go
Normal file
@ -0,0 +1,48 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aykhans/oh-my-url/app/config"
|
||||
gormPostgres "gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DB interface {
|
||||
Init()
|
||||
CreateURL(url string) (string, error)
|
||||
GetURL(key string) (string, error)
|
||||
}
|
||||
|
||||
func GetDB() DB {
|
||||
db := config.GetDB()
|
||||
switch db {
|
||||
case "postgres":
|
||||
postgresConf := config.GetPostgresConfig()
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
|
||||
postgresConf.HOST,
|
||||
postgresConf.USER,
|
||||
postgresConf.PASSWORD,
|
||||
postgresConf.DBNAME,
|
||||
postgresConf.PORT,
|
||||
"disable",
|
||||
"UTC",
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
var err error
|
||||
for i := 0; i < 5; i++ {
|
||||
db, err = gorm.Open(gormPostgres.Open(dsn), &gorm.Config{})
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &Postgres{gormDB: db}
|
||||
}
|
||||
return nil
|
||||
}
|
1
app/db/cassandra.go
Normal file
1
app/db/cassandra.go
Normal file
@ -0,0 +1 @@
|
||||
package db
|
1
app/db/mongodb.go
Normal file
1
app/db/mongodb.go
Normal file
@ -0,0 +1 @@
|
||||
package db
|
72
app/db/postgres.go
Normal file
72
app/db/postgres.go
Normal file
@ -0,0 +1,72 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
// "github.com/aykhans/oh-my-url/app/config"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
gormDB *gorm.DB
|
||||
}
|
||||
|
||||
type url struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Key string `gorm:"unique;not null;size:15;default:null"`
|
||||
URL string `gorm:"not null;default:null"`
|
||||
}
|
||||
|
||||
func (p *Postgres) Init() {
|
||||
err := p.gormDB.AutoMigrate(&url{})
|
||||
if err != nil {
|
||||
panic("failed to migrate database")
|
||||
}
|
||||
tx := p.gormDB.Exec(urlKeyCreateTrigger)
|
||||
if tx.Error != nil {
|
||||
panic("failed to create trigger")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Postgres) CreateURL(mainUrl string) (string, error) {
|
||||
url := url{URL: mainUrl}
|
||||
tx := p.gormDB.Create(&url)
|
||||
if tx.Error != nil {
|
||||
return "", tx.Error
|
||||
}
|
||||
return url.Key, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) GetURL(key string) (string, error) {
|
||||
var result url
|
||||
tx := p.gormDB.Where("key = ?", key).First(&result)
|
||||
if tx.Error != nil {
|
||||
return "", tx.Error
|
||||
}
|
||||
return result.URL, nil
|
||||
}
|
||||
|
||||
const urlKeyCreateTrigger = `
|
||||
CREATE OR REPLACE FUNCTION generate_url_key()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
key_characters TEXT := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
key TEXT := '';
|
||||
base INT := LENGTH(key_characters);
|
||||
n INT := NEW.id;
|
||||
BEGIN
|
||||
WHILE n > 0 LOOP
|
||||
n := n - 1;
|
||||
key := SUBSTRING(key_characters FROM (n % base) + 1 FOR 1) || key;
|
||||
n := n / base;
|
||||
END LOOP;
|
||||
NEW.key := key;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS trigger_generate_url_key on "public"."urls";
|
||||
|
||||
CREATE TRIGGER trigger_generate_url_key
|
||||
BEFORE INSERT ON urls
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION generate_url_key();
|
||||
`
|
Reference in New Issue
Block a user