From 6c9f3a2e664ba27f4e64e8a773e67e4eb7ca3003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nam=20Tr=E1=BA=A7n=20=28Software=20Engineer=20Intern=29?= Date: Sat, 8 Jul 2023 08:56:48 +0700 Subject: [PATCH] homework --- api/count.go | 20 ++++++++++ api/login.go | 83 ++++++++++++++++++++++++++++++++++++++++ api/middleware.go | 42 ++++++++++++++++++++ api/ping.go | 31 +++++++++++++++ api/server.go | 64 +++++++++++++++++++++++++++++++ api/top.go | 19 ++++++++++ go.mod | 39 +++++++++++++++++++ go.sum | 97 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 33 ++++++++++++++++ 9 files changed, 428 insertions(+) create mode 100644 api/count.go create mode 100644 api/login.go create mode 100644 api/middleware.go create mode 100644 api/ping.go create mode 100644 api/server.go create mode 100644 api/top.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/api/count.go b/api/count.go new file mode 100644 index 0000000..4e3100d --- /dev/null +++ b/api/count.go @@ -0,0 +1,20 @@ +package api + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) countPingEndpoint(ctx *gin.Context) { + count, err := s.redisDatabase.PFCount(ctx, "/api/v1/ping").Result() + if err != nil { + fmt.Println(err) + } + fmt.Println(count) + ctx.JSON(http.StatusOK, gin.H{ + "flag": 0, + "count": count, + }) +} diff --git a/api/login.go b/api/login.go new file mode 100644 index 0000000..1023cb8 --- /dev/null +++ b/api/login.go @@ -0,0 +1,83 @@ +package api + +import ( + "fmt" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/redis/go-redis/v9" +) + +type LoginRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +func (s *Server) loginEndpoint(ctx *gin.Context) { + + cookie, err := ctx.Cookie("session") + if err == http.ErrNoCookie { + var loginRequest LoginRequest + err = ctx.ShouldBind(&loginRequest) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{ + "message": "error when parse post data", + }) + return + } + + if !checkUserLogin(loginRequest.Username, loginRequest.Password) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "username or password incorrect", + }) + return + } + + exist, err := s.redisDatabase.Exists(ctx, loginRequest.Username).Result() + if err != nil { + ctx.JSON(http.StatusOK, gin.H{ + "message": "check exist redis error", + }) + return + } + if exist == 0 { + ok, err := s.redisDatabase.Set(ctx, loginRequest.Username, 1, 10*time.Second).Result() + if err != nil { + ctx.JSON(http.StatusOK, gin.H{ + "message": "redis fail", + }) + return + } + fmt.Println(ok) + } + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + }) + return + } + + if err != nil { + ctx.JSON(http.StatusOK, gin.H{ + "message": "fail", + }) + return + } + result, err := s.redisDatabase.Get(ctx, cookie).Result() + if err == redis.Nil { + ctx.JSON(http.StatusOK, gin.H{ + "message": "need login again", + }) + return + } + if result == "-1" { + ctx.JSON(http.StatusOK, gin.H{ + "message": "need login again", + }) + return + } + ctx.JSON(http.StatusOK, gin.H{ + "message": "logined", + "result": result, + }) +} diff --git a/api/middleware.go b/api/middleware.go new file mode 100644 index 0000000..1b36809 --- /dev/null +++ b/api/middleware.go @@ -0,0 +1,42 @@ +package api + +import ( + "context" + "fmt" + + "github.com/gin-gonic/gin" +) + +func (s *Server) increaseCountBlock() gin.HandlerFunc { + return func(ctx *gin.Context) { + var pingRequest PingRequest + err := ctx.ShouldBind(&pingRequest) + if err != nil { + fmt.Println(err) + return + } + err = s.redisDatabase.PFAdd(context.Background(), ctx.FullPath(), pingRequest.Username).Err() + fmt.Println(ctx.FullPath()) + if err != nil { + + } + ctx.Next() + } + +} + +func (s *Server) increaseCountRanking() gin.HandlerFunc { + return func(ctx *gin.Context) { + var pingRequest PingRequest + err := ctx.ShouldBind(&pingRequest) + if err != nil { + fmt.Println(err) + return + } + err = s.redisDatabase.ZIncrBy(context.Background(), "visitor", 1, fmt.Sprint(pingRequest.Username)).Err() + if err != nil { + + } + ctx.Next() + } +} diff --git a/api/ping.go b/api/ping.go new file mode 100644 index 0000000..01cd497 --- /dev/null +++ b/api/ping.go @@ -0,0 +1,31 @@ +package api + +import ( + "net/http" + "time" + + "github.com/bsm/redislock" + "github.com/gin-gonic/gin" +) + +type PingRequest struct { + Username string `json:"username"` +} + +func (s *Server) pingEndpoint(ctx *gin.Context) { + redisLock := redislock.New(s.redisDatabase) + lock, err := redisLock.Obtain(ctx, "api:ping", 10*time.Second, nil) + if err == redislock.ErrNotObtained { + ctx.JSON(http.StatusBadRequest, gin.H{ + "message": "only 1 person can ping at a time", + }) + return + } + defer lock.Release(ctx) + + time.Sleep(5 * time.Second) + + ctx.JSON(http.StatusOK, gin.H{ + "message": "pong", + }) +} diff --git a/api/server.go b/api/server.go new file mode 100644 index 0000000..3660b7a --- /dev/null +++ b/api/server.go @@ -0,0 +1,64 @@ +package api + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/go-redis/redis_rate/v10" + "github.com/redis/go-redis/v9" +) + +type Server struct { + redisDatabase *redis.Client + router *gin.Engine +} + +func NewServer(rdb *redis.Client) *Server { + server := &Server{redisDatabase: rdb} + router := gin.Default() + + v1 := router.Group("/api/v1") + { + v1.POST("/ping", server.increaseCountRanking(), server.increaseCountBlock(), server.rateLimitUser(), server.pingEndpoint) + v1.POST("/login", server.loginEndpoint) + v1.GET("/top", server.topEndpoint) + v1.GET("/count", server.countPingEndpoint) + } + + server.router = router + return server +} + +func (s *Server) StartServer(address string) error { + return s.router.Run(address) +} + +func checkUserLogin(username string, password string) bool { + if username == "admin" && password == "admin" { + return true + } + return false +} + +func (s *Server) rateLimitUser() gin.HandlerFunc { + return func(ctx *gin.Context) { + limiter := redis_rate.NewLimiter(s.redisDatabase) + var pingRequest PingRequest + res, err := limiter.Allow(ctx, fmt.Sprint(pingRequest.Username), redis_rate.PerMinute(2)) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{ + "message": "redis error", + }) + ctx.Abort() + } + + if res.Allowed == 0 { + ctx.JSON(http.StatusTooManyRequests, gin.H{ + "message": "too many requests", + }) + ctx.Abort() + } + ctx.Next() + } +} diff --git a/api/top.go b/api/top.go new file mode 100644 index 0000000..fcc22fb --- /dev/null +++ b/api/top.go @@ -0,0 +1,19 @@ +package api + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) topEndpoint(ctx *gin.Context) { + result, err := s.redisDatabase.ZRevRangeWithScores(ctx, "visitor", 0, 10).Result() + if err != nil { + fmt.Println(err) + } + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": result, + }) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..10b605a --- /dev/null +++ b/go.mod @@ -0,0 +1,39 @@ +module github.com/thnam4500/identity + +go 1.20 + +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/go-redis/redis_rate/v10 v10.0.1 + github.com/redis/go-redis/v9 v9.0.5 +) + +require ( + github.com/bsm/redislock v0.9.3 + github.com/bytedance/sonic v1.9.2 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.10.0 // indirect + golang.org/x/net v0.11.0 // indirect + golang.org/x/sys v0.9.0 // indirect + golang.org/x/text v0.10.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e02787c --- /dev/null +++ b/go.sum @@ -0,0 +1,97 @@ +github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= +github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= +github.com/bsm/redislock v0.9.3 h1:osmvugkXGiLDEhzUPdM0EUtKpTEgLLuli4Ky2Z4vx38= +github.com/bsm/redislock v0.9.3/go.mod h1:Epf7AJLiSFwLCiZcfi6pWFO/8eAYrYpQXFxEDPoDeAk= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM= +github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= +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/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +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/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k= +github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-redis/redis_rate/v10 v10.0.1 h1:calPxi7tVlxojKunJwQ72kwfozdy25RjA0bCj1h0MUo= +github.com/go-redis/redis_rate/v10 v10.0.1/go.mod h1:EMiuO9+cjRkR7UvdvwMO7vbgqJkltQHtwbdIQvaBKIU= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= +github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= +github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= +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.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= +golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= +golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= +golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..bc363cf --- /dev/null +++ b/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + + "github.com/redis/go-redis/v9" + "github.com/thnam4500/identity/api" +) + +type PingRequest struct { + Username int `json:"user_name"` +} + +type LoginRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +var rdb *redis.Client + +func main() { + rdb = redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password set + DB: 0, // use default DB + }) + defer rdb.Close() + s := api.NewServer(rdb) + err := s.StartServer(":3000") + if err != nil { + log.Fatalln(err) + } +}