add service_url field to shortlinks config

This commit is contained in:
Sergey Chubaryan 2025-02-23 20:42:45 +03:00
parent 2e70087f63
commit d96b5606ff
5 changed files with 19 additions and 10 deletions

View File

@ -1,3 +1,4 @@
service_url: http://localhost:8081
http_port: 8081
grpc_port: 8082
postgres_url: "postgres://postgres:postgres@localhost:5432/postgres"

View File

@ -3,6 +3,7 @@ package main
import "backend/pkg/config"
type IConfig interface {
GetServiceUrl() string
GetHttpPort() uint16
GetGrpcPort() uint16
GetPostgresUrl() string
@ -13,11 +14,16 @@ func LoadConfig(filePath string) (IConfig, error) {
}
type Config struct {
ServiceUrl string `yaml:"service_url" validate:"required"`
HttpPort uint16 `yaml:"http_port" validate:"required"`
GrpcPort uint16 `yaml:"grpc_port" validate:"required"`
PostgresUrl string `yaml:"postgres_url" validate:"required"`
}
func (c *Config) GetServiceUrl() string {
return c.ServiceUrl
}
func (c *Config) GetHttpPort() uint16 {
return c.HttpPort
}

View File

@ -10,7 +10,7 @@ import (
func NewShortlinksGrpc(log logger.Logger, shortlinkService services.ShortlinkService, host string) *ShortlinksGrpc {
return &ShortlinksGrpc{
handler: NewCreateHandler(log, shortlinkService, host),
handler: NewShortlinkCreateHandler(log, shortlinkService, host),
}
}

View File

@ -19,10 +19,10 @@ type shortlinkCreateOutput struct {
Link string `json:"link"`
}
func NewCreateHandler(
func NewShortlinkCreateHandler(
log logger.Logger,
shortlinkService services.ShortlinkService,
host string,
serviceUrl string,
) httpserver.Handler[shortlinkCreateInput, shortlinkCreateOutput] {
return func(ctx context.Context, input shortlinkCreateInput) (shortlinkCreateOutput, error) {
output := shortlinkCreateOutput{}
@ -39,13 +39,17 @@ func NewCreateHandler(
}
return shortlinkCreateOutput{
Link: fmt.Sprintf("%s/s/%s", host, linkId),
Link: fmt.Sprintf("%s/s/%s", serviceUrl, linkId),
}, nil
}
}
func NewShortlinkCreateHandler(log logger.Logger, shortlinkService services.ShortlinkService, host string) gin.HandlerFunc {
return httpserver.WrapGin(log, NewCreateHandler(log, shortlinkService, host))
func NewShortlinkCreateGinHandler(
log logger.Logger,
shortlinkService services.ShortlinkService,
serviceUrl string,
) gin.HandlerFunc {
return httpserver.WrapGin(log, NewShortlinkCreateHandler(log, shortlinkService, serviceUrl))
}
func NewShortlinkResolveHandler(logger logger.Logger, shortlinkService services.ShortlinkService) gin.HandlerFunc {

View File

@ -10,7 +10,6 @@ import (
"backend/pkg/cache"
"backend/pkg/logger"
"context"
"fmt"
"sync"
"github.com/gin-gonic/gin"
@ -79,7 +78,6 @@ func main() {
}
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)
@ -99,13 +97,13 @@ func RunServer(ctx context.Context, log logger.Logger, tracer trace.Tracer, conf
r.Use(httpserver.NewTracingMiddleware(tracer))
linkGroup := r.Group("/s")
linkGroup.POST("/new", NewShortlinkCreateHandler(log, shortlinkService, host))
linkGroup.POST("/new", NewShortlinkCreateGinHandler(log, shortlinkService, conf.GetServiceUrl()))
linkGroup.GET("/:linkId", NewShortlinkResolveHandler(log, shortlinkService))
grpcUnderlying := grpc.NewServer()
shortlinks.RegisterShortlinksServer(
grpcUnderlying,
NewShortlinksGrpc(log, shortlinkService, host),
NewShortlinksGrpc(log, shortlinkService, conf.GetServiceUrl()),
)
httpServer := httpserver.New(