Repository: answerdev/answer
File: internal/install/install_req.go (Go)

Code before:
// GetConnection get connection string
func (r *CheckDatabaseReq) GetConnection() string {
	if r.DbType == string(schemas.SQLITE) {
		return r.DbFile
	}
	if r.DbType == string(schemas.MYSQL) {
		return fmt.Sprintf("%s:%s@tcp(%s)/%s",
			r.DbUsername, r.DbPassword, r.DbHost, r.DbName)
	}
	if r.DbType == string(schemas.POSTGRES) {
		host, port := parsePgSQLHostPort(r.DbHost)
		if !r.Ssl {
			return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
				host, port, r.DbUsername, r.DbPassword, r.DbName)
		} else if r.SslMode == "require" {
			return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
				host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode)
		} else if r.SslMode == "verify-ca" || r.SslMode == "verify-full" {
			_, err_server_ca := os.Stat(r.SslCrt)
			_, err_client_cert := os.Stat(r.SslKey)
			_, err_client_key := os.Stat(r.SslCrtClient)
			if err_server_ca == nil || err_client_cert == nil || err_client_key == nil {
				return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s",
					host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode, r.SslCrt, r.SslCrtClient, r.SslKey)
			}
			log.Fatal("Certificate not Found !!")
		}
	}
	return ""
}

func parsePgSQLHostPort(dbHost string) (host string, port string) {
	if strings.Contains(dbHost, ":") {
		idx := strings.LastIndex(dbHost, ":")
		host, port = dbHost[:idx], dbHost[idx+1:]
	} else if len(dbHost) > 0 {
		host = dbHost
	}
	if host == "" {
		host = "127.0.0.1"
	}
	if port == "" {
		port = "5432"
	}
	return host, port
}

// CheckDatabaseResp check database response
type CheckDatabaseResp struct {
	ConnectionSuccess bool `json:"connection_success"`
}

Review comment (suggestion):
@unical1988 Almost done. This part needs to be adjusted.

1. Use the `dir.CheckFileExist` instead.
2. You don't need to `log.Fatal` if the file does not exist in this part. `log.Fatal` will cause panic.

If the file does not exist, the front end will show the error message when calling the `/installation/db/check` API. You can just log the warning message.

```go
if dir.CheckFileExist(r.SslCrt) {
    log.Warnf("ssl crt file not exist: %s", r.SslCrt)
}
if dir.CheckFileExist(r.SslCrtClient) {
    log.Warnf("ssl crt client file not exist: %s", r.SslCrtClient)
}
if dir.CheckFileExist(r.SslKey) {
    log.Warnf("ssl key file not exist: %s", r.SslKey)
}
return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s",
    host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode, r.SslCrt, r.SslCrtClient, r.SslKey)
```


Code after:
// GetConnection get connection string
func (r *CheckDatabaseReq) GetConnection() string {
	if r.DbType == string(schemas.SQLITE) {
		return r.DbFile
	}
	if r.DbType == string(schemas.MYSQL) {
		return fmt.Sprintf("%s:%s@tcp(%s)/%s",
			r.DbUsername, r.DbPassword, r.DbHost, r.DbName)
	}
	if r.DbType == string(schemas.POSTGRES) {
		host, port := parsePgSQLHostPort(r.DbHost)
		if !r.Ssl {
			return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
				host, port, r.DbUsername, r.DbPassword, r.DbName)
		} else if r.SslMode == "require" {
			return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
				host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode)
		} else if r.SslMode == "verify-ca" || r.SslMode == "verify-full" {
			if dir.CheckFileExist(r.SslCrt) {
				log.Warnf("ssl crt file not exist: %s", r.SslCrt)
			}
			if dir.CheckFileExist(r.SslCrtClient) {
				log.Warnf("ssl crt client file not exist: %s", r.SslCrtClient)
			}
			if dir.CheckFileExist(r.SslKey) {
				log.Warnf("ssl key file not exist: %s", r.SslKey)
			}
			return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s",
				host, port, r.DbUsername, r.DbPassword, r.DbName, r.SslMode, r.SslCrt, r.SslCrtClient, r.SslKey)
		}
	}
	return ""
}

func parsePgSQLHostPort(dbHost string) (host string, port string) {
	if strings.Contains(dbHost, ":") {
		idx := strings.LastIndex(dbHost, ":")
		host, port = dbHost[:idx], dbHost[idx+1:]
	} else if len(dbHost) > 0 {
		host = dbHost
	}
	if host == "" {
		host = "127.0.0.1"
	}
	if port == "" {
		port = "5432"
	}
	return host, port
}

// CheckDatabaseResp check database response