From ee4a1b3fabc2c8c823506acbfd40001167fed6fc Mon Sep 17 00:00:00 2001 From: "duc.pham" Date: Mon, 5 Feb 2024 13:35:16 +0700 Subject: [PATCH] assignment redis --- app.env | 9 ++ cmd/database.go | 64 ++++++++++ cmd/root.go | 44 +++++++ config/config.go | 34 +++++ db/migrate | 0 db/mysql.go | 20 +++ db/redis.go | 17 +++ go.mod | 51 ++++++++ go.sum | 100 +++++++++++++++ internal/auth/adapter/http/controller.go | 152 +++++++++++++++++++++++ internal/auth/application/auth.go | 71 +++++++++++ internal/auth/application/auth_errors.go | 12 ++ internal/auth/application/count_ping.go | 20 +++ internal/auth/application/count_user.go | 21 ++++ internal/auth/application/ping.go | 39 ++++++ internal/auth/application/rate_litmit.go | 24 ++++ internal/auth/application/top_user.go | 39 ++++++ internal/auth/domain/user.go | 39 ++++++ internal/auth/domain/user_dto.go | 6 + internal/auth/router.go | 18 +++ main.go | 23 ++++ 21 files changed, 803 insertions(+) create mode 100644 app.env create mode 100644 cmd/database.go create mode 100644 cmd/root.go create mode 100644 config/config.go create mode 100644 db/migrate create mode 100644 db/mysql.go create mode 100644 db/redis.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/auth/adapter/http/controller.go create mode 100644 internal/auth/application/auth.go create mode 100644 internal/auth/application/auth_errors.go create mode 100644 internal/auth/application/count_ping.go create mode 100644 internal/auth/application/count_user.go create mode 100644 internal/auth/application/ping.go create mode 100644 internal/auth/application/rate_litmit.go create mode 100644 internal/auth/application/top_user.go create mode 100644 internal/auth/domain/user.go create mode 100644 internal/auth/domain/user_dto.go create mode 100644 internal/auth/router.go create mode 100644 main.go diff --git a/app.env b/app.env new file mode 100644 index 0000000..62199d0 --- /dev/null +++ b/app.env @@ -0,0 +1,9 @@ +SERVER_PORT=8080 + +SQL_HOST=localhost +SQL_PORT=3306 +SQL_USER=root +SQL_PASSWORD=local-mysql +SQL_DATABASE=test + +REDIS_URL=localhost:6379 diff --git a/cmd/database.go b/cmd/database.go new file mode 100644 index 0000000..c05e9f4 --- /dev/null +++ b/cmd/database.go @@ -0,0 +1,64 @@ +package cmd + +import ( + "fmt" + "learn-redis/config" + "learn-redis/db" + "log" + "os" + + "github.com/spf13/cobra" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +var databaseCmd = &cobra.Command{ + Use: "check_connect_sql", + Short: "check mysql", + Long: "Check connect to database", + Run: func(_ *cobra.Command, _ []string) { + c := config.GetConfig() + _, err := db.ConnectionToDB(c) + if err != nil { + log.Fatalln("connect data failed ", err) + os.Exit(1) + } + log.Println("connected mysql") + os.Exit(0) + }, +} + +var redisCmd = &cobra.Command{ + Use: "check_connect_redis", + Short: "check redis", + Long: "Check connect to redis", + Run: func(_ *cobra.Command, _ []string) { + c := config.GetConfig() + _, err := db.ConnectionRedis(c) + if err != nil { + log.Fatal("redis connect failed ", err) + os.Exit(1) + } + log.Println("connected redis") + os.Exit(0) + }, +} + +var migrageGorm = &cobra.Command{ + Use: "migrate_gorm", + Short: "Gorm migrate gorm", + Long: `Save gorm auto migrate to file /db/migrate/{timestamp}.sql`, + Run: func(_ *cobra.Command, _ []string) { + conf := config.GetConfig() + var _db *gorm.DB + var err error + _db, err = db.ConnectionToDB(conf) + if err != nil { + log.Fatal("db connected failed ", err) + os.Exit(1) + } + _db.Logger.LogMode(logger.Info) + sql := _db.Statement.SQL + fmt.Println(sql) + }, +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..f3cf057 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "fmt" + "learn-redis/config" + "learn-redis/db" + "learn-redis/internal/auth" + "log" + "os" + + "github.com/gofiber/fiber/v2" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "app", + Short: "Demo redis", + Long: "Demo redis with golang, fiber, gorm, mysql", + Run: func(_ *cobra.Command, _ []string) { + _db, err := db.ConnectionToDB(config.GetConfig()) + if err != nil { + log.Fatal(err) + } + _rdb, err := db.ConnectionRedis(config.GetConfig()) + if err != nil { + log.Fatal(err) + } + app := fiber.New() + group := app.Group("/vi/api") + auth.Router(group, _db, _rdb) + addr := fmt.Sprintf(":%d", config.GetConfig().ServerPort) + app.Listen(addr) + }, +} + +func Execute() { + rootCmd.AddCommand(databaseCmd) + rootCmd.AddCommand(redisCmd) + rootCmd.AddCommand(migrageGorm) + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..36094a0 --- /dev/null +++ b/config/config.go @@ -0,0 +1,34 @@ +package config + +import "github.com/spf13/viper" + +type Config struct { + SQLHost string `mapstructure:"SQL_HOST"` + SQLUser string `mapstructure:"SQL_User"` + SQLPass string `mapstructure:"SQL_PASSWORD"` + SQLDatabase string `mapstructure:"SQL_DATABASE"` + RedisUrl string `mapstructure:"REDIS_URL"` + + ServerPort int `mapstructure:"SERVER_PORT"` + SQLPort int `mapstructure:"SQL_PORT"` +} + +var _conf = Config{} + +func LoadConfig(path string) (conf Config, err error) { + viper.AddConfigPath(path) + viper.SetConfigType("env") + viper.SetConfigName("app") + viper.AutomaticEnv() + err = viper.ReadInConfig() + if err != nil { + return + } + viper.Unmarshal(&conf) + _conf = conf + return +} + +func GetConfig() Config { + return _conf +} diff --git a/db/migrate b/db/migrate new file mode 100644 index 0000000..e69de29 diff --git a/db/mysql.go b/db/mysql.go new file mode 100644 index 0000000..ca5c613 --- /dev/null +++ b/db/mysql.go @@ -0,0 +1,20 @@ +package db + +import ( + "fmt" + "learn-redis/config" + "log" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +func ConnectionToDB(conf config.Config) (*gorm.DB, error) { + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", + conf.SQLUser, conf.SQLPass, conf.SQLHost, conf.SQLPort, conf.SQLDatabase) + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatal("Connect to mysql database failed:", err) + } + return db, err +} diff --git a/db/redis.go b/db/redis.go new file mode 100644 index 0000000..bceeb26 --- /dev/null +++ b/db/redis.go @@ -0,0 +1,17 @@ +package db + +import ( + "context" + "learn-redis/config" + + "github.com/redis/go-redis/v9" +) + +func ConnectionRedis(conf config.Config) (*redis.Client, error) { + rdb := redis.NewClient(&redis.Options{ + Addr: conf.RedisUrl, + }) + ctx := context.Background() + err := rdb.Ping(ctx).Err() + return rdb, err +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..26e3bc2 --- /dev/null +++ b/go.mod @@ -0,0 +1,51 @@ +module learn-redis + +go 1.21.6 + +require github.com/spf13/cobra v1.8.0 + +require ( + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/klauspost/compress v1.17.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +require ( + github.com/gofiber/fiber/v2 v2.52.0 + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/redis/go-redis/v9 v9.4.0 + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.18.2 + gorm.io/driver/mysql v1.5.2 + gorm.io/gorm v1.25.6 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3d2fb5c --- /dev/null +++ b/go.sum @@ -0,0 +1,100 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +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.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE= +github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= +github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk= +github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs= +gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gorm.io/gorm v1.25.6 h1:V92+vVda1wEISSOMtodHVRcUIOPYa2tgQtyF+DfFx+A= +gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/internal/auth/adapter/http/controller.go b/internal/auth/adapter/http/controller.go new file mode 100644 index 0000000..927802d --- /dev/null +++ b/internal/auth/adapter/http/controller.go @@ -0,0 +1,152 @@ +package http + +import ( + "context" + "learn-redis/internal/auth/application" + "learn-redis/internal/auth/domain" + "net/http" + "strconv" + + "github.com/gofiber/fiber/v2" + "github.com/redis/go-redis/v9" + "gorm.io/gorm" +) + +type Biz interface { + CreateUser(string, string, string, string) (*domain.User, error) + Login(string, string) (string, error) + GetCountUser() (int64, error) + GetPingCount(context.Context, int64) (int, error) + GetTopUser(int) ([]domain.UserItemTopUser, error) + Ping(string) (int, error) + GetUserFromSession(string) (*domain.User, error) +} +type Api struct { + biz Biz +} + +func NewApi(db *gorm.DB, rdb *redis.Client) Api { + biz := application.NewAuthServ(db, rdb) + return Api{ + biz: biz, + } +} + +type CreateUserBody struct { + Email string `json:"email"` + Password string `json:"password"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` +} + +func (api *Api) Register(c *fiber.Ctx) error { + var body CreateUserBody + if err := c.BodyParser(&body); err != nil { + c.Status(http.StatusBadRequest) + return err + } + _, err := api.biz.CreateUser(body.Email, body.Password, body.FirstName, body.LastName) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusCreated).JSON(fiber.Map{ + "data": "OK", + }) +} + +type LoginBody struct { + Email string `json:"email"` + Password string `json:"password"` +} + +func (api *Api) Login(c *fiber.Ctx) error { + body := new(LoginBody) + if err := c.BodyParser(body); err != nil { + c.Status(http.StatusBadRequest) + return err + } + sessionId, err := api.biz.Login(body.Email, body.Password) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusOK).JSON(fiber.Map{ + "data": fiber.Map{ + "session_id": sessionId, + }, + }) +} + +type SessionBody struct { + SessionId string `json:"session_id"` +} + +func (api *Api) Ping(c *fiber.Ctx) error { + body := new(SessionBody) + if err := c.BodyParser(body); err != nil { + c.Status(http.StatusBadRequest) + return err + } + count, err := api.biz.Ping(body.SessionId) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusOK).JSON(fiber.Map{ + "data": fiber.Map{ + "count": count, + "message": "Pong", + }, + }) +} + +func (api *Api) CountPing(c *fiber.Ctx) error { + body := new(SessionBody) + if err := c.BodyParser(body); err != nil { + c.Status(http.StatusBadRequest) + return err + } + user, err := api.biz.GetUserFromSession(body.SessionId) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + id := int64(user.ID) + count, err := api.biz.GetPingCount(c.Context(), id) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusOK).JSON(fiber.Map{ + "data": fiber.Map{"count": count}, + }) +} + +func (api *Api) CountUser(c *fiber.Ctx) error { + count, err := api.biz.GetCountUser() + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusOK).JSON(fiber.Map{ + "data": fiber.Map{"user": count}, + }) +} + +func (api *Api) TopUser(c *fiber.Ctx) error { + limitSrt := c.Params("limit", "10") + limit, err := strconv.Atoi(limitSrt) + if err != nil { + c.Status(http.StatusBadRequest) + return err + } + topUser, err := api.biz.GetTopUser(limit) + if err != nil { + c.Status(http.StatusInternalServerError) + return err + } + return c.Status(http.StatusOK).JSON(fiber.Map{ + "data": topUser, + }) +} diff --git a/internal/auth/application/auth.go b/internal/auth/application/auth.go new file mode 100644 index 0000000..9b48df7 --- /dev/null +++ b/internal/auth/application/auth.go @@ -0,0 +1,71 @@ +package application + +import ( + "context" + "errors" + "fmt" + "learn-redis/internal/auth/domain" + "time" + + "github.com/redis/go-redis/v9" + "gorm.io/gorm" +) + +type authServ struct { + db *gorm.DB + rdb *redis.Client +} + +func NewAuthServ(db *gorm.DB, rdb *redis.Client) *authServ { + return &authServ{ + db: db, + rdb: rdb, + } +} + +func (s *authServ) FindUser(email string) (*domain.User, error) { + var user *domain.User + err := s.db.Where("email = ?", email).First(user).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, ErrUserNotFound + } + if err != nil { + // log error + return nil, ErrDBInternal + } + return user, err +} + +func (s *authServ) CreateUser(email, pass, firstName, lastName string) (*domain.User, error) { + user := domain.NewUser(email, pass, firstName, lastName) + err := s.db.Create(user).Error + if err != nil { + // log error + return nil, ErrDBInternal + } + return user, err +} + +func (s *authServ) Login(email, pass string) (sessionId string, err error) { + var user *domain.User + user, err = s.FindUser(email) + if err != nil { + return "", err + } + verifyPass := user.VerifyPass(pass) + if !verifyPass { + return "", ErrAuth + } + key := fmt.Sprintf("session_%d", time.Now().UnixNano()) + s.rdb.Set(context.Background(), key, *user, 2*time.Hour) + return key, nil +} + +func (s *authServ) GetUserFromSession(session string) (*domain.User, error) { + userData, ok := s.db.Get("session_" + session) + if !ok { + return nil, ErrSessionInvalid + } + user := userData.(domain.User) + return &user, nil +} diff --git a/internal/auth/application/auth_errors.go b/internal/auth/application/auth_errors.go new file mode 100644 index 0000000..47dfd63 --- /dev/null +++ b/internal/auth/application/auth_errors.go @@ -0,0 +1,12 @@ +package application + +import "errors" + +var ( + ErrUserNotFound = errors.New("user not found") + ErrDBInternal = errors.New("internal server") + ErrAuth = errors.New("invalid pass or email") + ErrSessionInvalid = errors.New("session invalid, try login again") + ErrPing = errors.New("try ping again later") + ErrRateLimit = errors.New("ping to much") +) diff --git a/internal/auth/application/count_ping.go b/internal/auth/application/count_ping.go new file mode 100644 index 0000000..0ca058f --- /dev/null +++ b/internal/auth/application/count_ping.go @@ -0,0 +1,20 @@ +package application + +import ( + "context" + "fmt" +) + +const CountUserPingKey = "ping_count" + +func (s *authServ) AddPingToCount(ctx context.Context, id int64) error { + key := fmt.Sprintf("%d", id) + err := s.rdb.ZIncrBy(ctx, CountUserPingKey, 1, key).Err() + return err +} + +func (s *authServ) GetPingCount(ctx context.Context, id int64) (int, error) { + key := fmt.Sprintf("%d", id) + rs, err := s.rdb.ZScore(ctx, CountUserPingKey, key).Result() + return int(rs), err +} diff --git a/internal/auth/application/count_user.go b/internal/auth/application/count_user.go new file mode 100644 index 0000000..1075307 --- /dev/null +++ b/internal/auth/application/count_user.go @@ -0,0 +1,21 @@ +package application + +import ( + "context" + "fmt" +) + +const CountPing = "ping_count_user" + +func (s *authServ) AddPingCountUser(id int64) error { + ctx := context.Background() + key := fmt.Sprintf("%d", id) + err := s.rdb.PFAdd(ctx, CountPing, key).Err() + return err +} + +func (s *authServ) GetCountUser() (int64, error) { + ctx := context.Background() + rs, err := s.rdb.PFCount(ctx, CountPing).Result() + return rs, err +} diff --git a/internal/auth/application/ping.go b/internal/auth/application/ping.go new file mode 100644 index 0000000..fa06ef3 --- /dev/null +++ b/internal/auth/application/ping.go @@ -0,0 +1,39 @@ +package application + +import ( + "context" + "time" +) + +const PingLockKey = "ping_lock" + +func (s *authServ) Ping(session string) (int, error) { + ctx := context.Background() + rs, err := s.rdb.SetNX(ctx, PingLockKey, true, 0).Result() + defer s.rdb.Del(ctx, PingLockKey) + if !rs { + return 0, ErrPing + } + if err != nil { + return 0, ErrDBInternal + } + user, err := s.GetUserFromSession(session) + if err != nil { + return 0, err + } + id := int64(user.ID) + err = s.RateLimit(ctx, id) + if err != nil { + return 0, err + } + + // add ping count + s.AddPingToCount(ctx, id) + + // add user count + s.AddPingCountUser(id) + count, _ := s.GetPingCount(ctx, id) + + defer time.Sleep(5 * time.Second) + return count, nil +} diff --git a/internal/auth/application/rate_litmit.go b/internal/auth/application/rate_litmit.go new file mode 100644 index 0000000..0cf5ed3 --- /dev/null +++ b/internal/auth/application/rate_litmit.go @@ -0,0 +1,24 @@ +package application + +import ( + "context" + "fmt" + "time" +) + +const RateLimitKey = "ping_rate_limit:" + +func (s *authServ) RateLimit(ctx context.Context, id int64) error { + key := fmt.Sprintf("%s:%d", RateLimitKey, id) + count, err := s.rdb.Incr(ctx, key).Result() + if err != nil { + return ErrDBInternal + } + if count > 2 { + return ErrRateLimit + } + if count == 1 { + s.rdb.ExpireNX(ctx, key, 2*time.Minute) + } + return nil +} diff --git a/internal/auth/application/top_user.go b/internal/auth/application/top_user.go new file mode 100644 index 0000000..84a7b18 --- /dev/null +++ b/internal/auth/application/top_user.go @@ -0,0 +1,39 @@ +package application + +import ( + "context" + "learn-redis/internal/auth/domain" + "strconv" + + "github.com/redis/go-redis/v9" +) + +func (s *authServ) GetTopUser(limit int) ([]domain.UserItemTopUser, error) { + ctx := context.Background() + listZ, err := s.rdb.ZRevRangeByScoreWithScores(ctx, CountUserPingKey, &redis.ZRangeBy{ + Offset: 0, + Count: int64(limit), + }).Result() + if err != nil { + return nil, err + } + topUser := make([]domain.UserItemTopUser, len(listZ)) + for i, v := range listZ { + var user domain.User + idStr := v.Member.(string) + id, ok := strconv.Atoi(idStr) + if ok != nil { + return nil, ok + } + if err := s.db.Find(&user, id).Error; err != nil { + return nil, err + } + item := domain.UserItemTopUser{ + User: user, + Count: int64(v.Score), + } + topUser[i] = item + + } + return topUser, nil +} diff --git a/internal/auth/domain/user.go b/internal/auth/domain/user.go new file mode 100644 index 0000000..21384a2 --- /dev/null +++ b/internal/auth/domain/user.go @@ -0,0 +1,39 @@ +package domain + +import ( + "fmt" + "time" + + "gorm.io/gorm" +) + +const TableUser = "users" + +type User struct { + gorm.Model + FirstName string `gorm:"type:varchar(50);" json:"first_name"` + LastName string `gorm:"varchar(50);" json:"last_name"` + Email string `gorm:"type:varchar(50);unique;" json:"email"` + Salt string `gorm:"not null;" json:"-"` + HashedPass string `gorm:"column:hashed_pass;not null;" json:"-"` +} + +func (User) TableName() string { + return TableUser +} + +func NewUser(email, pass, firstName, lastName string) *User { + salt := fmt.Sprintf("%d", time.Now().UnixMilli()) + + return &User{ + FirstName: firstName, + LastName: lastName, + Email: email, + Salt: salt, + HashedPass: pass + salt, + } +} + +func (u User) VerifyPass(pass string) bool { + return pass+u.Salt == u.HashedPass +} diff --git a/internal/auth/domain/user_dto.go b/internal/auth/domain/user_dto.go new file mode 100644 index 0000000..83905d6 --- /dev/null +++ b/internal/auth/domain/user_dto.go @@ -0,0 +1,6 @@ +package domain + +type UserItemTopUser struct { + User + Count int64 `json:"ping_count"` +} diff --git a/internal/auth/router.go b/internal/auth/router.go new file mode 100644 index 0000000..6f7e1a7 --- /dev/null +++ b/internal/auth/router.go @@ -0,0 +1,18 @@ +package auth + +import ( + "learn-redis/internal/auth/adapter/http" + + "github.com/gofiber/fiber/v2" + "github.com/redis/go-redis/v9" + "gorm.io/gorm" +) + +func Router(r fiber.Router, db *gorm.DB, rdb *redis.Client) { + api := http.NewApi(db, rdb) + r.Post("/register", api.Register) + r.Post("/login", api.Login) + r.Get("/ping/top-user", api.TopUser) + r.Get("/ping/count", api.CountUser) + r.Get("/ping", api.Ping) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..06e06e5 --- /dev/null +++ b/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "learn-redis/cmd" + "learn-redis/config" + "os" + "os/signal" + "syscall" +) + +func main() { + fmt.Println("Start app") + exit := make(chan os.Signal, 1) + signal.Notify(exit, os.Interrupt, syscall.SIGTERM) + go func() { + <-exit + fmt.Println("Exit app") + os.Exit(0) + }() + config.LoadConfig(".") + cmd.Execute() +}