Skip to content

Commit

Permalink
cmd: Allows connectivity to MySQL
Browse files Browse the repository at this point in the history
Closes #82
  • Loading branch information
arekkas committed Jul 7, 2018
1 parent ae15d5f commit 42f8595
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
7 changes: 5 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 20 additions & 36 deletions cmd/helper_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,45 @@
package cmd

import (
"runtime"
"time"

"net/url"

"github.com/jmoiron/sqlx"
"github.com/ory/oathkeeper/rule"
"github.com/ory/sqlcon"
"github.com/pkg/errors"
)

func connectToSql(url string) (*sqlx.DB, error) {
db, err := sqlx.Open("postgres", url)
func connectToSql(dburl string) (*sqlx.DB, error) {
u, err := url.Parse(dburl)
if err != nil {
return nil, errors.WithStack(err)
logger.Fatalf("Could not parse DATABASE_URL: %s", err)
}

maxConns := maxParallelism() * 2
maxConnLifetime := time.Duration(0)
maxIdleConns := maxParallelism()
db.SetMaxOpenConns(maxConns)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxLifetime(maxConnLifetime)
return db, nil
}

func maxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
if maxProcs < numCPU {
return maxProcs
switch u.Scheme {
case "postgres":
fallthrough
case "mysql":
connection, err := sqlcon.NewSQLConnection(dburl, logger)
if err != nil {
logger.WithError(err).Fatalf(`Unable to initialize SQL connection`)
}
return connection.GetDatabase(), nil
}
return numCPU

return nil, errors.Errorf(`Unknown DSN "%s" in DATABASE_URL: %s`, u.Scheme, dburl)
}

func newRuleManager(db string) (rule.Manager, error) {
if db == "memory" {
func newRuleManager(dburl string) (rule.Manager, error) {
if dburl == "memory" {
return &rule.MemoryManager{Rules: map[string]rule.Rule{}}, nil
} else if db == "" {
} else if dburl == "" {
return nil, errors.New("No database URL provided")
}

u, err := url.Parse(db)
db, err := connectToSql(dburl)
if err != nil {
return nil, errors.WithStack(err)
}

switch u.Scheme {
case "postgres":
db, err := connectToSql(db)
if err != nil {
return nil, errors.WithStack(err)
}

return rule.NewSQLManager(db), nil
}

return nil, errors.Errorf("The provided database URL %s can not be handled", db)
return rule.NewSQLManager(db), nil
}

0 comments on commit 42f8595

Please sign in to comment.