mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
feat: added modules
This commit is contained in:
60
core/utils/postgresql.go
Normal file
60
core/utils/postgresql.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/crawlab-team/crawlab/core/constants"
|
||||
"github.com/crawlab-team/crawlab/core/models/models"
|
||||
"github.com/upper/db/v4"
|
||||
"github.com/upper/db/v4/adapter/postgresql"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetPostgresqlSession(ds *models.DataSource) (s db.Session, err error) {
|
||||
return getPostgresqlSession(context.Background(), ds)
|
||||
}
|
||||
|
||||
func GetPostgresqlSessionWithTimeout(ds *models.DataSource, timeout time.Duration) (s db.Session, err error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
return getPostgresqlSession(ctx, ds)
|
||||
}
|
||||
|
||||
func getPostgresqlSession(ctx context.Context, ds *models.DataSource) (s db.Session, err error) {
|
||||
// normalize settings
|
||||
host := ds.Host
|
||||
port := ds.Port
|
||||
if ds.Host == "" {
|
||||
host = constants.DefaultHost
|
||||
}
|
||||
if ds.Port == "" {
|
||||
port = constants.DefaultPostgresqlPort
|
||||
}
|
||||
|
||||
// connect settings
|
||||
settings := postgresql.ConnectionURL{
|
||||
User: ds.Username,
|
||||
Password: ds.Password,
|
||||
Database: ds.Database,
|
||||
Host: fmt.Sprintf("%s:%s", host, port),
|
||||
Options: nil,
|
||||
}
|
||||
|
||||
// session
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s, err = postgresql.Open(settings)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
// wait for done
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if ctx.Err() != nil {
|
||||
err = ctx.Err()
|
||||
}
|
||||
case <-done:
|
||||
}
|
||||
|
||||
return s, err
|
||||
}
|
||||
Reference in New Issue
Block a user