2024-11-18 15:23:13 +00:00
|
|
|
// Copyright 2024 Patial Tech. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-11-10 09:22:33 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2024-11-19 05:10:30 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2024-11-10 09:22:33 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"contrib.go.opencensus.io/integrations/ocsql"
|
|
|
|
"entgo.io/ent/dialect"
|
|
|
|
entsql "entgo.io/ent/dialect/sql"
|
|
|
|
pgx "github.com/jackc/pgx/v5/stdlib"
|
|
|
|
"gitserver.in/patialtech/rano/config"
|
|
|
|
"gitserver.in/patialtech/rano/db/ent"
|
2024-11-15 16:12:15 +00:00
|
|
|
"gitserver.in/patialtech/rano/util/logger"
|
2024-11-10 09:22:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type connector struct {
|
|
|
|
dsn string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New *sql.DB instance
|
|
|
|
func New() *sql.DB {
|
|
|
|
databaseUrl := config.Read().DbURL
|
|
|
|
db := sql.OpenDB(connector{dsn: databaseUrl})
|
|
|
|
db.SetMaxOpenConns(50)
|
2024-11-17 16:58:29 +00:00
|
|
|
db.SetMaxIdleConns(5)
|
|
|
|
db.SetConnMaxLifetime(0) // Maximum amount of time a connection can be reused (0 means no limit)
|
2024-11-10 09:22:33 +00:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c connector) Connect(context.Context) (driver.Conn, error) {
|
|
|
|
return c.Driver().Open(c.dsn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (connector) Driver() driver.Driver {
|
|
|
|
return ocsql.Wrap(
|
|
|
|
pgx.GetDefaultDriver(),
|
|
|
|
ocsql.WithAllTraceOptions(),
|
|
|
|
ocsql.WithRowsClose(false),
|
|
|
|
ocsql.WithRowsNext(false),
|
|
|
|
ocsql.WithDisableErrSkip(true),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client for pgx
|
|
|
|
//
|
|
|
|
// https://entgo.io/docs/sql-integration
|
|
|
|
func Client() *ent.Client {
|
|
|
|
// Create an ent.Driver from `db`.
|
|
|
|
drv := entsql.OpenDB(dialect.Postgres, New())
|
|
|
|
cl := ent.NewClient(ent.Driver(drv))
|
|
|
|
cl.Use(AuditHook)
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// A AuditHook is an example for audit-log hook.
|
|
|
|
func AuditHook(next ent.Mutator) ent.Mutator {
|
2024-11-15 16:12:15 +00:00
|
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (v ent.Value, err error) {
|
2024-11-19 05:10:30 +00:00
|
|
|
// start timer
|
2024-11-10 09:22:33 +00:00
|
|
|
start := time.Now()
|
2024-11-19 05:10:30 +00:00
|
|
|
// do the operation
|
2024-11-15 16:12:15 +00:00
|
|
|
v, err = next.Mutate(ctx, m)
|
2024-11-19 05:10:30 +00:00
|
|
|
|
|
|
|
// audit log
|
|
|
|
var id int64
|
|
|
|
if v != nil {
|
|
|
|
ev := reflect.Indirect(reflect.ValueOf(v))
|
|
|
|
f := ev.FieldByName("ID")
|
|
|
|
if f.IsValid() {
|
|
|
|
id = f.Int()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
saveAudit(ctx, m.Type(), m.Op(), id, err, time.Since(start))
|
|
|
|
|
2024-11-15 16:12:15 +00:00
|
|
|
return
|
2024-11-10 09:22:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-19 05:10:30 +00:00
|
|
|
func saveAudit(ctx context.Context, t string, op ent.Op, id int64, err error, d time.Duration) {
|
2024-11-18 15:23:13 +00:00
|
|
|
if t == "Audit" {
|
|
|
|
// skip Audit table operations
|
|
|
|
return
|
|
|
|
}
|
2024-11-10 09:22:33 +00:00
|
|
|
|
2024-11-18 15:23:13 +00:00
|
|
|
var entOp string
|
|
|
|
switch {
|
|
|
|
case op.Is(ent.OpCreate):
|
|
|
|
entOp = "Create"
|
|
|
|
case op.Is(ent.OpDelete):
|
|
|
|
entOp = "Delete"
|
|
|
|
case op.Is(ent.OpDeleteOne):
|
|
|
|
entOp = "DeleteOne"
|
|
|
|
case op.Is(ent.OpUpdate):
|
|
|
|
entOp = "Update"
|
|
|
|
case op.Is(ent.OpUpdateOne):
|
|
|
|
entOp = "UpdateOne"
|
|
|
|
}
|
2024-11-10 09:22:33 +00:00
|
|
|
|
2024-11-19 05:10:30 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
if reqID, ok := ctx.Value("RequestID").(string); ok {
|
|
|
|
sb.WriteString(reqID + " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
sb.WriteString("failed ")
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.WriteString(fmt.Sprintf("%s:%s:%d ", entOp, t, id))
|
|
|
|
|
|
|
|
if ip, ok := ctx.Value("RequestIP").(string); ok {
|
|
|
|
sb.WriteString(fmt.Sprintf("ip=%s ", ip))
|
|
|
|
}
|
2024-11-10 09:22:33 +00:00
|
|
|
|
2024-11-19 05:10:30 +00:00
|
|
|
if ua, ok := ctx.Value("RequestUA").(string); ok {
|
|
|
|
sb.WriteString(fmt.Sprintf("ip=%s ", ua))
|
2024-11-18 15:23:13 +00:00
|
|
|
}
|
2024-11-19 05:10:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
sb.WriteString(fmt.Sprintf("error=%s ", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.WriteString(fmt.Sprintf("t=%s", d))
|
|
|
|
|
|
|
|
logger.Info(sb.String())
|
2024-11-10 09:22:33 +00:00
|
|
|
}
|