Skip to content

Commit

Permalink
fix api handler missing eventbus env var
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimon-au committed Jun 29, 2023
1 parent 5f78ed9 commit 3f9b884
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
84 changes: 47 additions & 37 deletions src/cloud/src/Contextive.Cloud.Api/Slack.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,51 @@ open System.Text.Json
open AwsHelpers
open SlackModels

let eventBusName() = System.Environment.GetEnvironmentVariable("EVENT_BUS_NAME")

let post : HttpHandler =
fun next ctx -> task {
let! event = ctx.BindJsonAsync<Receiving.SlackEvent>()
if event.Type = "url_verification" then
return! text event.Challenge next ctx
else
let logger = ctx.GetLogger("slack")
if event.Event.User <> "U02J05WTE75" && event.Event.Text <> null then
use eventBridgeClient = new AmazonEventBridgeClient(
AmazonEventBridgeConfig() |> forEnvironment
)
logger.LogInformation($"Sending event (not waiting), text is '{event.Event.Text}'")
let! res = eventBridgeClient.PutEventsAsync(
Model.PutEventsRequest(
Entries = ResizeArray<Model.PutEventsRequestEntry> [
Model.PutEventsRequestEntry(
EventBusName=eventBusName(),
Source="contextive.api",
DetailType=($"{event.Type}.{event.Event.Type}"),
Detail=JsonSerializer.Serialize(event.Event)
)
]
),
System.Threading.CancellationToken.None
)
logger.LogInformation($"Sent {res}.")
let eventBusName () =
System.Environment.GetEnvironmentVariable("EVENT_BUS_NAME")

let post: HttpHandler =
fun next ctx ->
task {
let! event = ctx.BindJsonAsync<Receiving.SlackEvent>()

if event.Type = "url_verification" then
return! text event.Challenge next ctx
else
logger.LogInformation($"NOT sending event as channel is {event.Event.Channel} or text is '{event.Event.Text}'")

return! text "" next ctx
}

let routes:HttpHandler =
choose [
POST >=> route "/slack" >=> post
]
let logger = ctx.GetLogger("slack")

if event.Event.User <> "U02J05WTE75" && event.Event.Text <> null then
use eventBridgeClient =
new AmazonEventBridgeClient(AmazonEventBridgeConfig() |> forEnvironment)

logger.LogInformation($"Sending event, text is '{event.Event.Text}'")

try
let! res =
eventBridgeClient.PutEventsAsync(
Model.PutEventsRequest(
Entries =
ResizeArray<Model.PutEventsRequestEntry>
[ Model.PutEventsRequestEntry(
EventBusName = eventBusName (),
Source = "contextive.api",
DetailType = ($"{event.Type}.{event.Event.Type}"),
Detail = JsonSerializer.Serialize(event.Event)
) ]
),
System.Threading.CancellationToken.None
)

logger.LogInformation($"Sent {res}.")
with ex ->
logger.LogError(ex.ToString())
raise ex
else
logger.LogInformation(
$"NOT sending event as channel is {event.Event.Channel} or text is '{event.Event.Text}'"
)

return! text "" next ctx
}

let routes: HttpHandler = choose [ POST >=> route "/slack" >=> post ]
4 changes: 2 additions & 2 deletions src/cloud/src/Contextive.Cloud/ContextiveCloudStack.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ContextiveCloudStack(scope, id, props) as this =
let eventBus = EventBus(this, "SlackEvents", EventBusProps())

let eventHandlerFunctionProps =
LambdaFunctions.props this "Event Handler" "EventHandlerSetup" definitions
LambdaFunctions.props this "Event Handler" "EventHandlerSetup" definitions eventBus

let eventHandlerFunction = Function(this, "EventHandler", eventHandlerFunctionProps)

Expand All @@ -35,7 +35,7 @@ type ContextiveCloudStack(scope, id, props) as this =
|> ignore

let apiFunctionProps =
LambdaFunctions.props this "Api" "Setup+LambdaEntryPoint" definitions
LambdaFunctions.props this "Api" "Setup+LambdaEntryPoint" definitions eventBus

let apiFunction = Function(this, "Api", apiFunctionProps)

Expand Down
6 changes: 4 additions & 2 deletions src/cloud/src/Contextive.Cloud/LambdaFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ open System
open Amazon.CDK
open Amazon.CDK.AWS.Lambda
open Amazon.CDK.AWS.Logs
open Amazon.CDK.AWS.Events
open Amazon.CDK.AWS.S3

let props construct name entryPointModule (definitions: IBucket) =
let props construct name entryPointModule (definitions: IBucket) (eventBus: IEventBus) =
let assemblyPath relativePath =
let basePath =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Expand All @@ -24,6 +25,7 @@ let props construct name entryPointModule (definitions: IBucket) =
Map
[ "DEFINITIONS_BUCKET_NAME", definitions.BucketName
"SLACK_OAUTH_TOKEN", (System.Environment.GetEnvironmentVariable("SLACK_OAUTH_TOKEN"))
"IS_LOCAL", (Context.isLocal construct).ToString() ],
"IS_LOCAL", (Context.isLocal construct).ToString()
"EVENT_BUS_NAME", eventBus.EventBusName ],
LogRetention = Option.toNullable (Some RetentionDays.ONE_WEEK)
)

0 comments on commit 3f9b884

Please sign in to comment.