From b1615fa55d0a0332322e6821e0ea895f5f0c5e4d Mon Sep 17 00:00:00 2001 From: mamko3b228 Date: Mon, 2 Sep 2024 23:01:46 +0300 Subject: [PATCH] grpc --- cmd/shortener/main.go | 60 ++++++++++++++++++++++++------------------- config/config.go | 7 +++++ config/config.json | 3 ++- config/json_config.go | 4 +++ go.mod | 3 +++ go.sum | 24 ++++++++--------- 6 files changed, 60 insertions(+), 41 deletions(-) diff --git a/cmd/shortener/main.go b/cmd/shortener/main.go index 88fbc44..ee6320a 100644 --- a/cmd/shortener/main.go +++ b/cmd/shortener/main.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "github.com/nglmq/url-shortener/internal/app/cert" + grpcserver "github.com/nglmq/url-shortener/internal/app/grpc/server" "log" "net/http" _ "net/http/pprof" @@ -12,6 +12,8 @@ import ( "os/signal" "syscall" + "github.com/nglmq/url-shortener/internal/app/cert" + "github.com/nglmq/url-shortener/config" "github.com/nglmq/url-shortener/internal/app/server" ) @@ -32,37 +34,43 @@ func main() { log.Fatal(err) } - srv := &http.Server{ - Addr: config.FlagRunAddr, - Handler: r, - } + if config.GRPCServer { + if err := grpcserver.StartGRPCServer(); err != nil { + log.Fatal(err) + } + } else { + srv := &http.Server{ + Addr: config.FlagRunAddr, + Handler: r, + } + + idleConnsClosed := make(chan struct{}) + sigint := make(chan os.Signal, 3) - idleConnsClosed := make(chan struct{}) - sigint := make(chan os.Signal, 3) + signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) - signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + go func() { + <-sigint - go func() { - <-sigint + if err := srv.Shutdown(context.Background()); err != nil { + log.Printf("HTTP server Shutdown: %v", err) + } + close(idleConnsClosed) + }() - if err := srv.Shutdown(context.Background()); err != nil { - log.Printf("HTTP server Shutdown: %v", err) + if config.EnableHTTPS { + cert.CertGen() + fmt.Printf("Starting server: %s", config.FlagRunAddr) + err = srv.ListenAndServeTLS("cert.pem", "key.pem") + } else { + err = srv.ListenAndServe() } - close(idleConnsClosed) - }() - if config.EnableHTTPS { - cert.CertGen() - fmt.Printf("Starting server: %s", config.FlagRunAddr) - err = srv.ListenAndServeTLS("cert.pem", "key.pem") - } else { - err = srv.ListenAndServe() - } + if !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("HTTP server ListenAndServe: %v", err) + } - if !errors.Is(err, http.ErrServerClosed) { - log.Fatalf("HTTP server ListenAndServe: %v", err) + <-idleConnsClosed + fmt.Println("Server Shutdown gracefully") } - - <-idleConnsClosed - fmt.Println("Server Shutdown gracefully") } diff --git a/config/config.go b/config/config.go index bb63453..e83c3e2 100644 --- a/config/config.go +++ b/config/config.go @@ -20,6 +20,7 @@ var ( EnableHTTPS bool ReadConfigFile string TrustedSubnet string + GRPCServer bool ) // ParseFlags parses the command line args and ENV variables @@ -31,6 +32,7 @@ func ParseFlags() { flag.BoolVar(&EnableHTTPS, "s", false, "enable https") flag.StringVar(&ReadConfigFile, "c", "", "read json config from file") flag.StringVar(&TrustedSubnet, "t", "", "CIDR") + flag.BoolVar(&GRPCServer, "g", false, "run gRPC server") flag.Parse() envRunAddr := os.Getenv("SERVER_ADDRESS") @@ -63,6 +65,11 @@ func ParseFlags() { TrustedSubnet = envTrustedSubnet } + envGRPCServer := os.Getenv("GRPC_SERVER") + if envGRPCServer != "" { + GRPCServer = true + } + envReadConfig := os.Getenv("CONFIG") if envReadConfig != "" { ReadConfigFile = envReadConfig diff --git a/config/config.json b/config/config.json index e98f669..7cf9f7a 100644 --- a/config/config.json +++ b/config/config.json @@ -4,5 +4,6 @@ "file_storage_path": "", "database_dsn": "postgres://postgres:ANTON_og228@localhost:5432/postgres", "enable_https": false, - "trusted_subnet": "79.139.178.194/32" + "trusted_subnet": "79.139.178.194/32", + "grpc_server": true } \ No newline at end of file diff --git a/config/json_config.go b/config/json_config.go index ff09340..265630c 100644 --- a/config/json_config.go +++ b/config/json_config.go @@ -14,6 +14,7 @@ type JSONConfig struct { DatabaseDSN string `json:"database_dsn"` // аналог переменной окружения DATABASE_DSN или флага -d EnableHTTPS bool `json:"enable_https"` // аналог переменной окружения ENABLE_HTTPS или флага -s TrustedSubnet string `json:"trusted_subnet"` // аналог переменной окружения TRUSTED_SUBNET или флага -t + GRPCServer bool `json:"grpc_server"` // аналог переменной окружения GRPC_SERVER или флага -g } // ReadJSONConfig read config from file @@ -47,6 +48,9 @@ func ReadJSONConfig(filename string) error { if config.TrustedSubnet != "" { TrustedSubnet = config.TrustedSubnet } + if config.GRPCServer { + GRPCServer = true + } return nil } diff --git a/go.mod b/go.mod index 66d6015..6a712fc 100644 --- a/go.mod +++ b/go.mod @@ -13,11 +13,13 @@ require ( github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 golang.org/x/net v0.27.0 + google.golang.org/protobuf v1.34.2 ) require ( golang.org/x/exp/typeparams v0.0.0-20221208152030-732eee02a75a // indirect golang.org/x/mod v0.19.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect ) require ( @@ -37,6 +39,7 @@ require ( golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect golang.org/x/tools v0.23.0 + google.golang.org/grpc v1.66.0 gopkg.in/yaml.v3 v3.0.1 // indirect honnef.co/go/tools v0.4.7 ) diff --git a/go.sum b/go.sum index cd96f4a..e7e4d33 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= 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= @@ -11,12 +13,12 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o 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.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4= -github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao= github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 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/pgerrcode v0.0.0-20240316143900-6e2875d9b438 h1:Dj0L5fhJ9F82ZJyVOmBx6msDp/kfd1t9GRfny/mfJA0= @@ -25,8 +27,6 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= -github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY= github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= @@ -54,32 +54,28 @@ 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.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= -golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/exp/typeparams v0.0.0-20221208152030-732eee02a75a h1:Jw5wfR+h9mnIYH+OtGT2im5wV1YGGDora5vTv/aa5bE= golang.org/x/exp/typeparams v0.0.0-20221208152030-732eee02a75a/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.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= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0= +google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c= +google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=