Skip to content

Commit

Permalink
drop endpoint cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
barryoneill committed Jun 13, 2022
1 parent 0c6acc5 commit af84437
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 43 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
14 changes: 0 additions & 14 deletions src/main/scala/io/laserdisc/slack4s/slashcmd/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] =
Expand All @@ -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
) {
Expand All @@ -50,24 +46,24 @@ 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 =
new SlashCommandBotBuilder(
signingSecret = signingSecret,
bindPort = bindPort,
bindAddress = bindAddress,
endpointCfg = endpointCfg,
endpointRoot = endpointRoot,
commandParser = commandParser,
http4sBuilder = http4sBuilder
)

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)
Expand Down Expand Up @@ -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)
}
)
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/io/laserdisc/slack4s/slashcmd/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down

0 comments on commit af84437

Please sign in to comment.