Skip to content

Commit

Permalink
chore: golang example end using negentropy dependency plus simple rea…
Browse files Browse the repository at this point in the history
…dme.md (#3235)

* avoid dependency with libpcre by using regex module
  • Loading branch information
Ivansete-status authored Jan 15, 2025
1 parent 625c8ee commit 0e0fcfb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 24 deletions.
44 changes: 44 additions & 0 deletions examples/golang/README.md
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 1 addition & 1 deletion examples/golang/waku.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 3 additions & 3 deletions waku/common/databases/db_postgres/dbconn.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import
std/[times, strutils, asyncnet, os, sequtils, sets, strformat],
regex,
results,
chronos,
chronos/threadsync,
metrics,
re,
chronicles
import ./query_metrics

Expand Down Expand Up @@ -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()
Expand Down
25 changes: 15 additions & 10 deletions waku/common/databases/db_postgres/pgasyncpool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{.push raises: [].}

import
std/[sequtils, nre, strformat],
std/[sequtils, strformat],
regex,
results,
chronos,
chronos/threadsync,
Expand All @@ -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(
Expand Down
17 changes: 9 additions & 8 deletions waku/waku_api/rest/origin_handler.nim
Original file line number Diff line number Diff line change
@@ -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(",")

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0e0fcfb

Please sign in to comment.