Skip to content

Commit

Permalink
added auth in clickhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithunikzz committed Oct 23, 2023
1 parent 5fd64f0 commit 731514b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
45 changes: 34 additions & 11 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,40 @@ type DBInterface interface {

func NewDBClient(conf *config.Config) (DBInterface, error) {
ctx := context.Background()
log.Println("Connecting to Clickhouse DB and creating schemas...")
splconn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Debugf: func(format string, v ...any) {
fmt.Printf(format, v)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
})
var connOptions clickhouse.Options

if conf.ClickHouseUsername != "" && conf.ClickHousePassword != "" {
connOptions = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Auth: clickhouse.Auth{
Username: conf.ClickHouseUsername,
Password: conf.ClickHousePassword,
},
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v...)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
}
fmt.Printf("Connecting to ClickHouse using usename and password")
} else {
connOptions = clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", conf.DBAddress, conf.DbPort)},
Debug: true,
Debugf: func(format string, v ...interface{}) {
fmt.Printf(format, v...)
},
Settings: clickhouse.Settings{
"allow_experimental_object_type": 1,
},
}
fmt.Printf("Connecting to ClickHouse without usename and password")

}

splconn, err := clickhouse.Open(&connOptions)
if err != nil {
return nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions client/pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

type Config struct {
NatsAddress string `envconfig:"NATS_ADDRESS"`
NatsToken string `envconfig:"NATS_TOKEN"`
DbPort int `envconfig:"DB_PORT"`
DBAddress string `envconfig:"DB_ADDRESS"`
NatsAddress string `envconfig:"NATS_ADDRESS"`
NatsToken string `envconfig:"NATS_TOKEN"`
DbPort int `envconfig:"DB_PORT"`
DBAddress string `envconfig:"DB_ADDRESS"`
ClickHouseUsername string `envconfig:"CLICKHOUSE_USERNAME"`
ClickHousePassword string `envconfig:"CLICKHOUSE_PASSWORD"`
}

0 comments on commit 731514b

Please sign in to comment.