diff --git a/config.default.yaml b/config.default.yaml index ab2b344..13b0f5e 100644 --- a/config.default.yaml +++ b/config.default.yaml @@ -9,6 +9,12 @@ database: name: postgres username: postgres password: postgres +cache: + hosts: ["127.0.0.1:6379"] + master: "" + username: "" + password: "" + db: 0 email: resend_api_key: abc from: diff --git a/config/types.go b/config/types.go index 58eb947..8cd6829 100644 --- a/config/types.go +++ b/config/types.go @@ -3,6 +3,7 @@ package config type config struct { Server server `yaml:"server"` Database database `yaml:"database"` + Cache cache `yaml:"cache"` Email email `yaml:"email"` Secrets secrets `yaml:"secrets"` TTL ttl `yaml:"ttl"` @@ -23,6 +24,14 @@ type database struct { Password string `yaml:"password"` } +type cache struct { + Hosts []string `yaml:"hosts"` + Master string `yaml:"master"` + Username string `yaml:"username"` + Password string `yaml:"passowrd"` + DB int `yaml:"db"` +} + type email struct { ResendApiKey string `yaml:"resend_api_key"` From string `yaml:"from"` diff --git a/data/data.go b/data/data.go index 865a2d3..22d32be 100644 --- a/data/data.go +++ b/data/data.go @@ -3,11 +3,13 @@ package data import ( "nixcn-cms/data/drivers" + "github.com/redis/go-redis/v9" log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) var Database *drivers.DBClient +var Redis *redis.UniversalClient func Init() { // Init database @@ -34,6 +36,20 @@ func Init() { if err != nil { log.Error("[Database] Error migrating database: ", err) } - Database = db + + // Init redis conection + rdbAddress := viper.GetStringSlice("cache.hosts") + dsn := drivers.RedisDSN{ + Hosts: rdbAddress, + Master: viper.GetString("cache.master"), + Username: viper.GetString("cache.username"), + Password: viper.GetString("cache.password"), + DB: viper.GetInt("cache.db"), + } + rdb, err := drivers.Redis(dsn) + if err != nil { + log.Fatal("[Redis] Error connecting to Redis: ", err) + } + Redis = rdb } diff --git a/data/drivers/redis.go b/data/drivers/redis.go new file mode 100644 index 0000000..7e403ad --- /dev/null +++ b/data/drivers/redis.go @@ -0,0 +1,22 @@ +package drivers + +import ( + "context" + + "github.com/redis/go-redis/v9" +) + +func Redis(dsn RedisDSN) (*redis.UniversalClient, error) { + // Connect to Redis + rdb := redis.NewUniversalClient(&redis.UniversalOptions{ + Addrs: dsn.Hosts, + MasterName: dsn.Master, + Username: dsn.Username, + Password: dsn.Password, + DB: dsn.DB, + }) + ctx := context.Background() + // Ping Redis + _, err := rdb.Ping(ctx).Result() + return &rdb, err +} diff --git a/data/drivers/types.go b/data/drivers/types.go index 3936553..f662b05 100644 --- a/data/drivers/types.go +++ b/data/drivers/types.go @@ -11,6 +11,14 @@ type ExternalDSN struct { Password string } +type RedisDSN struct { + Hosts []string + Master string + Username string + Password string + DB int +} + type DBClient struct { *gorm.DB } diff --git a/data/event.go b/data/event.go new file mode 100644 index 0000000..8cd11a0 --- /dev/null +++ b/data/event.go @@ -0,0 +1,16 @@ +package data + +import ( + "time" + + "github.com/google/uuid" +) + +type Event struct { + Id uint `json:"id" gorm:"primarykey;autoincrement"` + UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"` + EventId uuid.UUID `json:"event_id" gorm:"type:uuid;uniqueindex;not null"` + Name string `json:"name" gorm:"type:varchar(255);index;not null"` + StartTime time.Time `json:"start_time" gorm:"index"` + EndTime time.Time `json:"end_time" gorm:"index"` +} diff --git a/data/user.go b/data/user.go index 1e6ecdc..d0c084d 100644 --- a/data/user.go +++ b/data/user.go @@ -10,12 +10,12 @@ type User struct { Id uint `json:"id" gorm:"primarykey;autoincrement"` UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"` UserId uuid.UUID `json:"user_id" gorm:"type:uuid;uniqueindex;not null"` - Email string `json:"email" gorm:"uniqueindex;not null"` - Type string `json:"type" gorm:"not null"` + Email string `json:"email" gorm:"type:varchar(255);uniqueindex;not null"` + Type string `json:"type" gorm:"type:varchar(32);index;not null"` Nickname string `json:"nickname"` Subtitle string `json:"subtitle"` Avatar string `json:"avatar"` - Checkin time.Time `json:"checkin"` + Checkin time.Time `json:"checkin" gorm:"index"` } func (self *User) GetByEmail(email string) error { diff --git a/go.mod b/go.mod index 4249f4d..dd3d69e 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,9 @@ require ( github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/sonic v1.14.2 // indirect github.com/bytedance/sonic/loader v0.4.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect @@ -35,6 +37,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.57.1 // indirect + github.com/redis/go-redis/v9 v9.17.2 // indirect github.com/sagikazarmark/locafero v0.11.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect diff --git a/go.sum b/go.sum index e2fb676..e826074 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,14 @@ github.com/bytedance/sonic v1.14.2 h1:k1twIoe97C1DtYUo+fZQy865IuHia4PR5RPiuGPPII github.com/bytedance/sonic v1.14.2/go.mod h1:T80iDELeHiHKSc0C9tubFygiuXoGzrkjKzX2quAx980= github.com/bytedance/sonic/loader v0.4.0 h1:olZ7lEqcxtZygCK9EKYKADnpQoYkRQxaeY2NYzevs+o= github.com/bytedance/sonic/loader v0.4.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= @@ -67,6 +71,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.57.1 h1:25KAAR9QR8KZrCZRThWMKVAwGoiHIrNbT72ULHTuI10= github.com/quic-go/quic-go v0.57.1/go.mod h1:ly4QBAjHA2VhdnxhojRsCUOeJwKYg+taDlos92xb1+s= +github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI= +github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370= github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=