feat: add register agent in db

This commit is contained in:
lorsan
2026-04-16 13:38:35 +03:00
parent 65d8f883dc
commit 52766cf7e8
16 changed files with 271 additions and 29 deletions
+18 -5
View File
@@ -1,15 +1,18 @@
package app
import (
"context"
"database/sql"
"fmt"
standartlog "log"
"net"
"github.com/jackc/pgx/v5/pgxpool"
hubdir "github.com/lorsanstand/HomeOps-Hub/internal/hub"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/migrator"
grpcserv "github.com/lorsanstand/HomeOps-Hub/internal/hub/rpc"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/service/hub_service"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store"
"github.com/lorsanstand/HomeOps-Hub/internal/shared/config"
"github.com/lorsanstand/HomeOps-Hub/internal/shared/log"
"github.com/rs/zerolog"
@@ -32,6 +35,7 @@ func NewApp() *App {
}
func (a *App) Run() {
ctx := context.Background()
migratePGConn, err := sql.Open("pgx", a.cfg.GetURLPostgres())
if err != nil {
a.log.Error().Err(err).Msg("failed to connect to the database")
@@ -47,23 +51,32 @@ func (a *App) Run() {
if err = mgrt.ApplyMigrations(migratePGConn); err != nil {
a.log.Error().Err(err).Msg("migrations were not applied")
return
}
migratePGConn.Close()
err = a.hubServe()
pool, err := pgxpool.New(ctx, a.cfg.GetURLPostgres())
if err != nil {
a.log.Error().Err(err).Msg("failed create db pool")
return
}
hubStore := store.NewHubStore(pool)
hubService := hub_service.NewHubService(hubStore, a.log)
err = a.hubServe(hubService)
if err != nil {
a.log.Error().Err(err).Msg("failed to start the server")
return
}
}
func (a *App) hubServe() error {
func (a *App) hubServe(hubService *hub_service.HubService) error {
address := fmt.Sprintf("0.0.0.0:%v", a.cfg.Port)
a.log.Info().Str("address", "http://"+address).Msg("start GRPC server")
hub := hub_service.NewHubService(a.log)
server := grpcserv.NewHubHandler(hub, a.log)
server := grpcserv.NewHubHandler(hubService, a.log)
lis, err := net.Listen("tcp", address)
if err != nil {