mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 20:05:17 +03:00
feat: add register agent in db
This commit is contained in:
+18
-5
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user