Skip to content

Commit

Permalink
Merge pull request #91 from illacloud/develop
Browse files Browse the repository at this point in the history
feat! & fix
  • Loading branch information
naj1n authored Dec 15, 2022
2 parents 222fc75 + 83a0567 commit 42442ba
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 23 deletions.
22 changes: 14 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/illa-family/builder-backend
go 1.17

require (
cloud.google.com/go/firestore v1.6.1
firebase.google.com/go/v4 v4.10.0
github.com/ClickHouse/clickhouse-go/v2 v2.2.0
github.com/aws/aws-sdk-go-v2 v1.17.1
github.com/aws/aws-sdk-go-v2/config v1.18.1
github.com/aws/aws-sdk-go-v2/credentials v1.13.1
Expand All @@ -24,9 +26,9 @@ require (
github.com/jackc/pgx/v4 v4.16.1
github.com/mitchellh/mapstructure v1.4.3
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
go.mongodb.org/mongo-driver v1.10.3
go.uber.org/zap v1.21.0
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
google.golang.org/api v0.96.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
Expand All @@ -37,7 +39,6 @@ require (
require (
cloud.google.com/go v0.102.1 // indirect
cloud.google.com/go/compute v1.7.0 // indirect
cloud.google.com/go/firestore v1.6.1 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
cloud.google.com/go/storage v1.26.0 // indirect
github.com/MicahParks/keyfunc v1.5.1 // indirect
Expand All @@ -62,10 +63,10 @@ require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -77,26 +78,31 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/paulmach/orb v0.7.1 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
70 changes: 61 additions & 9 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions pkg/action/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package action

import (
"github.com/illa-family/builder-backend/pkg/plugins/clickhouse"
"github.com/illa-family/builder-backend/pkg/plugins/common"
"github.com/illa-family/builder-backend/pkg/plugins/elasticsearch"
"github.com/illa-family/builder-backend/pkg/plugins/firebase"
Expand All @@ -41,6 +42,7 @@ var (
SMTP_ACTION = "smtp"
SUPABASEDB_ACTION = "supabasedb"
FIREBASE_ACTION = "firebase"
CLICKHOUSE_ACTION = "clickhouse"
)

type AbstractActionFactory interface {
Expand Down Expand Up @@ -80,6 +82,9 @@ func (f *Factory) Build() common.DataConnector {
case FIREBASE_ACTION:
firebaseAction := &firebase.Connector{}
return firebaseAction
case CLICKHOUSE_ACTION:
clickhouseAction := &clickhouse.Connector{}
return clickhouseAction
default:
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/action/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"go.uber.org/zap"
)

var type_array = [14]string{"transformer", "restapi", "graphql", "redis", "mysql", "mariadb", "postgresql", "mongodb", "tidb", "elasticsearch", "s3", "smtp", "supabasedb", "firebase"}
var type_array = [15]string{"transformer", "restapi", "graphql", "redis", "mysql", "mariadb", "postgresql", "mongodb",
"tidb", "elasticsearch", "s3", "smtp", "supabasedb", "firebase", "clickhouse"}
var type_map = map[string]int{
"transformer": 0,
"restapi": 1,
Expand All @@ -39,6 +40,7 @@ var type_map = map[string]int{
"smtp": 11,
"supabasedb": 12,
"firebase": 13,
"clickhouse": 14,
}

type ActionService interface {
Expand All @@ -57,7 +59,7 @@ type ActionDto struct {
Version int `json:"-"`
Resource int `json:"resourceId,omitempty"`
DisplayName string `json:"displayName" validate:"required"`
Type string `json:"actionType" validate:"oneof=transformer restapi graphql redis mysql mariadb postgresql mongodb tidb elasticsearch s3 smtp supabasedb firebase"`
Type string `json:"actionType" validate:"oneof=transformer restapi graphql redis mysql mariadb postgresql mongodb tidb elasticsearch s3 smtp supabasedb firebase clickhouse"`
Template map[string]interface{} `json:"content" validate:"required"`
Transformer map[string]interface{} `json:"transformer" validate:"required"`
TriggerMode string `json:"triggerMode" validate:"oneof=manually automate"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type AppServiceImpl struct {
actionRepository repository.ActionRepository
}

var type_array = [14]string{"transformer", "restapi", "graphql", "redis", "mysql", "mariadb", "postgresql", "mongodb", "tidb", "elasticsearch", "s3", "smtp", "supabasedb", "firebase"}
var type_array = [15]string{"transformer", "restapi", "graphql", "redis", "mysql", "mariadb", "postgresql", "mongodb", "tidb", "elasticsearch", "s3", "smtp", "supabasedb", "firebase", "clickhouse"}

type AppDto struct {
ID int `json:"appId"` // generated by database primary key serial
Expand Down
110 changes: 110 additions & 0 deletions pkg/plugins/clickhouse/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2022 The ILLA Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package clickhouse

import (
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/pem"
"errors"
"fmt"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/mitchellh/mapstructure"
)

const (
tableSQLStr = "SHOW TABLES"
columnSQLStr = "DESCRIBE TABLE "
)

func (c *Connector) getConnectionWithOptions(resourceOptions map[string]interface{}) (*sql.DB, error) {
if err := mapstructure.Decode(resourceOptions, &c.ResourceOpts); err != nil {
return nil, err
}

t := &tls.Config{}
if c.ResourceOpts.SSL.SSL && c.ResourceOpts.SSL.SelfSigned {
pool := x509.NewCertPool()
if ok := pool.AppendCertsFromPEM([]byte(c.ResourceOpts.SSL.CACert)); !ok {
return nil, errors.New("clickhouse SSL/TLS Connection failed")
}
t.RootCAs = pool
ccBlock, _ := pem.Decode([]byte(c.ResourceOpts.SSL.ClientCert))
ckBlock, _ := pem.Decode([]byte(c.ResourceOpts.SSL.PrivateKey))
if (ccBlock != nil && ccBlock.Type == "CERTIFICATE") && (ckBlock != nil || ckBlock.Type == "PRIVATE KEY") {
cert, err := tls.X509KeyPair([]byte(c.ResourceOpts.SSL.ClientCert), []byte(c.ResourceOpts.SSL.PrivateKey))
if err != nil {
return nil, err
}
t.Certificates = []tls.Certificate{cert}
}
}

db := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", c.ResourceOpts.Host, c.ResourceOpts.Port)},
Auth: clickhouse.Auth{
Database: c.ResourceOpts.DatabaseName,
Username: c.ResourceOpts.Username,
Password: c.ResourceOpts.Password,
},
TLS: t,
})

return db, nil
}

func tablesInfo(db *sql.DB, dbName string) []string {
tableNames := make([]string, 0, 0)
tableRows, err := db.Query(tableSQLStr, dbName)
if err != nil {
return nil
}
for tableRows.Next() {
var tableName string
err = tableRows.Scan(&tableName)
if err != nil {
return nil
}

tableNames = append(tableNames, tableName)
}

return tableNames
}

func fieldsInfo(db *sql.DB, dbName string, tableNames []string) map[string]interface{} {
columns := make(map[string]interface{})
for _, tableName := range tableNames {
tmpSQLStr := columnSQLStr + tableName
columnRows, err := db.Query(tmpSQLStr)
if err != nil {
return nil
}
tables := make(map[string]interface{})
for columnRows.Next() {
var Name, Type, defaultType, defaultExpression, ttlExpression, comment, codecExpression string
err = columnRows.Scan(&Name, &Type, &defaultType, &defaultExpression, &ttlExpression, &comment, &codecExpression)
if err != nil {
return nil
}
tables[Name] = map[string]string{"data_type": Type}

}
columns[tableName] = tables
}
return columns
}
Loading

0 comments on commit 42442ba

Please sign in to comment.