chore: golang example end using negentropy dependency plus simple readme.md (#3235)

* avoid dependency with libpcre by using regex module
This commit is contained in:
Ivan FB 2025-01-15 10:32:22 +01:00 committed by GitHub
parent 625c8ee51b
commit 0e0fcfb1a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 74 additions and 24 deletions

44
examples/golang/README.md Normal file
View File

@ -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
```

View File

@ -1,7 +1,7 @@
package main package main
/* /*
#cgo LDFLAGS: -L../../build/ -lwaku -lnegentropy #cgo LDFLAGS: -L../../build/ -lwaku
#cgo LDFLAGS: -L../../ -Wl,-rpath,../../ #cgo LDFLAGS: -L../../ -Wl,-rpath,../../
#include "../../library/libwaku.h" #include "../../library/libwaku.h"

View File

@ -1,10 +1,10 @@
import import
std/[times, strutils, asyncnet, os, sequtils, sets, strformat], std/[times, strutils, asyncnet, os, sequtils, sets, strformat],
regex,
results, results,
chronos, chronos,
chronos/threadsync, chronos/threadsync,
metrics, metrics,
re,
chronicles chronicles
import ./query_metrics import ./query_metrics
@ -247,8 +247,8 @@ proc dbConnQuery*(
let cleanedQuery = ($query).replace(" ", "").replace("\n", "") let cleanedQuery = ($query).replace(" ", "").replace("\n", "")
## remove everything between ' or " all possible sequence of numbers. e.g. rm partition partition ## remove everything between ' or " all possible sequence of numbers. e.g. rm partition partition
var querySummary = cleanedQuery.replace(re"""(['"]).*?\1""", "") var querySummary = cleanedQuery.replace(re2("""(['"]).*?\\1"""), "")
querySummary = querySummary.replace(re"\d+", "") querySummary = querySummary.replace(re2"\d+", "")
querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 200)] querySummary = "query_tag_" & querySummary[0 ..< min(querySummary.len, 200)]
var queryStartTime = getTime().toUnixFloat() var queryStartTime = getTime().toUnixFloat()

View File

@ -3,7 +3,8 @@
{.push raises: [].} {.push raises: [].}
import import
std/[sequtils, nre, strformat], std/[sequtils, strformat],
regex,
results, results,
chronos, chronos,
chronos/threadsync, chronos/threadsync,
@ -23,17 +24,21 @@ proc new*(T: type PgAsyncPool, dbUrl: string, maxConnections: int): DatabaseResu
var connString: string var connString: string
try: try:
let regex = re("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""") let regex = re2("""^postgres:\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)$""")
let matches = find(dbUrl, regex).get.captures var m: RegexMatch2
let user = matches[0] if dbUrl.match(regex, m) == false:
let password = matches[1] return err("could not properly parse dbUrl: " & dbUrl)
let host = matches[2]
let port = matches[3] let user = dbUrl[m.captures[0]]
let dbName = matches[4] ## 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 = connString =
fmt"user={user} host={host} port={port} dbname={dbName} password={password}" fmt"user={user} host={host} port={port} dbname={dbName} password={password}"
except KeyError, InvalidUnicodeError, RegexInternalError, ValueError, StudyError, except KeyError, ValueError:
SyntaxError:
return err("could not parse postgres string: " & getCurrentExceptionMsg()) return err("could not parse postgres string: " & getCurrentExceptionMsg())
let pool = PgAsyncPool( let pool = PgAsyncPool(

View File

@ -1,34 +1,35 @@
{.push raises: [].} {.push raises: [].}
import import
std/[options, strutils, re, net], std/[options, strutils, net],
regex,
results, results,
chronicles, chronicles,
chronos, chronos,
chronos/apps/http/httpserver chronos/apps/http/httpserver
type OriginHandlerMiddlewareRef* = ref object of HttpServerMiddlewareRef type OriginHandlerMiddlewareRef* = ref object of HttpServerMiddlewareRef
allowedOriginMatcher: Option[Regex] allowedOriginMatcher: Option[Regex2]
everyOriginAllowed: bool everyOriginAllowed: bool
proc isEveryOriginAllowed(maybeAllowedOrigin: Option[string]): bool = proc isEveryOriginAllowed(maybeAllowedOrigin: Option[string]): bool =
return maybeAllowedOrigin.isSome() and maybeAllowedOrigin.get() == "*" return maybeAllowedOrigin.isSome() and maybeAllowedOrigin.get() == "*"
proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] = proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex2] =
if maybeAllowedOrigin.isNone(): if maybeAllowedOrigin.isNone():
return none(Regex) return none(Regex2)
let allowedOrigin = maybeAllowedOrigin.get() let allowedOrigin = maybeAllowedOrigin.get()
if (len(allowedOrigin) == 0): if (len(allowedOrigin) == 0):
return none(Regex) return none(Regex2)
try: try:
var matchOrigin: string var matchOrigin: string
if allowedOrigin == "*": if allowedOrigin == "*":
matchOrigin = r".*" matchOrigin = r".*"
return some(re(matchOrigin, {reIgnoreCase, reExtended})) return some(re2(matchOrigin, {regexCaseless, regexExtended}))
let allowedOrigins = allowedOrigin.split(",") let allowedOrigins = allowedOrigin.split(",")
@ -54,11 +55,11 @@ proc compileOriginMatcher(maybeAllowedOrigin: Option[string]): Option[Regex] =
let finalExpression = matchExpressions.join("|") let finalExpression = matchExpressions.join("|")
return some(re(finalExpression, {reIgnoreCase, reExtended})) return some(re2(finalExpression, {regexCaseless, regexExtended}))
except RegexError: except RegexError:
var msg = getCurrentExceptionMsg() var msg = getCurrentExceptionMsg()
error "Failed to compile regex", source = allowedOrigin, err = msg error "Failed to compile regex", source = allowedOrigin, err = msg
return none(Regex) return none(Regex2)
proc originsMatch( proc originsMatch(
originHandler: OriginHandlerMiddlewareRef, requestOrigin: string originHandler: OriginHandlerMiddlewareRef, requestOrigin: string

View File

@ -1,7 +1,7 @@
{.push raises: [].} {.push raises: [].}
import import
std/[nre, options, sequtils, strutils, strformat, times, sugar], std/[options, sequtils, strutils, strformat, times, sugar],
stew/[byteutils, arrayops], stew/[byteutils, arrayops],
results, results,
chronos, chronos,

View File

@ -4,7 +4,7 @@ else:
{.push raises: [].} {.push raises: [].}
import import
std/[nre, options, sequtils, strutils, strformat, times], std/[options, sequtils, strutils, strformat, times],
stew/[byteutils, arrayops], stew/[byteutils, arrayops],
results, results,
chronos, chronos,