http server

This commit is contained in:
nquidox 2025-10-25 17:41:19 +03:00
parent a995f4c75a
commit efd0924c60
2 changed files with 35 additions and 4 deletions

View file

@ -7,15 +7,18 @@ import (
"imageStorage/config"
"imageStorage/internal/app"
"imageStorage/internal/convert"
"imageStorage/internal/httpDelivery"
"imageStorage/internal/mainHandler"
"imageStorage/internal/router"
"imageStorage/internal/utils"
"os"
"os/signal"
"syscall"
)
func main() {
//c := config.NewConfig()
c := config.DevConfig()
c := config.NewConfig()
//c := config.DevConfig()
config.LogSetup(c.App.LogLevel)
log.Infof("Log level: %s", c.App.LogLevel)
@ -27,14 +30,25 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
u := utils.NewHandler(c.App.Domain, c.App.Volume)
r := router.SetRouter("debug")
mainRoute := r.Group("/")
delivery := httpDelivery.NewHandler(u)
delivery.RegisterRoutes(mainRoute)
converter := convert.NewHandler()
imageServerHandler := mainHandler.NewHandler(mainHandler.Deps{
Converter: converter,
Volume: c.App.Volume,
Utils: u,
})
a := app.NewApp(app.Deps{
Config: c,
GrpcServer: imageServerHandler,
HttpServer: r,
})
if err := a.Start(ctx); err != nil {

View file

@ -2,32 +2,49 @@ package app
import (
"context"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"imageStorage/config"
"net"
"net/http"
"time"
)
type App struct {
config *config.Config
grpcServer *grpc.Server
config *config.Config
grpcServer *grpc.Server
httpServer *gin.Engine
httpServerAddr string
}
type Deps struct {
Config *config.Config
GrpcServer *grpc.Server
HttpServer *gin.Engine
}
func NewApp(deps Deps) *App {
return &App{
config: deps.Config,
grpcServer: deps.GrpcServer,
httpServer: deps.HttpServer,
}
}
func (app *App) Start(ctx context.Context) error {
serverErr := make(chan error, 1)
addr := app.config.App.Host + ":" + app.config.App.HttpPort
server := &http.Server{
Handler: app.httpServer,
Addr: addr,
}
go func() {
log.Info("Starting server on: ", addr)
serverErr <- server.ListenAndServe()
}()
endpoint := net.JoinHostPort(app.config.App.Host, app.config.App.GrpcPort)
go func() {
listener, err := net.Listen("tcp", endpoint)