Created
March 4, 2022 06:08
-
-
Save percybolmer/762337f21feb4b725e2ed199f41e281f to your computer and use it in GitHub Desktop.
Cadence API for greetings and execute workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GreetUser is used to Welcome a new User into the tavern | |
func (cc *CadenceClient) GreetUser(w http.ResponseWriter, r *http.Request) { | |
// Grab user info from body | |
var visitor customer.Customer | |
err := json.NewDecoder(r.Body).Decode(&visitor) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
// Trigger Workflow here | |
log.Print(visitor) | |
// Create workflow options, this is the same as the CLI, a task list, a timeout timer | |
opts := client.StartWorkflowOptions{ | |
TaskList: "greetings", | |
ExecutionStartToCloseTimeout: time.Second * 10, | |
} | |
log.Println("Starting workflow") | |
// This is how you Execute a Workflow and wait for it to finish | |
// This is useful if you have synchronous workflows that you want to leverage as functions | |
future, err := cc.client.ExecuteWorkflow(r.Context(), opts, GreetingsWorkflow, visitor) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
log.Println("Get Result from workflow") | |
// Fetch result once done and marshal into | |
if err := future.Get(r.Context(), &visitor); err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
data, _ := json.Marshal(visitor) | |
w.WriteHeader(http.StatusOK) | |
w.Write(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment