Skip to content

Commit

Permalink
feat: ✨ implement OnRecordAfterCreateRequest() hook to run tagger i…
Browse files Browse the repository at this point in the history
…f environment variables set
Dan6erbond committed May 13, 2023
1 parent b2ae2a7 commit abce21a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ package main

import (
"log"
"net/http"
"net/url"
"os"

_ "github.com/joho/godotenv/autoload"
@@ -52,6 +54,43 @@ func main() {
return nil
})

app.OnRecordBeforeCreateRequest("files", "create", "defaults").Add(func(e *core.RecordCreateEvent) error {
if e.Record.Collection().Name != "files" {
return nil
}

e.Record.Set("tags", "[]")
e.Record.Set("tagsSuggestions", "[]")

return nil
})

if os.Getenv("TAGGER_HOST") != "" {
app.OnRecordAfterCreateRequest("files", "create", "tag").Add(func(e *core.RecordCreateEvent) error {
if e.Record.Collection().Name != "files" {
return nil
}

url, err := url.Parse(os.Getenv("TAGGER_HOST"))

if err != nil {
log.Println(err)
return err
}

url.Path = "files/" + e.Record.Id

_, err = http.Post(url.String(), "application/json", nil)

if err != nil {
log.Println(err)
return err
}

return err
})
}

if err := app.Start(); err != nil {
log.Fatal(err)
}

0 comments on commit abce21a

Please sign in to comment.