http server
This commit is contained in:
parent
a995f4c75a
commit
efd0924c60
2 changed files with 35 additions and 4 deletions
18
cmd/main.go
18
cmd/main.go
|
|
@ -7,15 +7,18 @@ import (
|
||||||
"imageStorage/config"
|
"imageStorage/config"
|
||||||
"imageStorage/internal/app"
|
"imageStorage/internal/app"
|
||||||
"imageStorage/internal/convert"
|
"imageStorage/internal/convert"
|
||||||
|
"imageStorage/internal/httpDelivery"
|
||||||
"imageStorage/internal/mainHandler"
|
"imageStorage/internal/mainHandler"
|
||||||
|
"imageStorage/internal/router"
|
||||||
|
"imageStorage/internal/utils"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//c := config.NewConfig()
|
c := config.NewConfig()
|
||||||
c := config.DevConfig()
|
//c := config.DevConfig()
|
||||||
config.LogSetup(c.App.LogLevel)
|
config.LogSetup(c.App.LogLevel)
|
||||||
log.Infof("Log level: %s", 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)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
defer cancel()
|
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()
|
converter := convert.NewHandler()
|
||||||
imageServerHandler := mainHandler.NewHandler(mainHandler.Deps{
|
imageServerHandler := mainHandler.NewHandler(mainHandler.Deps{
|
||||||
Converter: converter,
|
Converter: converter,
|
||||||
|
Volume: c.App.Volume,
|
||||||
|
Utils: u,
|
||||||
})
|
})
|
||||||
|
|
||||||
a := app.NewApp(app.Deps{
|
a := app.NewApp(app.Deps{
|
||||||
Config: c,
|
Config: c,
|
||||||
GrpcServer: imageServerHandler,
|
GrpcServer: imageServerHandler,
|
||||||
|
HttpServer: r,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := a.Start(ctx); err != nil {
|
if err := a.Start(ctx); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -2,32 +2,49 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"imageStorage/config"
|
"imageStorage/config"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
config *config.Config
|
config *config.Config
|
||||||
grpcServer *grpc.Server
|
grpcServer *grpc.Server
|
||||||
|
httpServer *gin.Engine
|
||||||
|
httpServerAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
type Deps struct {
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
GrpcServer *grpc.Server
|
GrpcServer *grpc.Server
|
||||||
|
HttpServer *gin.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(deps Deps) *App {
|
func NewApp(deps Deps) *App {
|
||||||
return &App{
|
return &App{
|
||||||
config: deps.Config,
|
config: deps.Config,
|
||||||
grpcServer: deps.GrpcServer,
|
grpcServer: deps.GrpcServer,
|
||||||
|
httpServer: deps.HttpServer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Start(ctx context.Context) error {
|
func (app *App) Start(ctx context.Context) error {
|
||||||
serverErr := make(chan error, 1)
|
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)
|
endpoint := net.JoinHostPort(app.config.App.Host, app.config.App.GrpcPort)
|
||||||
go func() {
|
go func() {
|
||||||
listener, err := net.Listen("tcp", endpoint)
|
listener, err := net.Listen("tcp", endpoint)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue