diff --git a/data/data.go b/data/data.go new file mode 100644 index 0000000..c1920ec --- /dev/null +++ b/data/data.go @@ -0,0 +1,39 @@ +package data + +import ( + "nixcn-cms/config" + "nixcn-cms/data/drivers" + + log "github.com/sirupsen/logrus" +) + +var Database *drivers.DBClient + +func Init() { + // Init database + dbType := config.Get("database.type").(string) + exDSN := drivers.ExternalDSN{ + Host: config.Get("database.host").(string), + Name: config.Get("database.name").(string), + Username: config.Get("database.username").(string), + Password: config.Get("database.password").(string), + } + + if dbType != "postgres" { + log.Fatal("[Database] Only support postgras db!") + } + + // Conect to db + db, err := drivers.Postgres(exDSN) + if err != nil { + log.Fatal("[Database] Error connecting to db!") + } + + // Auto migrate + err = db.DB.AutoMigrate(&User{}) + if err != nil { + log.Error("[Database] Error migrating database: ", err) + } + + Database = db +} diff --git a/data/user.go b/data/user.go new file mode 100644 index 0000000..316b2e5 --- /dev/null +++ b/data/user.go @@ -0,0 +1,21 @@ +package data + +import "github.com/google/uuid" + +type User struct { + Id uint `json:"id" gorm:"primarykey;autoincrement"` + UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"` + UserId string `json:"user_id" gorm:"size:8;uniqueindex;not null"` + Email string `json:"email" gorm:"uniqueindex;not null"` + Nickname string `json:"nickname" gorm:"not null"` + Type string `json:"type" gorm:"not null"` + Subtitle string `json:"subtitle" gorm:"not null"` + Avatar string `json:"avatar" gorm:"not null"` +} + +func (self *User) GetByEmail(email string) error { + if err := Database.Where("email = ?", email).First(&self).Error; err != nil { + return err + } + return nil +} diff --git a/go.mod b/go.mod index 54bb075..3dffd96 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/go-viper/mapstructure/v2 v2.4.0 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.19.1 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.6.0 // indirect diff --git a/go.sum b/go.sum index 92cbdce..10aa00e 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU github.com/goccy/go-yaml v1.19.1 h1:3rG3+v8pkhRqoQ/88NYNMHYVGYztCOCIZ7UQhu7H+NE= github.com/goccy/go-yaml v1.19.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= diff --git a/main.go b/main.go index 994fe0b..f16e285 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "nixcn-cms/config" + "nixcn-cms/data" "nixcn-cms/logger" "nixcn-cms/server" ) @@ -9,5 +10,6 @@ import ( func main() { config.Init() logger.Init() + data.Init() server.Start() }