From d45087898baf3ec2d1948b51f806e793ad943ae2 Mon Sep 17 00:00:00 2001 From: Sergey Chubaryan Date: Tue, 4 Feb 2025 02:04:12 +0300 Subject: [PATCH] massive refactoring, moved shortlinks to dedicated service --- cmd/backend/app.go | 26 +-- cmd/backend/{config => }/config.go | 8 +- cmd/backend/config/new.go | 10 - cmd/backend/{ => config_defaults}/config.yaml | 0 .../{ => config_defaults}/jwt_signing_key | 0 cmd/backend/server/server.go | 4 - cmd/shortlinks/conf.yml | 3 + cmd/shortlinks/config.go | 31 +++ .../handlers.go} | 42 +++- cmd/shortlinks/main.go | 176 +++++++++++++++++ cmd/shortlinks/makefile | 2 + go.mod | 22 ++- go.sum | 42 ++++ internal/grpc_server/server.go | 50 +++++ .../grpc_server/shortlinks/shortlinks.pb.go | 179 ++++++++++++++++++ .../grpc_server/shortlinks/shortlinks.proto | 17 ++ .../shortlinks/shortlinks_grpc.pb.go | 121 ++++++++++++ internal/http_server/middleware/recovery.go | 155 +++++++++++++++ .../http_server/middleware/request_log.go | 60 ++++++ internal/http_server/middleware/tracing.go | 33 ++++ internal/http_server/server.go | 50 +++++ internal/integrations/opentelemetry.go | 29 +++ makefile | 8 + pkg/config/new.go | 13 ++ {cmd/backend => pkg}/config/parser.go | 5 +- .../config/parser_test.go | 0 26 files changed, 1042 insertions(+), 44 deletions(-) rename cmd/backend/{config => }/config.go (83%) delete mode 100644 cmd/backend/config/new.go rename cmd/backend/{ => config_defaults}/config.yaml (100%) rename cmd/backend/{ => config_defaults}/jwt_signing_key (100%) create mode 100644 cmd/shortlinks/conf.yml create mode 100644 cmd/shortlinks/config.go rename cmd/{backend/server/handlers/shortlink_handlers.go => shortlinks/handlers.go} (64%) create mode 100644 cmd/shortlinks/main.go create mode 100644 cmd/shortlinks/makefile create mode 100644 internal/grpc_server/server.go create mode 100644 internal/grpc_server/shortlinks/shortlinks.pb.go create mode 100644 internal/grpc_server/shortlinks/shortlinks.proto create mode 100644 internal/grpc_server/shortlinks/shortlinks_grpc.pb.go create mode 100644 internal/http_server/middleware/recovery.go create mode 100644 internal/http_server/middleware/request_log.go create mode 100644 internal/http_server/middleware/tracing.go create mode 100644 internal/http_server/server.go create mode 100644 internal/integrations/opentelemetry.go create mode 100644 pkg/config/new.go rename {cmd/backend => pkg}/config/parser.go (91%) rename cmd/backend/config/config_test.go => pkg/config/parser_test.go (100%) diff --git a/cmd/backend/app.go b/cmd/backend/app.go index 11b0fb0..aa520d2 100644 --- a/cmd/backend/app.go +++ b/cmd/backend/app.go @@ -2,7 +2,6 @@ package main import ( "backend/cmd/backend/args_parser" - "backend/cmd/backend/config" "backend/cmd/backend/server" "backend/internal/core/models" "backend/internal/core/repos" @@ -21,10 +20,6 @@ import ( "runtime/pprof" "syscall" "time" - - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" - traceSdk "go.opentelemetry.io/otel/sdk/trace" - "go.opentelemetry.io/otel/trace" ) type App struct{} @@ -76,7 +71,7 @@ func (a *App) Run(p RunParams) { log.Fatalf("failed to create logger object: %v\n", err) } - conf, err := config.NewFromFile(args.GetConfigPath()) + conf, err := LoadConfig(args.GetConfigPath()) if err != nil { logger.Fatal().Err(err).Msg("failed to parse config file") } @@ -107,22 +102,9 @@ func (a *App) Run(p RunParams) { } } - var tracer trace.Tracer - { - tracerExporter, err := otlptracehttp.New(context.Background(), otlptracehttp.WithEndpointURL("http://localhost:4318")) - if err != nil { - logger.Fatal().Err(err).Msg("failed initializing tracer") - } - - tracerProvider := traceSdk.NewTracerProvider( - traceSdk.WithSampler(traceSdk.TraceIDRatioBased(0.1)), - traceSdk.WithBatcher( - tracerExporter, - traceSdk.WithMaxQueueSize(8192), - traceSdk.WithMaxExportBatchSize(2048), - ), - ) - tracer = tracerProvider.Tracer("backend") + tracer, err := integrations.NewTracer("backend") + if err != nil { + logger.Fatal().Err(err).Msg("failed initializing tracer") } // Build business-logic objects diff --git a/cmd/backend/config/config.go b/cmd/backend/config.go similarity index 83% rename from cmd/backend/config/config.go rename to cmd/backend/config.go index 19d69f9..893c0db 100644 --- a/cmd/backend/config/config.go +++ b/cmd/backend/config.go @@ -1,4 +1,6 @@ -package config +package main + +import "backend/pkg/config" type IConfig interface { GetPort() uint16 @@ -8,6 +10,10 @@ type IConfig interface { GetKafkaTopic() string } +func LoadConfig(filePath string) (IConfig, error) { + return config.NewFromFile[*Config](filePath) +} + type Config struct { Port uint16 `yaml:"port"` PostgresUrl string `yaml:"postgres_url"` diff --git a/cmd/backend/config/new.go b/cmd/backend/config/new.go deleted file mode 100644 index 6a52c39..0000000 --- a/cmd/backend/config/new.go +++ /dev/null @@ -1,10 +0,0 @@ -package config - -func NewFromFile(path string) (IConfig, error) { - p := NewParser[Config]() - config, err := p.ParseFile(path) - if err != nil { - return nil, err - } - return &config, nil -} diff --git a/cmd/backend/config.yaml b/cmd/backend/config_defaults/config.yaml similarity index 100% rename from cmd/backend/config.yaml rename to cmd/backend/config_defaults/config.yaml diff --git a/cmd/backend/jwt_signing_key b/cmd/backend/config_defaults/jwt_signing_key similarity index 100% rename from cmd/backend/jwt_signing_key rename to cmd/backend/config_defaults/jwt_signing_key diff --git a/cmd/backend/server/server.go b/cmd/backend/server/server.go index 8ea6050..e968048 100644 --- a/cmd/backend/server/server.go +++ b/cmd/backend/server/server.go @@ -46,10 +46,6 @@ func New(opts NewServerOpts) *Server { r.Use(middleware.NewRequestLogMiddleware(opts.Logger, opts.Tracer, prometheus)) r.Use(middleware.NewTracingMiddleware(opts.Tracer)) - linkGroup := r.Group("/s") - linkGroup.POST("/new", handlers.NewShortlinkCreateHandler(opts.Logger, opts.ShortlinkService)) - linkGroup.GET("/:linkId", handlers.NewShortlinkResolveHandler(opts.Logger, opts.ShortlinkService)) - userGroup := r.Group("/user") userGroup.POST("/create", handlers.NewUserCreateHandler(opts.Logger, opts.UserService)) userGroup.POST("/login", handlers.NewUserLoginHandler(opts.Logger, opts.UserService)) diff --git a/cmd/shortlinks/conf.yml b/cmd/shortlinks/conf.yml new file mode 100644 index 0000000..fb50a15 --- /dev/null +++ b/cmd/shortlinks/conf.yml @@ -0,0 +1,3 @@ +http_port: 8081 +grpc_port: 8082 +postgres_url: "postgres://postgres:postgres@localhost:5432/postgres" \ No newline at end of file diff --git a/cmd/shortlinks/config.go b/cmd/shortlinks/config.go new file mode 100644 index 0000000..a2c6036 --- /dev/null +++ b/cmd/shortlinks/config.go @@ -0,0 +1,31 @@ +package main + +import "backend/pkg/config" + +type IConfig interface { + GetHttpPort() uint16 + GetGrpcPort() uint16 + GetPostgresUrl() string +} + +func LoadConfig(filePath string) (IConfig, error) { + return config.NewFromFile[*Config](filePath) +} + +type Config struct { + HttpPort uint16 `yaml:"http_port" validate:"required"` + GrpcPort uint16 `yaml:"grpc_port" validate:"required"` + PostgresUrl string `yaml:"postgres_url" validate:"required"` +} + +func (c *Config) GetHttpPort() uint16 { + return c.HttpPort +} + +func (c *Config) GetGrpcPort() uint16 { + return c.GrpcPort +} + +func (c *Config) GetPostgresUrl() string { + return c.PostgresUrl +} diff --git a/cmd/backend/server/handlers/shortlink_handlers.go b/cmd/shortlinks/handlers.go similarity index 64% rename from cmd/backend/server/handlers/shortlink_handlers.go rename to cmd/shortlinks/handlers.go index 811bb21..3fdc089 100644 --- a/cmd/backend/server/handlers/shortlink_handlers.go +++ b/cmd/shortlinks/handlers.go @@ -1,8 +1,10 @@ -package handlers +package main import ( "backend/internal/core/services" + "backend/internal/grpc_server/shortlinks" "backend/pkg/logger" + "context" "encoding/json" "fmt" "net/url" @@ -14,7 +16,41 @@ type shortlinkCreateOutput struct { Link string `json:"link"` } -func NewShortlinkCreateHandler(logger logger.Logger, shortlinkService services.ShortlinkService) gin.HandlerFunc { +type ShortlinksGrpc struct { + shortlinks.UnimplementedShortlinksServer + log logger.Logger + host string + shortlinkService services.ShortlinkService +} + +func (s *ShortlinksGrpc) Create(ctx context.Context, req *shortlinks.CreateRequest) (*shortlinks.CreateResponse, error) { + ctxLogger := s.log.WithContext(ctx) + + rawUrl := req.GetUrl() + if rawUrl == "" { + ctxLogger.Error().Msg("url query param missing") + return nil, fmt.Errorf("url query param missing") + } + + u, err := url.Parse(rawUrl) + if err != nil { + ctxLogger.Error().Err(err).Msg("error parsing url param") + return nil, err + } + u.Scheme = "https" + + linkId, err := s.shortlinkService.CreateShortlink(ctx, u.String()) + if err != nil { + ctxLogger.Error().Err(err).Msg("err creating shortlink") + return nil, err + } + + return &shortlinks.CreateResponse{ + Link: fmt.Sprintf("%s/s/%s", s.host, linkId), + }, nil +} + +func NewShortlinkCreateHandler(logger logger.Logger, shortlinkService services.ShortlinkService, host string) gin.HandlerFunc { return func(ctx *gin.Context) { ctxLogger := logger.WithContext(ctx) @@ -41,7 +77,7 @@ func NewShortlinkCreateHandler(logger logger.Logger, shortlinkService services.S } resultBody, err := json.Marshal(shortlinkCreateOutput{ - Link: "https://nucrea.ru/s/" + linkId, + Link: fmt.Sprintf("%s/s/%s", host, linkId), }) if err != nil { ctxLogger.Error().Err(err).Msg("err marshalling shortlink") diff --git a/cmd/shortlinks/main.go b/cmd/shortlinks/main.go new file mode 100644 index 0000000..d6ef4ed --- /dev/null +++ b/cmd/shortlinks/main.go @@ -0,0 +1,176 @@ +package main + +import ( + "backend/internal/core/repos" + "backend/internal/core/services" + grpcserver "backend/internal/grpc_server" + "backend/internal/grpc_server/shortlinks" + httpserver "backend/internal/http_server" + "backend/internal/http_server/middleware" + "backend/internal/integrations" + "backend/pkg/cache" + "backend/pkg/logger" + "context" + "fmt" + "sync" + + "github.com/gin-gonic/gin" + "github.com/spf13/cobra" + "go.opentelemetry.io/otel/trace" + "google.golang.org/grpc" +) + +var rootCmd = &cobra.Command{ + Use: "shortlinks", + Short: "shortlinks is a microservice for creating ang managing shortlinks", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func main() { + ctx := context.Background() + + var ( + configPath = "" + logPath = "" + ) + { + rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "path to configuration file") + rootCmd.MarkPersistentFlagRequired("config") + + rootCmd.PersistentFlags().StringVarP(&logPath, "logfile", "l", "", "path to log file") + rootCmd.MarkPersistentFlagRequired("logfile") + + if err := rootCmd.Execute(); err != nil { + panic(err) + } + } + + log, err := logger.New(ctx, logger.NewLoggerOpts{ + Debug: true, + OutputFile: logPath, + }) + if err != nil { + panic(err) + } + + conf, err := LoadConfig(configPath) + if err != nil { + log.Error().Err(err).Msg("failed loading config") + } + + pgDb, err := integrations.NewPostgresConn(ctx, conf.GetPostgresUrl()) + if err != nil { + log.Error().Err(err).Msg("failed connecting to postgres") + } + + tracer, err := integrations.NewTracer("backend") + if err != nil { + log.Fatal().Err(err).Msg("failed initializing tracer") + } + + repo := repos.NewShortlinkRepo(pgDb, tracer) + service := services.NewShortlinkSevice( + services.NewShortlinkServiceParams{ + Cache: cache.NewCacheInmem[string, string](), + Repo: repo, + }, + ) + + RunServer(ctx, log, tracer, conf, service) +} + +func RunServer(ctx context.Context, log logger.Logger, tracer trace.Tracer, conf IConfig, shortlinkService services.ShortlinkService) { + host := fmt.Sprintf("http://localhost:%d", conf.GetHttpPort()) + debugMode := true + if !debugMode { + gin.SetMode(gin.ReleaseMode) + } + + prometheus := integrations.NewPrometheus() + + r := gin.New() + r.Any("/metrics", gin.WrapH(prometheus.GetRequestHandler())) + r.GET("/health", func(ctx *gin.Context) { + ctx.Status(200) + }) + + r.Use(middleware.NewRecoveryMiddleware(log, prometheus, debugMode)) + r.Use(middleware.NewRequestLogMiddleware(log, tracer, prometheus)) + r.Use(middleware.NewTracingMiddleware(tracer)) + + linkGroup := r.Group("/s") + linkGroup.POST("/new", NewShortlinkCreateHandler(log, shortlinkService, host)) + linkGroup.GET("/:linkId", NewShortlinkResolveHandler(log, shortlinkService)) + + grpcObj := &ShortlinksGrpc{ + log: log, + host: host, + shortlinkService: shortlinkService, + } + + grpcUnderlying := grpc.NewServer() + shortlinks.RegisterShortlinksServer(grpcUnderlying, grpcObj) + + httpServer := httpserver.New( + httpserver.NewServerOpts{ + Logger: log, + HttpServer: r, + }, + ) + grpcServer := grpcserver.New( + grpcserver.NewServerOpts{ + Logger: log, + GrpcServer: grpcUnderlying, + }, + ) + + wg := &sync.WaitGroup{} + + wg.Add(1) + go func() { + defer wg.Done() + httpServer.Run(ctx, conf.GetHttpPort()) + }() + + wg.Add(1) + go func() { + defer wg.Done() + grpcServer.Run(ctx, conf.GetGrpcPort()) + }() + + wg.Wait() +} + +// func RunTestGrpcClient() { +// go func() { +// conn, err := grpc.NewClient( +// fmt.Sprintf(":%d", conf.GetPort()), +// grpc.WithTransportCredentials(insecure.NewCredentials()), +// ) +// if err != nil { +// log.Fatal().Err(err).Msg("failed initializing grpc test client") +// } +// defer conn.Close() + +// c := shortlinks.NewShortlinksClient(conn) + +// for { +// select { +// case <-ctx.Done(): +// return +// default: +// } + +// res, err := c.Create(ctx, &shortlinks.CreateRequest{ +// Url: "https://google.com", +// }) +// if err != nil { +// log.Error().Err(err).Msg("failed creating shortlink") +// } else { +// log.Log().Msgf("Successfully created link: %s", res.GetLink()) +// } + +// time.Sleep(3 * time.Second) +// } +// }() +// } diff --git a/cmd/shortlinks/makefile b/cmd/shortlinks/makefile new file mode 100644 index 0000000..7145e0e --- /dev/null +++ b/cmd/shortlinks/makefile @@ -0,0 +1,2 @@ +run: + go run . -c ./conf.yml -l ./../../.run/shortlinks.log \ No newline at end of file diff --git a/go.mod b/go.mod index 66f6a5a..bcec722 100644 --- a/go.mod +++ b/go.mod @@ -23,20 +23,37 @@ require ( require ( github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pierrec/lz4/v4 v4.1.15 // 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/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.19.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect go.opentelemetry.io/otel/metric v1.29.0 // indirect go.opentelemetry.io/proto/otlp v1.3.1 // 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/sync v0.8.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect google.golang.org/grpc v1.65.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect ) require ( @@ -46,7 +63,7 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -63,10 +80,11 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect + github.com/spf13/cobra v1.8.1 github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.9.0 // indirect diff --git a/go.sum b/go.sum index fcd8ce2..49a2138 100644 --- a/go.sum +++ b/go.sum @@ -16,9 +16,14 @@ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.4/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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -50,6 +55,10 @@ 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/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= +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/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-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -75,12 +84,16 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +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.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 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= @@ -95,6 +108,8 @@ github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFu github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg= github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= @@ -108,8 +123,25 @@ github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99 github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +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/segmentio/kafka-go v0.4.47 h1:IqziR4pA3vrZq7YdRxaT3w1/5fvIH5qpCwstUanQQB0= github.com/segmentio/kafka-go v0.4.47/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg= +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.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +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.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= 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= @@ -122,6 +154,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 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.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= @@ -147,6 +181,10 @@ go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt3 go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= +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/arch v0.9.0 h1:ub9TgUInamJ8mrZIGlBG6/4TqWeMszd4N8lNorbrr6k= golang.org/x/arch v0.9.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -154,6 +192,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -216,6 +256,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= +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= diff --git a/internal/grpc_server/server.go b/internal/grpc_server/server.go new file mode 100644 index 0000000..76a3535 --- /dev/null +++ b/internal/grpc_server/server.go @@ -0,0 +1,50 @@ +package grpcserver + +import ( + "backend/pkg/logger" + "context" + "fmt" + "net" +) + +type serverGrpc interface { + Serve(lis net.Listener) error +} + +type Server struct { + logger logger.Logger + grpc serverGrpc +} + +type NewServerOpts struct { + Logger logger.Logger + GrpcServer serverGrpc +} + +func New(opts NewServerOpts) *Server { + return &Server{ + logger: opts.Logger, + grpc: opts.GrpcServer, + } +} + +func (s *Server) Run(ctx context.Context, port uint16) { + listenAddr := fmt.Sprintf("0.0.0.0:%d", port) + s.logger.Log().Msgf("server listening on %s", listenAddr) + + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listenAddr) + if err != nil { + s.logger.Fatal().Err(err).Msg("can not create network listener") + } + + go func() { + <-ctx.Done() + s.logger.Log().Msg("stopping tcp listener...") + listener.Close() + }() + + err = s.grpc.Serve(listener) + if err != nil && err == net.ErrClosed { + s.logger.Fatal().Err(err).Msg("server stopped with error") + } +} diff --git a/internal/grpc_server/shortlinks/shortlinks.pb.go b/internal/grpc_server/shortlinks/shortlinks.pb.go new file mode 100644 index 0000000..ccda4c5 --- /dev/null +++ b/internal/grpc_server/shortlinks/shortlinks.pb.go @@ -0,0 +1,179 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.4 +// protoc v5.29.0 +// source: shortlinks.proto + +package shortlinks + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type CreateRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Url string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *CreateRequest) Reset() { + *x = CreateRequest{} + mi := &file_shortlinks_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *CreateRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateRequest) ProtoMessage() {} + +func (x *CreateRequest) ProtoReflect() protoreflect.Message { + mi := &file_shortlinks_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateRequest.ProtoReflect.Descriptor instead. +func (*CreateRequest) Descriptor() ([]byte, []int) { + return file_shortlinks_proto_rawDescGZIP(), []int{0} +} + +func (x *CreateRequest) GetUrl() string { + if x != nil { + return x.Url + } + return "" +} + +type CreateResponse struct { + state protoimpl.MessageState `protogen:"open.v1"` + Link string `protobuf:"bytes,1,opt,name=link,proto3" json:"link,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *CreateResponse) Reset() { + *x = CreateResponse{} + mi := &file_shortlinks_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *CreateResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateResponse) ProtoMessage() {} + +func (x *CreateResponse) ProtoReflect() protoreflect.Message { + mi := &file_shortlinks_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CreateResponse.ProtoReflect.Descriptor instead. +func (*CreateResponse) Descriptor() ([]byte, []int) { + return file_shortlinks_proto_rawDescGZIP(), []int{1} +} + +func (x *CreateResponse) GetLink() string { + if x != nil { + return x.Link + } + return "" +} + +var File_shortlinks_proto protoreflect.FileDescriptor + +var file_shortlinks_proto_rawDesc = string([]byte{ + 0x0a, 0x10, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x12, 0x0a, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x22, 0x21, + 0x0a, 0x0d, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, + 0x6c, 0x22, 0x24, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x69, 0x6e, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x04, 0x6c, 0x69, 0x6e, 0x6b, 0x32, 0x4f, 0x0a, 0x0a, 0x53, 0x68, 0x6f, 0x72, 0x74, + 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x12, 0x41, 0x0a, 0x06, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x12, + 0x19, 0x2e, 0x73, 0x68, 0x6f, 0x72, 0x74, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x2e, 0x43, 0x72, 0x65, + 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x73, 0x68, 0x6f, + 0x72, 0x74, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x73, 0x68, + 0x6f, 0x72, 0x74, 0x6c, 0x69, 0x6e, 0x6b, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +}) + +var ( + file_shortlinks_proto_rawDescOnce sync.Once + file_shortlinks_proto_rawDescData []byte +) + +func file_shortlinks_proto_rawDescGZIP() []byte { + file_shortlinks_proto_rawDescOnce.Do(func() { + file_shortlinks_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_shortlinks_proto_rawDesc), len(file_shortlinks_proto_rawDesc))) + }) + return file_shortlinks_proto_rawDescData +} + +var file_shortlinks_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_shortlinks_proto_goTypes = []any{ + (*CreateRequest)(nil), // 0: shortlinks.CreateRequest + (*CreateResponse)(nil), // 1: shortlinks.CreateResponse +} +var file_shortlinks_proto_depIdxs = []int32{ + 0, // 0: shortlinks.Shortlinks.Create:input_type -> shortlinks.CreateRequest + 1, // 1: shortlinks.Shortlinks.Create:output_type -> shortlinks.CreateResponse + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_shortlinks_proto_init() } +func file_shortlinks_proto_init() { + if File_shortlinks_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_shortlinks_proto_rawDesc), len(file_shortlinks_proto_rawDesc)), + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_shortlinks_proto_goTypes, + DependencyIndexes: file_shortlinks_proto_depIdxs, + MessageInfos: file_shortlinks_proto_msgTypes, + }.Build() + File_shortlinks_proto = out.File + file_shortlinks_proto_goTypes = nil + file_shortlinks_proto_depIdxs = nil +} diff --git a/internal/grpc_server/shortlinks/shortlinks.proto b/internal/grpc_server/shortlinks/shortlinks.proto new file mode 100644 index 0000000..c7111dc --- /dev/null +++ b/internal/grpc_server/shortlinks/shortlinks.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +option go_package = "./shortlinks"; + +package shortlinks; + +service Shortlinks { + rpc Create (CreateRequest) returns (CreateResponse) {} +} + +message CreateRequest { + string url = 1; +} + +message CreateResponse { + string link = 1; +} \ No newline at end of file diff --git a/internal/grpc_server/shortlinks/shortlinks_grpc.pb.go b/internal/grpc_server/shortlinks/shortlinks_grpc.pb.go new file mode 100644 index 0000000..1cc1ca6 --- /dev/null +++ b/internal/grpc_server/shortlinks/shortlinks_grpc.pb.go @@ -0,0 +1,121 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v5.29.0 +// source: shortlinks.proto + +package shortlinks + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + Shortlinks_Create_FullMethodName = "/shortlinks.Shortlinks/Create" +) + +// ShortlinksClient is the client API for Shortlinks service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type ShortlinksClient interface { + Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) +} + +type shortlinksClient struct { + cc grpc.ClientConnInterface +} + +func NewShortlinksClient(cc grpc.ClientConnInterface) ShortlinksClient { + return &shortlinksClient{cc} +} + +func (c *shortlinksClient) Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(CreateResponse) + err := c.cc.Invoke(ctx, Shortlinks_Create_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ShortlinksServer is the server API for Shortlinks service. +// All implementations must embed UnimplementedShortlinksServer +// for forward compatibility. +type ShortlinksServer interface { + Create(context.Context, *CreateRequest) (*CreateResponse, error) + mustEmbedUnimplementedShortlinksServer() +} + +// UnimplementedShortlinksServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedShortlinksServer struct{} + +func (UnimplementedShortlinksServer) Create(context.Context, *CreateRequest) (*CreateResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Create not implemented") +} +func (UnimplementedShortlinksServer) mustEmbedUnimplementedShortlinksServer() {} +func (UnimplementedShortlinksServer) testEmbeddedByValue() {} + +// UnsafeShortlinksServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to ShortlinksServer will +// result in compilation errors. +type UnsafeShortlinksServer interface { + mustEmbedUnimplementedShortlinksServer() +} + +func RegisterShortlinksServer(s grpc.ServiceRegistrar, srv ShortlinksServer) { + // If the following call pancis, it indicates UnimplementedShortlinksServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&Shortlinks_ServiceDesc, srv) +} + +func _Shortlinks_Create_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ShortlinksServer).Create(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Shortlinks_Create_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ShortlinksServer).Create(ctx, req.(*CreateRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Shortlinks_ServiceDesc is the grpc.ServiceDesc for Shortlinks service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Shortlinks_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "shortlinks.Shortlinks", + HandlerType: (*ShortlinksServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Create", + Handler: _Shortlinks_Create_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "shortlinks.proto", +} diff --git a/internal/http_server/middleware/recovery.go b/internal/http_server/middleware/recovery.go new file mode 100644 index 0000000..472e126 --- /dev/null +++ b/internal/http_server/middleware/recovery.go @@ -0,0 +1,155 @@ +package middleware + +// Modified recovery from gin, use own logger + +import ( + "backend/internal/integrations" + "backend/pkg/logger" + "bytes" + "errors" + "fmt" + "net" + "net/http" + "net/http/httputil" + "os" + "runtime" + "strings" + "time" + + "github.com/gin-gonic/gin" +) + +const ( + reset = "\033[0m" +) + +var ( + dunno = []byte("???") + centerDot = []byte("·") + dot = []byte(".") + slash = []byte("/") +) + +func NewRecoveryMiddleware(logger logger.Logger, prometheus *integrations.Prometheus, debugMode bool) gin.HandlerFunc { + handle := defaultHandleRecovery + return func(c *gin.Context) { + defer func() { + if err := recover(); err != nil { + prometheus.AddPanic() + + // Check for a broken connection, as it is not really a + // condition that warrants a panic stack trace. + var brokenPipe bool + if ne, ok := err.(*net.OpError); ok { + var se *os.SyscallError + if errors.As(ne, &se) { + seStr := strings.ToLower(se.Error()) + if strings.Contains(seStr, "broken pipe") || + strings.Contains(seStr, "connection reset by peer") { + brokenPipe = true + } + } + } + if logger != nil { + stack := stack(3) + httpRequest, _ := httputil.DumpRequest(c.Request, false) + headers := strings.Split(string(httpRequest), "\r\n") + for idx, header := range headers { + current := strings.Split(header, ":") + if current[0] == "Authorization" { + headers[idx] = current[0] + ": *" + } + } + headersToStr := strings.Join(headers, "\r\n") + if brokenPipe { + logger.Printf("%s\n%s%s", err, headersToStr, reset) + } else if debugMode { + logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s", + timeFormat(time.Now()), headersToStr, err, stack, reset) + } else { + logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s", + timeFormat(time.Now()), err, stack, reset) + } + } + if brokenPipe { + // If the connection is dead, we can't write a status to it. + c.Error(err.(error)) //nolint: errcheck + c.Abort() + } else { + handle(c, err) + } + } + }() + c.Next() + } +} + +func defaultHandleRecovery(c *gin.Context, _ any) { + c.AbortWithStatus(http.StatusInternalServerError) +} + +// stack returns a nicely formatted stack frame, skipping skip frames. +func stack(skip int) []byte { + buf := new(bytes.Buffer) // the returned data + // As we loop, we open files and read them. These variables record the currently + // loaded file. + var lines [][]byte + var lastFile string + for i := skip; ; i++ { // Skip the expected number of frames + pc, file, line, ok := runtime.Caller(i) + if !ok { + break + } + // Print this much at least. If we can't find the source, it won't show. + fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) + if file != lastFile { + data, err := os.ReadFile(file) + if err != nil { + continue + } + lines = bytes.Split(data, []byte{'\n'}) + lastFile = file + } + fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line)) + } + return buf.Bytes() +} + +// source returns a space-trimmed slice of the n'th line. +func source(lines [][]byte, n int) []byte { + n-- // in stack trace, lines are 1-indexed but our array is 0-indexed + if n < 0 || n >= len(lines) { + return dunno + } + return bytes.TrimSpace(lines[n]) +} + +// function returns, if possible, the name of the function containing the PC. +func function(pc uintptr) []byte { + fn := runtime.FuncForPC(pc) + if fn == nil { + return dunno + } + name := []byte(fn.Name()) + // The name includes the path name to the package, which is unnecessary + // since the file name is already included. Plus, it has center dots. + // That is, we see + // runtime/debug.*T·ptrmethod + // and want + // *T.ptrmethod + // Also the package path might contain dot (e.g. code.google.com/...), + // so first eliminate the path prefix + if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 { + name = name[lastSlash+1:] + } + if period := bytes.Index(name, dot); period >= 0 { + name = name[period+1:] + } + name = bytes.ReplaceAll(name, centerDot, dot) + return name +} + +// timeFormat returns a customized time string for logger. +func timeFormat(t time.Time) string { + return t.Format("2006/01/02 - 15:04:05") +} diff --git a/internal/http_server/middleware/request_log.go b/internal/http_server/middleware/request_log.go new file mode 100644 index 0000000..814d86e --- /dev/null +++ b/internal/http_server/middleware/request_log.go @@ -0,0 +1,60 @@ +package middleware + +import ( + "backend/internal/integrations" + log "backend/pkg/logger" + "fmt" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "go.opentelemetry.io/otel/trace" +) + +func NewRequestLogMiddleware(logger log.Logger, tracer trace.Tracer, prometheus *integrations.Prometheus) gin.HandlerFunc { + return func(c *gin.Context) { + prometheus.RequestInc() + defer prometheus.RequestDec() + + requestId := c.GetHeader("X-Request-Id") + if requestId == "" { + requestId = uuid.New().String() + } + c.Header("X-Request-Id", requestId) + c.Header("Access-Control-Allow-Origin", "*") + + log.SetCtxRequestId(c, requestId) + + path := c.Request.URL.Path + if c.Request.URL.RawQuery != "" { + path = path + "?" + c.Request.URL.RawQuery + } + + start := time.Now() + c.Next() + latency := time.Since(start) + + prometheus.AddRequestTime(float64(latency.Microseconds())) + + method := c.Request.Method + statusCode := c.Writer.Status() + + ctxLogger := logger.WithContext(c) + + msg := fmt.Sprintf("Request %s %s %d %v", method, path, statusCode, latency) + + if statusCode >= 200 && statusCode < 400 { + // ctxLogger.Log().Msg(msg) + return + } + + if statusCode >= 400 && statusCode < 500 { + prometheus.Add4xxError() + ctxLogger.Warning().Msg(msg) + return + } + + prometheus.Add5xxError() + ctxLogger.Error().Msg(msg) + } +} diff --git a/internal/http_server/middleware/tracing.go b/internal/http_server/middleware/tracing.go new file mode 100644 index 0000000..a739c3c --- /dev/null +++ b/internal/http_server/middleware/tracing.go @@ -0,0 +1,33 @@ +package middleware + +import ( + "fmt" + + "github.com/gin-gonic/gin" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/trace" +) + +func NewTracingMiddleware(tracer trace.Tracer) gin.HandlerFunc { + prop := otel.GetTextMapPropagator() + + return func(c *gin.Context) { + savedCtx := c.Request.Context() + defer func() { + c.Request = c.Request.WithContext(savedCtx) + }() + + ctx := prop.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header)) + + ctx, span := tracer.Start(ctx, fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path)) + defer span.End() + + traceId := span.SpanContext().TraceID() + c.Header("X-Trace-Id", traceId.String()) + + c.Request = c.Request.WithContext(ctx) + + c.Next() + } +} diff --git a/internal/http_server/server.go b/internal/http_server/server.go new file mode 100644 index 0000000..b824543 --- /dev/null +++ b/internal/http_server/server.go @@ -0,0 +1,50 @@ +package httpserver + +import ( + "backend/pkg/logger" + "context" + "fmt" + "net" +) + +type serverHttp interface { + RunListener(l net.Listener) error +} + +type Server struct { + logger logger.Logger + http serverHttp +} + +type NewServerOpts struct { + Logger logger.Logger + HttpServer serverHttp +} + +func New(opts NewServerOpts) *Server { + return &Server{ + logger: opts.Logger, + http: opts.HttpServer, + } +} + +func (s *Server) Run(ctx context.Context, port uint16) { + listenAddr := fmt.Sprintf("0.0.0.0:%d", port) + s.logger.Log().Msgf("server listening on %s", listenAddr) + + listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listenAddr) + if err != nil { + s.logger.Fatal().Err(err).Msg("can not create network listener") + } + + go func() { + <-ctx.Done() + s.logger.Log().Msg("stopping tcp listener...") + listener.Close() + }() + + err = s.http.RunListener(listener) + if err != nil && err == net.ErrClosed { + s.logger.Fatal().Err(err).Msg("server stopped with error") + } +} diff --git a/internal/integrations/opentelemetry.go b/internal/integrations/opentelemetry.go new file mode 100644 index 0000000..36cb6be --- /dev/null +++ b/internal/integrations/opentelemetry.go @@ -0,0 +1,29 @@ +package integrations + +import ( + "context" + + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" + "go.opentelemetry.io/otel/trace" + + traceSdk "go.opentelemetry.io/otel/sdk/trace" +) + +func NewTracer(serviceName string) (trace.Tracer, error) { + tracerExporter, err := otlptracehttp.New(context.Background(), otlptracehttp.WithEndpointURL("http://localhost:4318")) + if err != nil { + return nil, err + // logger.Fatal().Err(err).Msg("failed initializing tracer") + } + + tracerProvider := traceSdk.NewTracerProvider( + traceSdk.WithSampler(traceSdk.TraceIDRatioBased(0.1)), + traceSdk.WithBatcher( + tracerExporter, + traceSdk.WithMaxQueueSize(8192), + traceSdk.WithMaxExportBatchSize(2048), + ), + ) + + return tracerProvider.Tracer("backend"), nil +} diff --git a/makefile b/makefile index 212b08a..88d70ce 100644 --- a/makefile +++ b/makefile @@ -6,6 +6,14 @@ release: install: go install +grpc: + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + + # protoc --go_out=. --go_opt=paths=source_relative \ + # --go-grpc_out=. --go-grpc_opt=paths=source_relative \ + # helloworld/helloworld.proto + run: install release mkdir -p ./.run ./.build/release/backend -c ./misc/config.yaml -o ./.run/log.txt -p ./.run/cpu.pprof \ No newline at end of file diff --git a/pkg/config/new.go b/pkg/config/new.go new file mode 100644 index 0000000..e046bd5 --- /dev/null +++ b/pkg/config/new.go @@ -0,0 +1,13 @@ +package config + +func NewFromFile[T interface{}](filePath string) (T, error) { + p := NewParser[T]() + + config, err := p.ParseFile(filePath) + if err != nil { + var t T + return t, err + } + + return config, nil +} diff --git a/cmd/backend/config/parser.go b/pkg/config/parser.go similarity index 91% rename from cmd/backend/config/parser.go rename to pkg/config/parser.go index d0e6cb1..6b050e1 100644 --- a/cmd/backend/config/parser.go +++ b/pkg/config/parser.go @@ -23,13 +23,13 @@ type parser[T interface{}] struct { } func (p *parser[T]) ParseFile(path string) (T, error) { - fBytes, err := os.ReadFile(path) + fileBytes, err := os.ReadFile(path) if err != nil { var t T return t, err } - return p.parse(fBytes) + return p.parse(fileBytes) } func (p *parser[T]) parse(b []byte) (T, error) { @@ -38,6 +38,7 @@ func (p *parser[T]) parse(b []byte) (T, error) { if err := yaml.Unmarshal(b, &t); err != nil { return t, err } + if err := p.validate.Struct(t); err != nil { return t, err } diff --git a/cmd/backend/config/config_test.go b/pkg/config/parser_test.go similarity index 100% rename from cmd/backend/config/config_test.go rename to pkg/config/parser_test.go