Skip to content

Commit

Permalink
move main function to registrar.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Jan 19, 2025
1 parent f0490ad commit b842709
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions node-registrar/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go run main.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --domain localhost --server-port 8080
go run cmds/registrar.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --domain localhost --server-port 8080

postgres:
docker run --name postgres -e POSTGRES_USER=postgres POSTGRES_PASSWORD=password POSTGRES_DB=postgres -p 5432:5432 -d postgres
Expand All @@ -8,10 +8,10 @@ stop-postgres:
docker stop postgres && docker rm postgres

build: ## Bulil the server
go build -o bin/server main.go
go build -o bin/server cmds/registrar.go

server-start:
@go run main.go \
@go run cmds/registrar.go \
--server-port :8080 \
--log-level debug \
--domain localhost \
Expand Down
24 changes: 17 additions & 7 deletions node-registrar/cmds/main.go → node-registrar/cmds/registrar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmds
package main

import (
"flag"
Expand Down Expand Up @@ -27,7 +27,13 @@ var (
version string
)

func Run() {
func main() {
if err := Run(); err != nil {
log.Fatal().Err(err).Send()
}
}

func Run() error {
f := flags{}
var sqlLogLevel int
flag.StringVar(&f.PostgresHost, "postgres-host", "", "postgres host")
Expand All @@ -49,11 +55,11 @@ func Run() {

if f.version {
log.Info().Str("version", version).Str("commit", commit).Send()
return
return nil
}

if err := f.validate(); err != nil {
log.Error().Err(err).Send()
return err
}

zerolog.SetGlobalLevel(zerolog.InfoLevel)
Expand All @@ -63,18 +69,19 @@ func Run() {

db, err := db.NewDB(f.Config)
if err != nil {
log.Fatal().Err(err).Msg("failed to open database with the specified configurations")
return errors.Wrap(err, "failed to open database with the specified configurations")
}

s, err := server.NewServer(db)
if err != nil {
log.Fatal().Err(err).Msg("failed to start gin server")
return errors.Wrap(err, "failed to start gin server")
}

err = s.Run(fmt.Sprintf("%s:%d", f.domain, f.serverPort))
if err != nil {
log.Fatal().Err(err).Msg("failed to run gin server")
return errors.Wrap(err, "failed to run gin server")
}
return nil
}

func (f flags) validate() error {
Expand All @@ -91,3 +98,6 @@ func (f flags) validate() error {

return f.Config.Validate()
}

//
// registar
7 changes: 0 additions & 7 deletions node-registrar/main.go

This file was deleted.

0 comments on commit b842709

Please sign in to comment.