Private
Public Access
feat: añadido soporte para mandar webhooks
This commit is contained in:
1 parent
4c0dbaa91c
commit
d5dd03fff5
12 files changed
+665
-6
No files matched your search
@@ -3,20 +3,26 @@ package com.fede.comms
|
||||
import cats.effect.{Async, Resource}
|
||||
import com.comcast.ip4s._
|
||||
import com.fede.comms.config.SmtpConfig
|
||||
import com.fede.comms.emails.SmtpEmail
|
||||
import com.fede.comms.routes.AllRoutes
|
||||
// import com.fede.comms.config.WebHookConfig
|
||||
import fs2.io.net.Network
|
||||
import org.http4s.ember.server.EmberServerBuilder
|
||||
import org.http4s.ember.client.EmberClientBuilder
|
||||
import org.http4s.implicits._
|
||||
import org.http4s.server.middleware.Logger
|
||||
import com.fede.comms.webhook.HttpWebHook
|
||||
import com.fede.comms.emails.SmtpEmail
|
||||
|
||||
object CommsServer {
|
||||
|
||||
def run[F[_]: Async: Network]: F[Nothing] = {
|
||||
for {
|
||||
client <- EmberClientBuilder.default[F].build
|
||||
smtpConfig <- Resource.eval(SmtpConfig.load[F]())
|
||||
email = new SmtpEmail[F](smtpConfig)
|
||||
webhook = new HttpWebHook[F](client)
|
||||
|
||||
httpApp = CommsRoutes.emailRoutes[F](email).orNotFound
|
||||
httpApp = AllRoutes.allRoutes[F](email, webhook).orNotFound
|
||||
|
||||
finalHttpApp = Logger.httpApp(true, true)(httpApp)
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ object SmtpConfig {
|
||||
user = required("SMTP_USER"),
|
||||
password = required("SMTP_PASSWORD"),
|
||||
from = required("SMTP_FROM"),
|
||||
to = required("SMTP_TO")
|
||||
to = required("SMTP_TO") //testing only
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.fede.comms.routes
|
||||
|
||||
import cats.effect.Concurrent
|
||||
import com.fede.comms.emails.{Email}
|
||||
import com.fede.comms.routes.WebhookRoutes
|
||||
import cats.syntax.all._
|
||||
import org.http4s.HttpRoutes
|
||||
import com.fede.comms.webhook.HttpWebHook
|
||||
|
||||
object AllRoutes {
|
||||
def allRoutes[F[_]: Concurrent](email: Email[F], webhook: HttpWebHook[F]): HttpRoutes[F] =
|
||||
EmailRoutes.emailRoutes(email) <+> WebhookRoutes.webhookRoutes(webhook)
|
||||
}
|
||||
+2
-3
@@ -1,4 +1,4 @@
|
||||
package com.fede.comms
|
||||
package com.fede.comms.routes
|
||||
|
||||
import cats.effect.Concurrent
|
||||
import cats.syntax.all._
|
||||
@@ -7,8 +7,7 @@ import org.http4s.HttpRoutes
|
||||
import org.http4s.circe.CirceEntityDecoder._
|
||||
import org.http4s.dsl.Http4sDsl
|
||||
|
||||
object CommsRoutes {
|
||||
|
||||
object EmailRoutes {
|
||||
def emailRoutes[F[_]: Concurrent](email: Email[F]): HttpRoutes[F] = {
|
||||
val dsl = new Http4sDsl[F] {}
|
||||
import dsl._
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.fede.comms.routes
|
||||
|
||||
import cats.effect.Concurrent
|
||||
import org.http4s.HttpRoutes
|
||||
import org.http4s.circe.CirceEntityDecoder._
|
||||
import cats.syntax.all._
|
||||
import com.fede.comms.webhook.{WebHook,Mensaje}
|
||||
import org.http4s.dsl.Http4sDsl
|
||||
|
||||
object WebhookRoutes {
|
||||
def webhookRoutes[F[_]: Concurrent](hook: WebHook[F]): HttpRoutes[F] = {
|
||||
val dsl = new Http4sDsl[F] {}
|
||||
import dsl._
|
||||
HttpRoutes.of[F] {
|
||||
case req @ POST -> Root / "webhook" =>
|
||||
req.attemptAs[Mensaje].foldF(
|
||||
_ => BadRequest(),
|
||||
mensaje => hook.enviar(mensaje) *> Ok()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.fede.comms.webhook
|
||||
|
||||
import io.circe.generic.semiauto._
|
||||
import io.circe.Encoder
|
||||
|
||||
final case class DiscordField(name: String, value: String, inline: Boolean = false)
|
||||
|
||||
object DiscordField {
|
||||
implicit val encoder: Encoder[DiscordField] = deriveEncoder
|
||||
}
|
||||
|
||||
final case class DiscordEmbed(
|
||||
title: String,
|
||||
description: String,
|
||||
color: Int,
|
||||
fields: List[DiscordField]
|
||||
)
|
||||
|
||||
object DiscordEmbed {
|
||||
implicit val encoder: Encoder[DiscordEmbed] = deriveEncoder
|
||||
}
|
||||
|
||||
final case class DiscordWebhook(content: String = "", embeds: List[DiscordEmbed])
|
||||
|
||||
object DiscordWebhook {
|
||||
implicit val encoder: Encoder[DiscordWebhook] = deriveEncoder
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.fede.comms.webhook
|
||||
|
||||
import cats.effect.Concurrent
|
||||
import cats.syntax.all._
|
||||
import org.http4s._
|
||||
import org.http4s.Method.POST
|
||||
import org.http4s.client.Client
|
||||
import org.http4s.headers.`Content-Type`
|
||||
import org.http4s.MediaType
|
||||
|
||||
class HttpWebHook[F[_]: Concurrent](client: Client[F]) extends WebHook[F] {
|
||||
|
||||
def enviar(m: Mensaje): F[Unit] =
|
||||
m.to.traverse_ { target =>
|
||||
val request =
|
||||
Request[F](
|
||||
method = POST,
|
||||
uri = Uri.unsafeFromString(target)
|
||||
).withEntity(m.toStringDiscord)
|
||||
.putHeaders(`Content-Type`(MediaType.application.json))
|
||||
|
||||
client.run(request).use { response =>
|
||||
if (response.status.isSuccess)
|
||||
Concurrent[F].unit
|
||||
else
|
||||
response.as[String].flatMap { body =>
|
||||
Concurrent[F].raiseError[Unit](
|
||||
new RuntimeException(
|
||||
s"Webhook POST to $target failed with status ${response.status.code}: $body"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.fede.comms.webhook
|
||||
|
||||
import io.circe.{Decoder, Encoder}
|
||||
import io.circe.generic.semiauto._
|
||||
import org.http4s.circe.{jsonOf}
|
||||
import io.circe.generic.auto._
|
||||
import io.circe.syntax._
|
||||
// import org.http3s.circe.{jsonEncoderOf}
|
||||
import org.http4s.{EntityDecoder}
|
||||
// import org.http4s.{EntityEncoder}
|
||||
import cats.effect.Concurrent
|
||||
|
||||
final case class Mensaje(
|
||||
titulo: String,
|
||||
contenido: String,
|
||||
descripcion: String,
|
||||
items: List[String],
|
||||
to: List[String]
|
||||
) {
|
||||
def toStringDiscord: String = {
|
||||
val fields = items.zipWithIndex.map { case (item, i) =>
|
||||
DiscordField(name = s"Item ${i + 1}", value = item.take(1024), inline = true)
|
||||
}
|
||||
val embed = DiscordEmbed(
|
||||
title = titulo.take(256),
|
||||
description = descripcion.take(4096),
|
||||
color = 0x00ff00,
|
||||
fields = fields
|
||||
)
|
||||
DiscordWebhook(content = contenido, embeds = List(embed)).asJson.noSpaces
|
||||
}
|
||||
}
|
||||
|
||||
object Mensaje {
|
||||
implicit val decoder: Decoder[Mensaje] = deriveDecoder
|
||||
implicit val encoder: Encoder[Mensaje] = deriveEncoder
|
||||
|
||||
implicit def entityDecoder[F[_]: Concurrent]: EntityDecoder[F, Mensaje] = jsonOf[F, Mensaje]
|
||||
// implicit def entityEncoder[F[_]: Concurrent]: EntityEncoder[F, Mensaje] = jsonEncoderOf[F, Mensaje]
|
||||
}
|
||||
|
||||
trait WebHook[F[_]] {
|
||||
def enviar(m: Mensaje): F[Unit]
|
||||
}
|
||||
|
||||
object WebHook {
|
||||
def apply[F[_]](implicit ev: WebHook[F]): WebHook[F] = ev
|
||||
}
|
||||
Reference in new issue
Block a user