mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-16 18:06:45 +00:00
a27d005ffc
* dburl.nim: simpler regex that can support db_urls with . and - in hostname * dbrul.nim: accepting any non-empty sequence for user and password * dburl.nim: skipping validation for 'sqlite' db paths
34 lines
931 B
Nim
34 lines
931 B
Nim
|
|
import
|
|
std/strutils,
|
|
regex,
|
|
stew/results
|
|
|
|
proc validateDbUrl*(dbUrl: string): Result[string, string] =
|
|
## dbUrl mimics SQLAlchemy Database URL schema
|
|
## See: https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls
|
|
let regex = re"^\w+:\/\/.+:.+@[\w*-.]+:[0-9]+\/[\w*-.]+$"
|
|
let dbUrl = dbUrl.strip()
|
|
if "sqlite" in dbUrl or dbUrl == "" or dbUrl == "none" or dbUrl.match(regex):
|
|
return ok(dbUrl)
|
|
else:
|
|
return err("invalid 'db url' option format: " & dbUrl)
|
|
|
|
proc getDbEngine*(dbUrl: string): Result[string, string] =
|
|
let dbUrlParts = dbUrl.split("://", 1)
|
|
|
|
if dbUrlParts.len != 2:
|
|
return err("Incorrect dbUrl : " & dbUrl)
|
|
|
|
let engine = dbUrlParts[0]
|
|
return ok(engine)
|
|
|
|
proc getDbPath*(dbUrl: string): Result[string, string] =
|
|
let dbUrlParts = dbUrl.split("://", 1)
|
|
|
|
if dbUrlParts.len != 2:
|
|
return err("Incorrect dbUrl : " & dbUrl)
|
|
|
|
let path = dbUrlParts[1]
|
|
return ok(path)
|