From 3dd84e6932281458ba3c610e1568bdd5e42a443c Mon Sep 17 00:00:00 2001 From: ericprd Date: Sat, 22 Feb 2025 16:15:38 +0800 Subject: [PATCH] feat: initial router on main function --- cmd/gorm/main.go | 4 +-- cmd/legalgo/main.go | 20 ++++-------- go.mod | 25 +++++++-------- go.sum | 45 ++++++++++++--------------- internal/api/http/router.go | 31 +++++++++++++++++++ internal/config/chi_router.go | 51 +++++++++++++++++++++++++++++++ internal/config/server.go | 4 +++ internal/utilities/utils/jwt.go | 12 ++++++++ internal/utilities/utils/redis.go | 13 ++++++++ 9 files changed, 150 insertions(+), 55 deletions(-) create mode 100644 internal/api/http/router.go create mode 100644 internal/config/chi_router.go create mode 100644 internal/config/server.go create mode 100644 internal/utilities/utils/redis.go diff --git a/cmd/gorm/main.go b/cmd/gorm/main.go index d3103f9..7905807 100644 --- a/cmd/gorm/main.go +++ b/cmd/gorm/main.go @@ -1,5 +1,5 @@ package main func main() { - -} \ No newline at end of file + +} diff --git a/cmd/legalgo/main.go b/cmd/legalgo/main.go index 6852462..ce4e38e 100644 --- a/cmd/legalgo/main.go +++ b/cmd/legalgo/main.go @@ -1,22 +1,14 @@ package main import ( - "context" - "log" - + internalhttp "github.com/ardeman/project-legalgo-go/internal/api/http" + pkgconfig "github.com/ardeman/project-legalgo-go/internal/config" "go.uber.org/fx" ) func main() { - ctx := context.Background() - - app, err := fx.New() - - if err != nil { - log.Fatal(err) - } - - if err := app.Run(ctx); err != nil { - log.Fatal(err) - } + fx.New( + fx.Provide(internalhttp.Module), + fx.Invoke(pkgconfig.Router), + ) } diff --git a/go.mod b/go.mod index e8bb435..adeacf5 100644 --- a/go.mod +++ b/go.mod @@ -3,23 +3,20 @@ module github.com/ardeman/project-legalgo-go go 1.24.0 require ( + github.com/golang-jwt/jwt/v5 v5.2.1 + go.uber.org/fx v1.23.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/go-chi/chi/v5 v5.2.1 // indirect - github.com/golang-jwt/jwt/v5 v5.2.1 // 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.7.2 // indirect - github.com/jackc/puddle/v2 v2.2.2 // indirect - github.com/jinzhu/inflection v1.0.0 // indirect - github.com/jinzhu/now v1.1.5 // indirect - github.com/joho/godotenv v1.5.1 // indirect + github.com/go-chi/cors v1.2.1 + github.com/redis/go-redis/v9 v9.7.1 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect go.uber.org/dig v1.18.0 // indirect - go.uber.org/fx v1.23.0 // indirect go.uber.org/multierr v1.10.0 // indirect go.uber.org/zap v1.26.0 // indirect - golang.org/x/crypto v0.34.0 // indirect - golang.org/x/sync v0.11.0 // indirect + golang.org/x/sync v0.11.0 golang.org/x/sys v0.30.0 // indirect - golang.org/x/text v0.22.0 // indirect - gorm.io/driver/postgres v1.5.11 // indirect - gorm.io/gorm v1.25.12 // indirect ) diff --git a/go.sum b/go.sum index 4fca432..9856691 100644 --- a/go.sum +++ b/go.sum @@ -1,47 +1,42 @@ +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/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/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8= github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= -github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= -github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= -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= -github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= -github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= -github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= -github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +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.7.1 h1:4LhKRCIduqXqtvCUlaq9c8bdHOkICjDMrr1+Zb3osAc= +github.com/redis/go-redis/v9 v9.7.1/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -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.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw= go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= go.uber.org/fx v1.23.0 h1:lIr/gYWQGfTwGcSXWXu4vP5Ws6iqnNEIY+F/aFzCKTg= go.uber.org/fx v1.23.0/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= -golang.org/x/crypto v0.34.0 h1:+/C6tk6rf/+t5DhUketUbD1aNGqiSX3j15Z6xuIDlBA= -golang.org/x/crypto v0.34.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ= golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= -golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= 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= -gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= -gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= -gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= -gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/api/http/router.go b/internal/api/http/router.go new file mode 100644 index 0000000..4e73cef --- /dev/null +++ b/internal/api/http/router.go @@ -0,0 +1,31 @@ +package internalhttp + +import ( + "github.com/go-chi/chi/v5" + "github.com/go-chi/cors" + "go.uber.org/fx" + + chimware "github.com/go-chi/chi/v5/middleware" +) + +var Module = fx.Module("router", + fx.Provide(initRouter), +) + +func initRouter() chi.Router { + router := chi.NewRouter() + localCors := cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"}, + AllowedHeaders: []string{"Accept", "Content-Type", "Authorization", "X-Device", "X-Client-Ip"}, + AllowCredentials: true, + Debug: true, + }) + + router.Use( + localCors.Handler, + chimware.RequestID, + ) + + return router +} diff --git a/internal/config/chi_router.go b/internal/config/chi_router.go new file mode 100644 index 0000000..d1a7a6e --- /dev/null +++ b/internal/config/chi_router.go @@ -0,0 +1,51 @@ +package pkgconfig + +import ( + "context" + "fmt" + "net/http" + "os/signal" + "syscall" + "time" + + "github.com/go-chi/chi/v5" + "github.com/sirupsen/logrus" + "golang.org/x/sync/errgroup" +) + +func Router(apiRouter chi.Router) { + mainRouter := chi.NewRouter() + + mainRouter.Mount("/", apiRouter) + mainCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + svr := &http.Server{ + Addr: fmt.Sprintf(":%d", APP_PORT), + Handler: mainRouter, + } + + group, groupCtx := errgroup.WithContext(mainCtx) + + group.Go(func() error { + logrus.Infof("Listening to port %d", APP_PORT) + return svr.ListenAndServe() + }) + + group.Go(func() error { + <-groupCtx.Done() + + ctxTimeout, cancel := context.WithTimeout(mainCtx, GRACEFULL_TIMEOUT*time.Second) + defer cancel() + + svr.Shutdown(ctxTimeout) + + return nil + }) + + if err := group.Wait(); err != nil { + logrus.Errorf("system exit, reason: %v", err.Error()) + } else { + logrus.Info("system exit normally") + } +} diff --git a/internal/config/server.go b/internal/config/server.go new file mode 100644 index 0000000..5254372 --- /dev/null +++ b/internal/config/server.go @@ -0,0 +1,4 @@ +package pkgconfig + +const APP_PORT = 3000 +const GRACEFULL_TIMEOUT = 20 diff --git a/internal/utilities/utils/jwt.go b/internal/utilities/utils/jwt.go index b94b2f6..68df162 100644 --- a/internal/utilities/utils/jwt.go +++ b/internal/utilities/utils/jwt.go @@ -1,6 +1,8 @@ package utils import ( + "time" + jwtclaimenum "github.com/ardeman/project-legalgo-go/internal/enums/jwt" timeutils "github.com/ardeman/project-legalgo-go/internal/utilities/time_utils" "github.com/golang-jwt/jwt/v5" @@ -25,3 +27,13 @@ func GenerateToken(options ...ClaimOption) (string, error) { return token.SignedString(jwtSecret) } + +func GenerateToken2(username string) (string, error) { + now := timeutils.Now() + token := jwt.New(jwt.SigningMethodES256) + claims := token.Claims.(jwt.MapClaims) + claims["username"] = username + claims["exp"] = now.Add(time.Hour).Unix() + + return token.SignedString(jwtSecret) +} diff --git a/internal/utilities/utils/redis.go b/internal/utilities/utils/redis.go new file mode 100644 index 0000000..bfa3134 --- /dev/null +++ b/internal/utilities/utils/redis.go @@ -0,0 +1,13 @@ +package utils + +import ( + "context" + "time" + + "github.com/redis/go-redis/v9" +) + +func StoreToken(ctx context.Context, rdb *redis.Client, token, username string) error { + // return rdb.Set(ctx context.Context, key string, value interface{}, expiration time.Duration) + return rdb.Set(ctx, "token"+username, token, time.Hour).Err() +}