From 0e0fcfb1a58dbe33aa25dbd3183c3d7d83bae86a Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:32:22 +0100 Subject: [PATCH] chore: golang example end using negentropy dependency plus simple readme.md (#3235) * avoid dependency with libpcre by using regex module --- examples/golang/README.md | 44 +++++++++++++++++++ examples/golang/waku.go | 2 +- waku/common/databases/db_postgres/dbconn.nim | 6 +-- .../databases/db_postgres/pgasyncpool.nim | 25 ++++++----- waku/waku_api/rest/origin_handler.nim | 17 +++---- .../postgres_driver/postgres_driver.nim | 2 +- .../postgres_driver/postgres_driver.nim | 2 +- 7 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 examples/golang/README.md diff --git a/examples/golang/README.md b/examples/golang/README.md new file mode 100644 index 0000000000..39d4882931 --- /dev/null +++ b/examples/golang/README.md @@ -0,0 +1,44 @@ + +## Pre-requisite +libwaku.so is needed to be compiled and present in build folder. To create it: + +- Run only the first time and after changing the current commit +```code +make update +``` +- Run the next every time you want to compile libwaku +```code +make POSTGRES=1 libwaku -j4 +``` + +Also needed: + +- Install libpq (needed by Postgres client) + - On Linux: +```code +sudo apt install libpq5 +``` + - On MacOS (not tested) +```code +brew install libpq +``` + +## Compilation + +From the nwaku root folder: + +```code +go build -o waku-go examples/golang/waku.go +``` + +## Run +From the nwaku root folder: + + +```code +export LD_LIBRARY_PATH=build +``` + +```code +./waku-go +``` diff --git a/examples/golang/waku.go b/examples/golang/waku.go index ad7c40b345..846362dfe5 100644 --- a/examples/golang/waku.go +++ b/examples/golang/waku.go @@ -1,7 +1,7 @@ package main /* - #cgo LDFLAGS: -L../../build/ -lwaku -lnegentropy + #cgo LDFLAGS: -L../../build/ -lwaku #cgo LDFLAGS: -L../../ -Wl,-rpath,../../ #include "../../library/libwaku.h" diff --git a/waku/common/databases/db_postgres/dbconn.nim b/waku/common/databases/db_postgres/dbconn.nim index 243c195c36..84584a10dc 100644 --- a/waku/common/databases/db_postgres/dbconn.nim +++ b/waku/common/databases/db_postgres/dbconn.nim @@ -1,10 +1,10 @@ import std/[times, strutils, asyncnet, os, sequtils, sets, strformat], + regex, results, chronos, chronos/threadsync, metrics, - re, chronicles import ./query_metrics @@ -247,8 +247,8 @@ proc dbConnQuery*( let cleanedQuery = ($query).replace(" ", "").replace("\n", "") ## remove everything between ' or " all possible sequence of numbers. e.g. rm partition partition - var querySummary = cleanedQuery.replace(re"""(['"]).*?\1""", "") - querySummary = querySummary.replace(re"\d+", "") + var querySummary = cleanedQuery.replace(re2("""(['"]).*?\\1"""), "") + querySummary = querySummary.replace(re2"\d+", "") querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 200)] var queryStartTime = getTime().toUnixFloat() diff --git a/waku/common/databases/db_postgres/pgasyncpool.nim b/waku/common/databases/db_postgres/pgasyncpool.nim index 10e70eb515..d1e2908424 100644 --- a/waku/common/databases/db_postgres/pgasyncpool.nim +++ b/waku/common/databases/db_postgres/pgasyncpool.nim @@ -3,7 +3,8 @@ {.push raises: [].} import - std/[sequtils, nre, strformat], + std/[sequtils, strformat], + regex, results, chronos, chronos/threadsync, @@ -23,17 +24,21 @@ proc new*(T: type PgAsyncPool, dbUrl: string, maxConnections: int): DatabaseResu var connString: string try: - let regex = re("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""") - let matches = find(dbUrl, regex).get.captures - let user = matches[0] - let password = matches[1] - let host = matches[2] - let port = matches[3] - let dbName = matches[4] + let regex = re2("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""") + var m: RegexMatch2 + if dbUrl.match(regex, m) == false: + return err("could not properly parse dbUrl: " & dbUrl) + + let user = dbUrl[m.captures[0]] + ## m.captures[i] contains an slice with the desired value + let password = dbUrl[m.captures[1]] + let host = dbUrl[m.captures[2]] + let port = dbUrl[m.captures[3]] + let dbName = dbUrl[m.captures[4]] + connString = fmt"user={user} host={host} port={port} dbname={dbName} password={password}" - except KeyError, InvalidUnicodeError, RegexInternalError, ValueError, StudyError, - SyntaxError: + except KeyError, ValueError: return err("could not parse postgres string: " & getCurrentExceptionMsg()) let pool = PgAsyncPool( diff --git a/waku/waku_api/rest/origin_handler.nim b/waku/waku_api/rest/origin_handler.nim index a7be36a4b5..2317c945f9 100644 --- a/waku/waku_api/rest/origin_handler.nim +++ b/waku/waku_api/rest/origin_handler.nim @@ -1,34 +1,35 @@ {.push raises: [].} import - std/[options, strutils, re, net], + std/[options, strutils, net], + regex, results, chronicles, chronos, chronos/apps/http/httpserver type OriginHandlerMiddlewareRef* = ref object of HttpServerMiddlewareRef - allowedOriginMatcher: Option[Regex] + allowedOriginMatcher: Option[Regex2] everyOriginAllowed: bool proc isEveryOriginAllowed(maybeAllowedOrigin: Option[string]): bool = return maybeAllowedOrigin.isSome() and maybeAllowedOrigin.get() == "*" -proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] = +proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex2] = if maybeAllowedOrigin.isNone(): - return none(Regex) + return none(Regex2) let allowedOrigin = maybeAllowedOrigin.get() if (len(allowedOrigin) == 0): - return none(Regex) + return none(Regex2) try: var matchOrigin: string if allowedOrigin == "*": matchOrigin = r".*" - return some(re(matchOrigin, {reIgnoreCase, reExtended})) + return some(re2(matchOrigin, {regexCaseless, regexExtended})) let allowedOrigins = allowedOrigin.split(",") @@ -54,11 +55,11 @@ proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] = let finalExpression = matchExpressions.join("|") - return some(re(finalExpression, {reIgnoreCase, reExtended})) + return some(re2(finalExpression, {regexCaseless, regexExtended})) except RegexError: var msg = getCurrentExceptionMsg() error "Failed to compile regex", source = allowedOrigin, err = msg - return none(Regex) + return none(Regex2) proc originsMatch( originHandler: OriginHandlerMiddlewareRef, requestOrigin: string diff --git a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim index bfdffa6bf2..6c49ee15ac 100644 --- a/waku/waku_archive/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive/driver/postgres_driver/postgres_driver.nim @@ -1,7 +1,7 @@ {.push raises: [].} import - std/[nre, options, sequtils, strutils, strformat, times, sugar], + std/[options, sequtils, strutils, strformat, times, sugar], stew/[byteutils, arrayops], results, chronos, diff --git a/waku/waku_archive_legacy/driver/postgres_driver/postgres_driver.nim b/waku/waku_archive_legacy/driver/postgres_driver/postgres_driver.nim index e8eed42386..3891184c11 100644 --- a/waku/waku_archive_legacy/driver/postgres_driver/postgres_driver.nim +++ b/waku/waku_archive_legacy/driver/postgres_driver/postgres_driver.nim @@ -4,7 +4,7 @@ else: {.push raises: [].} import - std/[nre, options, sequtils, strutils, strformat, times], + std/[options, sequtils, strutils, strformat, times], stew/[byteutils, arrayops], results, chronos,