Ver código fonte

insure the correct sed replacement for the database server address, and add extra checks to see if db connection working properly at code init time

silviucm 10 anos atrás
pai
commit
106c607887

+ 1 - 1
frameworks/Go/fasthttp-postgresql/setup.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
+sed -i 's|localhost|'"${DBHOST}"'|g' src/hello/hello.go
 
 fw_depends go
 

+ 1 - 1
frameworks/Go/fasthttp-postgresql/setup_prefork.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-sed -i 's|tcp(.*:3306)|tcp('"${DBHOST}"':3306)|g' src/hello/hello.go
+sed -i 's|localhost|'"${DBHOST}"'|g' src/hello/hello.go
 
 fw_depends go
 

+ 10 - 2
frameworks/Go/fasthttp-postgresql/src/hello/hello.go

@@ -64,9 +64,9 @@ var (
 func main() {
 	flag.Parse()
 
-	// func initDatabase(dbHost string, dbUser string, dbPass string, dbName string, dbPort int) (*pgx.ConnPool, error) {
-
 	var err error
+
+	// initialize the connection pool
 	if db, err = initDatabase("localhost", "benchmarkdbuser", "benchmarkdbpass", "hello_world", 5432, maxConnectionCount); err != nil {
 		log.Fatalf("Error opening database: %s", err)
 	}
@@ -305,6 +305,14 @@ func initDatabase(dbHost string, dbUser string, dbPass string, dbName string, db
 		log.Println("Connecting to database ", dbName, " as user ", dbUser, " ", successOrFailure, ": \n ", err)
 	} else {
 		log.Println("Connecting to database ", dbName, " as user ", dbUser, ": ", successOrFailure)
+
+		log.Println("Fetching one record to test if db connection is valid...")
+		var w World
+		n := randomWorldNum()
+		if errPing := connPool.QueryRow("worldSelectStmt", n).Scan(&w.Id, &w.RandomNumber); errPing != nil {
+			log.Fatalf("Error scanning world row: %s", errPing)
+		}
+		log.Println("OK")
 	}
 
 	fmt.Println("--------------------------------------------------------------------------------------------")