From af844370deed27a9914005cbac4dd45df0ecf60f Mon Sep 17 00:00:00 2001 From: Barry O'Neill Date: Sun, 12 Jun 2022 23:18:21 -0400 Subject: [PATCH] drop endpoint cfg --- README.md | 7 ----- project/Dependencies.scala | 6 ++-- .../laserdisc/slack4s/slashcmd/Models.scala | 14 ---------- .../slashcmd/SlashCommandBotBuilder.scala | 28 ++++++++----------- .../laserdisc/slack4s/slashcmd/package.scala | 8 ++++-- 5 files changed, 20 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 8b857b0..545f819 100644 --- a/README.md +++ b/README.md @@ -65,13 +65,6 @@ The builder has some more useful functions if you need to customize your deploym SlashCommandBotBuilder[IO](secret) .withCommandMapper(testCommandMapper) // your mapper impl, see next section .withBindOptions(port = 9999, address = "192.168.0.1") // by default, binds to 0.0.0.0:8080 - .withEndpointConfig(defaultEC => // endpoint config is a case class, this method gives you the default config and allows you to override via copy - defaultEC.copy( // the values shown are the defaults - healthCheckRoot = "/healthCheck", // - GET /healthCheck for health check - slackRoot = "/slack", // - root of all signature-protected slack endpoints (there's only 1 right now) - slackSlashCmd = "slashCmd" // - POST /slack/slashCmd for the slack command handler entrypoint - ) - ) .withHttp4sBuilder{ // offer the chance to customize http4s' BlazeServerBuilder used under the hood // USE WITH CAUTION; it overrides any settings set by slack4s diff --git a/project/Dependencies.scala b/project/Dependencies.scala index e0123a6..7e4506b 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -7,14 +7,14 @@ object Dependencies { val TestLib = Seq( libraryDependencies ++= Seq( "org.scalameta" %% "munit" % "0.7.29" % Test, - "org.mockito" % "mockito-core" % "4.5.1" % Test, + "org.mockito" % "mockito-core" % "4.6.1" % Test, "org.gnieh" %% "diffson-circe" % "4.1.1" ), testFrameworks += new TestFramework("munit.Framework") ) val Slack = Seq( - libraryDependencies += "com.slack.api" % "slack-app-backend" % "1.22.1" + libraryDependencies += "com.slack.api" % "slack-app-backend" % "1.22.2" ) val Refined = Seq( @@ -31,7 +31,7 @@ object Dependencies { ) ) - val Http4sVersion = "0.23.11" + val Http4sVersion = "0.23.12" val Http4s = Seq( libraryDependencies ++= Seq( "org.http4s" %% "http4s-dsl" % Http4sVersion, diff --git a/src/main/scala/io/laserdisc/slack4s/slashcmd/Models.scala b/src/main/scala/io/laserdisc/slack4s/slashcmd/Models.scala index d41e7fe..7682fb4 100644 --- a/src/main/scala/io/laserdisc/slack4s/slashcmd/Models.scala +++ b/src/main/scala/io/laserdisc/slack4s/slashcmd/Models.scala @@ -67,17 +67,3 @@ case object Delayed extends ResponseType * The intermediate message - e.g. "This might take a little while, please wait..." */ case class DelayedWithMsg(msg: ChatPostMessageRequest) extends ResponseType - -/** For configuring the endpoint names that the slackbot listens for - * @param healthCheckRoot - * The endpoint that responds to GET call for container health (e.g. "/healthCheck") - * @param slackRoot - * The root of the signature-protected slack-related calls (e.g. "/slack") - * @param slackSlashCmd - * The `slackRoot`-nested endpoint for the POST command for slash commands (e.g. "slashCmd") - */ -case class EndpointConfig( - healthCheckRoot: String, - slackRoot: String, - slackSlashCmd: String -) diff --git a/src/main/scala/io/laserdisc/slack4s/slashcmd/SlashCommandBotBuilder.scala b/src/main/scala/io/laserdisc/slack4s/slashcmd/SlashCommandBotBuilder.scala index 444ed34..ec2b8af 100644 --- a/src/main/scala/io/laserdisc/slack4s/slashcmd/SlashCommandBotBuilder.scala +++ b/src/main/scala/io/laserdisc/slack4s/slashcmd/SlashCommandBotBuilder.scala @@ -8,7 +8,7 @@ import io.laserdisc.slack4s.slashcmd.SlashCommandBotBuilder.Defaults import io.laserdisc.slack4s.slashcmd.internal.SignatureValidator._ import io.laserdisc.slack4s.slashcmd.internal._ import org.http4s._ -import org.http4s.blaze.server.BlazeServerBuilder +import org.http4s.blaze.server._ import org.http4s.dsl.Http4sDsl import org.http4s.server.{Router, ServiceErrorHandler} import org.typelevel.log4cats.Logger @@ -18,13 +18,9 @@ import slack4s.BuildInfo object SlashCommandBotBuilder { object Defaults { - val BindPort: BindPort = 8080 - val BindAddress: BindAddress = "0.0.0.0" - val EndpointCfg: EndpointConfig = EndpointConfig( - healthCheckRoot = "/healthCheck", - slackRoot = "/slack", - slackSlashCmd = "slashCmd" - ) + val BindPort: BindPort = 8080 + val BindAddress: BindAddress = "0.0.0.0" + val EndpointRoot: EndpointRoot = "/" } def apply[F[_]: Async](signingSecret: SigningSecret): SlashCommandBotBuilder[F] = @@ -35,7 +31,7 @@ class SlashCommandBotBuilder[F[_]: Async] private[slashcmd] ( signingSecret: SigningSecret, bindPort: BindPort = Defaults.BindPort, bindAddress: BindAddress = Defaults.BindAddress, - endpointCfg: EndpointConfig = Defaults.EndpointCfg, + endpointRoot: EndpointRoot = Defaults.EndpointRoot, commandParser: Option[CommandMapper[F]] = None, http4sBuilder: BlazeServerBuilder[F] => BlazeServerBuilder[F] = (b: BlazeServerBuilder[F]) => b ) { @@ -50,7 +46,7 @@ class SlashCommandBotBuilder[F[_]: Async] private[slashcmd] ( signingSecret: SigningSecret = signingSecret, bindPort: BindPort = bindPort, bindAddress: BindAddress = bindAddress, - endpointCfg: EndpointConfig = endpointCfg, + endpointRoot: EndpointRoot = endpointRoot, commandParser: Option[CommandMapper[F]] = commandParser, http4sBuilder: BlazeServerBuilder[F] => BlazeServerBuilder[F] = http4sBuilder ): Self = @@ -58,7 +54,7 @@ class SlashCommandBotBuilder[F[_]: Async] private[slashcmd] ( signingSecret = signingSecret, bindPort = bindPort, bindAddress = bindAddress, - endpointCfg = endpointCfg, + endpointRoot = endpointRoot, commandParser = commandParser, http4sBuilder = http4sBuilder ) @@ -66,8 +62,8 @@ class SlashCommandBotBuilder[F[_]: Async] private[slashcmd] ( def withBindOptions(port: BindPort, address: BindAddress = "0.0.0.0"): Self = copy(bindPort = port, bindAddress = address) - def withEndpointConfig(endpointCfgUpdate: EndpointConfig => EndpointConfig): Self = - copy(endpointCfg = endpointCfgUpdate(Defaults.EndpointCfg)) + def withEndpointRoot(root: EndpointRoot): Self = + copy(endpointRoot = root) def withHttp4sBuilder(http4sBuilder: BlazeServerBuilder[F] => BlazeServerBuilder[F]): Self = copy(http4sBuilder = http4sBuilder) @@ -115,11 +111,11 @@ class SlashCommandBotBuilder[F[_]: Async] private[slashcmd] ( def buildHttpApp(cmdRunner: CommandRunner[F]): HttpApp[F] = Router( - endpointCfg.healthCheckRoot -> HttpRoutes.of[F] { case GET -> Root => + s"${endpointRoot.value}healthCheck" -> HttpRoutes.of[F] { case GET -> Root => Ok.apply(s"OK") }, - endpointCfg.slackRoot -> withValidSignature(signingSecret).apply( - AuthedRoutes.of[SlackUser, F] { case req @ POST -> Root / `endpointCfg`.slackSlashCmd as _ => + s"${endpointRoot.value}slack" -> withValidSignature(signingSecret).apply( + AuthedRoutes.of[SlackUser, F] { case req @ POST -> Root / "slashCmd" as _ => cmdRunner.processRequest(req) } ) diff --git a/src/main/scala/io/laserdisc/slack4s/slashcmd/package.scala b/src/main/scala/io/laserdisc/slack4s/slashcmd/package.scala index 7ec6eaa..742ac2d 100644 --- a/src/main/scala/io/laserdisc/slack4s/slashcmd/package.scala +++ b/src/main/scala/io/laserdisc/slack4s/slashcmd/package.scala @@ -2,23 +2,25 @@ package io.laserdisc.slack4s import com.slack.api.app_backend.slash_commands.payload.SlashCommandPayload import eu.timepit.refined.api.{Refined, RefinedTypeOps} -import eu.timepit.refined.collection.NonEmpty import eu.timepit.refined.numeric.Positive import eu.timepit.refined.string.{MatchesRegex, Url} +import eu.timepit.refined.types.string.NonEmptyString package object slashcmd { - final type SigningSecret = String Refined NonEmpty + final type SigningSecret = NonEmptyString final type LogToken = String Refined MatchesRegex["[A-Za-z0-9\\-\\_]+"] final type URL = String Refined Url final type BindPort = Int Refined Positive - final type BindAddress = String Refined NonEmpty + final type BindAddress = NonEmptyString + final type EndpointRoot = NonEmptyString object SigningSecret extends RefinedTypeOps[SigningSecret, String] object LogToken extends RefinedTypeOps[LogToken, String] object URL extends RefinedTypeOps[URL, String] object BindPort extends RefinedTypeOps[BindPort, Int] object BindAddress extends RefinedTypeOps[BindAddress, String] + object EndpointRoot extends RefinedTypeOps[EndpointRoot, String] type CommandMapper[F[_]] = SlashCommandPayload => F[Command[F]]