@@ -39,6 +39,21 @@ import (
3939 "github.com/joho/godotenv"
4040)
4141
42+ // getEnv is a helper function to read an environment variable or return a fallback value.
43+ func getEnv (key , fallback string ) string {
44+ if value , ok := os .LookupEnv (key ); ok {
45+ return value
46+ }
47+ return fallback
48+ }
49+
50+ var (
51+ blockchainRestAPIURL = getEnv ("BLOCKCHAIN_REST_API_URL" , "http://localhost:1317" )
52+ grpcAddr = getEnv ("GRPC_ADDR" , "localhost:9090" )
53+ nodeRPCURL = getEnv ("NODE_RPC_URL" , "http://localhost:26657" )
54+ faucetURL = getEnv ("FAUCET_URL" , "http://localhost:4500" )
55+ )
56+
4257func init () {
4358 zlog .Logger = zlog .Output (zerolog.ConsoleWriter {Out : os .Stdout })
4459}
@@ -257,8 +272,9 @@ func NewServer(clientCtx client.Context, grpcAddr string) (*Server, error) {
257272 // Ensure that the faucet account from config exists in the keyring.
258273 if _ , err := s .clientCtx .Keyring .Key (s .faucetName ); err != nil {
259274 zlog .Warn ().Msgf ("faucet user '%s' from config.yml not found in keyring: %v" , s .faucetName , err )
275+ } else {
276+ zlog .Info ().Msgf ("Faucet user '%s' found in keyring." , s .faucetName )
260277 }
261- zlog .Info ().Msgf ("Using '%s' as the faucet account." , s .faucetName )
262278
263279 // Ensure faucet user has the 'bank' role.
264280 if faucetUserData , ok := s .users [s .faucetName ]; ok {
@@ -329,7 +345,7 @@ func (s *Server) adminLoginHandler(c *fiber.Ctx) error {
329345
330346// passthroughGET proxies a GET request to the blockchain REST API and returns the response as-is.
331347func passthroughGET (path string , c * fiber.Ctx ) error {
332- baseURL := "http://localhost:1317"
348+ baseURL := blockchainRestAPIURL
333349 // Compose the full URL
334350 url := baseURL + path
335351 resp , err := http .Get (url )
@@ -391,45 +407,40 @@ func getMortgagesPassthrough(c *fiber.Ctx) error {
391407// isBlockchainRunning checks if both the blockchain REST API and gRPC API are accessible
392408func isBlockchainRunning () bool {
393409 // Check REST API
394- baseURL := os .Getenv ("BLOCKCHAIN_REST_API_URL" )
395- if baseURL == "" {
396- baseURL = "http://localhost:1317"
397- }
410+ baseURL := blockchainRestAPIURL
398411 restURL := baseURL + "/cosmos/base/tendermint/v1beta1/node_info"
412+ zlog .Info ().Msgf ("Checking blockchain REST API at %s" , restURL )
399413
400- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
401- defer cancel ()
414+ restCtx , restCancel := context .WithTimeout (context .Background (), 10 * time .Second )
415+ defer restCancel ()
402416
403- req , err := http .NewRequestWithContext (ctx , "GET" , restURL , nil )
417+ req , err := http .NewRequestWithContext (restCtx , "GET" , restURL , nil )
404418 if err != nil {
419+ zlog .Error ().Msgf ("Blockchain not ready: failed to create request for REST API: %v" , err )
405420 return false
406421 }
407422
408423 resp , err := http .DefaultClient .Do (req )
409424 if err != nil {
425+ zlog .Error ().Msgf ("Blockchain not ready: failed to make request to REST API: %v" , err )
410426 return false
411427 }
412428 defer resp .Body .Close ()
413429
414- if resp .StatusCode != 200 {
430+ if resp .StatusCode != http .StatusOK {
431+ zlog .Error ().Msgf ("Blockchain not ready: REST API returned status %d" , resp .StatusCode )
415432 return false
416433 }
417434
418435 // Check gRPC API
419- grpcHost := "localhost"
420- if strings .Contains (baseURL , "://" ) {
421- if u , err := url .Parse (baseURL ); err == nil {
422- grpcHost = u .Hostname ()
423- }
424- }
425- grpcAddr := fmt .Sprintf ("%s:9090" , grpcHost )
436+ zlog .Info ().Msgf ("Checking blockchain gRPC API at %s" , grpcAddr )
426437
427- // Try to establish a gRPC connection
428- grpcConn , err := grpc .Dial (grpcAddr , grpc .WithTransportCredentials (insecure .NewCredentials ()), grpc .WithBlock (), grpc .WithTimeout (5 * time .Second ))
438+ conn , err := grpc .NewClient (grpcAddr , grpc .WithTransportCredentials (insecure .NewCredentials ()), grpc .WithBlock ())
429439 if err != nil {
440+ zlog .Error ().Msgf ("Blockchain not ready: NewClient failed for gRPC: %v" , err )
430441 return false
431442 }
432- defer grpcConn .Close ()
443+ defer conn .Close ()
433444
434445 return true
435446}
@@ -464,8 +475,12 @@ func main() {
464475 zlog .Fatal ().Msgf ("Failed to parse node URI: %v" , err )
465476 }
466477 host := strings .Split (parsedURL .Host , ":" )[0 ]
467- grpcAddr := fmt .Sprintf ("%s:9090" , host )
478+ clientCtxGrpcAddr := fmt .Sprintf ("%s:9090" , host )
468479
480+ if clientCtxGrpcAddr != grpcAddr {
481+ zlog .Warn ().Msgf ("Using grpc addresses don't match: %s != %s" , clientCtxGrpcAddr , grpcAddr )
482+ }
483+ // Use the client context's grpc address if it's set, otherwise use the default.
469484 server , err := NewServer (clientCtx , grpcAddr )
470485 if err != nil {
471486 zlog .Fatal ().Msgf ("Failed to create server: %v" , err )
@@ -557,3 +572,4 @@ func main() {
557572 zlog .Fatal ().Msgf ("Failed to start server: %v" , err )
558573 }
559574}
575+
0 commit comments