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 48859c4266
commit 9d0b30cc99
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
/*
#cgo LDFLAGS: -L../../build/ -lwaku -lnegentropy
#cgo LDFLAGS: -L../../build/ -lwaku
#cgo LDFLAGS: -L../../ -Wl,-rpath,../../
#include "../../library/libwaku.h"

View File

@ -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()

View File

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

View File

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

View File

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

View File

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