Skip to content

Commit

Permalink
adjustments to fit replicaSet deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
justdeko committed Jul 18, 2021
1 parent 616ab07 commit 8455d7e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion functions/syncmesh-fn-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ functions:
GO111MODULE: on
write_debug: true
environment:
mongo_url: openfaas-db-mongodb
mongo_url: openfaas-db-mongodb-0.openfaas-db-mongodb-headless.openfaas-fn.svc.cluster.local/?directConnection=true
1 change: 1 addition & 0 deletions functions/syncmesh-fn/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func connectDB(ctx context.Context, db string, collection string) mongoDB {
mongoUrl = "openfaas-db-mongodb" // default mongodb kubernetes namespace if no env passed
}
uri := fmt.Sprintf("mongodb://%s", mongoUrl)
log.Printf("MongoDB access url: %s", uri)
// attempt connecting to mongodb
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions mongo_event_listener/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ FROM golang:1.16-alpine

WORKDIR /app

# change mongodb url to mongodb instead of localhost TODO: this does not work yet, find correct namespace/url in deployment
ENV mongo_url="openfaas-db-mongodb/?directConnection=true"

# copy mod files
COPY go.mod .
COPY go.sum .
Expand Down
8 changes: 5 additions & 3 deletions mongo_event_listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type StreamEvent struct {
}

func main() {
log.Println("Starting mongoDB change stream listener...")
ctx := context.Background()
// check mongodb value
mongoUrl, present := os.LookupEnv("mongo_url")
Expand All @@ -33,10 +34,10 @@ func main() {
syncmeshBaseUrl, present := os.LookupEnv("syncmesh_url")
if !present {
log.Println("Syncmesh function base url not found, defaulting to localhost")
mongoUrl = "localhost:8080" // default mongodb location if no env passed
syncmeshBaseUrl = "localhost:8080" // default mongodb location if no env passed
}
uri := fmt.Sprintf("mongodb://%s", mongoUrl)
log.Println(uri)
log.Printf("Full mongodb url: %s", uri)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
stopFatal(err)

Expand All @@ -55,7 +56,8 @@ func main() {
var data StreamEvent
err := sensorStream.Decode(&data)
stopFatal(err)
url := fmt.Sprintf("%s/functions/syncmesh-fn", syncmeshBaseUrl)
fmt.Printf("%v\n", data)
url := fmt.Sprintf("%s/function/syncmesh-fn", syncmeshBaseUrl)
jsonBody, err := json.Marshal(&data)
stopFatal(err)
_, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
Expand Down

0 comments on commit 8455d7e

Please sign in to comment.