Skip to content

Commit

Permalink
Added mail endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LotuxPunk committed Nov 4, 2023
1 parent dacb309 commit 25f88c4
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EXPOSE 8080:8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/hermes.jar
ENV CONTACT_FORM_CONFIGS=[]
ENV MAIL_CONFIGS=[]
ENV GOOGLE_RECAPTCHA_SECRET=
ENV SENDGRID_API_KEY=
ENTRYPOINT ["java","-jar","/app/hermes.jar"]
ENTRYPOINT ["java","-jar","/app/hermes.jar"]
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {

implementation("com.sendgrid:sendgrid-java:4.9.3")

implementation("net.pwall.mustache:kotlin-mustache:0.11")

testImplementation("io.ktor:ktor-server-tests-jvm")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
Expand All @@ -63,4 +65,4 @@ ktor {
hostname = providers.environmentVariable("DOCKER_REGISTRY_HOSTNAME")
)
}
}
}
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ services:
- "3000:3000"
environment:
CONTACT_FORM_CONFIGS: '[]'
MAIL_CONFIGS: '[]'
GOOGLE_RECAPTCHA_SECRET: ''
SENDGRID_API_KEY: ''
SENDGRID_API_KEY: ''
7 changes: 7 additions & 0 deletions src/main/kotlin/com/vandeas/dto/Mail.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.vandeas.dto

data class Mail(
val id: String,
val email: String,
val attributes: Map<String, String>
)
8 changes: 8 additions & 0 deletions src/main/kotlin/com/vandeas/dto/MailConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.vandeas.dto

data class MailConfig(
val id: String,
val sender: String,
val subjectTemplate: String,
val contentTemplate: String,
)
4 changes: 3 additions & 1 deletion src/main/kotlin/com/vandeas/logic/MailLogic.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.vandeas.logic

import com.vandeas.dto.ContactForm
import com.vandeas.dto.Mail
import com.vandeas.service.Response

interface MailLogic {
suspend fun sendContactForm(form: ContactForm): Response
}
suspend fun sendMail(mail: Mail): Response
}
17 changes: 16 additions & 1 deletion src/main/kotlin/com/vandeas/logic/impl/MailLogicImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.vandeas.logic.impl
import com.github.mustachejava.DefaultMustacheFactory
import com.github.mustachejava.Mustache
import com.vandeas.dto.ContactForm
import com.vandeas.dto.Mail
import com.vandeas.exception.RecaptchaFailedException
import com.vandeas.logic.MailLogic
import com.vandeas.service.*
import io.ktor.http.*
import net.pwall.mustache.Template
import java.io.StringWriter

class MailLogicImpl(
Expand Down Expand Up @@ -39,6 +41,19 @@ class MailLogicImpl(
)
}

override suspend fun sendMail(mail: Mail): Response {
val config = configLoader.getMailConfig(mail.id)
val contentTemplate = Template.parse(config.contentTemplate)
val subjectTemplate = Template.parse(config.subjectTemplate)

return mailer.sendEmail(
from = config.sender,
to = mail.email,
subject = subjectTemplate.processToString(mail.attributes),
content = contentTemplate.processToString(mail.attributes)
)
}

private fun getMustacheFactory(): DefaultMustacheFactory {
return DefaultMustacheFactory("templates")
}
Expand All @@ -53,4 +68,4 @@ class MailLogicImpl(
mustache.execute(writer, model).flush()
return writer.toString()
}
}
}
18 changes: 18 additions & 0 deletions src/main/kotlin/com/vandeas/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vandeas.plugins

import com.vandeas.dto.ContactForm
import com.vandeas.dto.Mail
import com.vandeas.exception.RecaptchaFailedException
import com.vandeas.logic.MailLogic
import com.vandeas.logic.impl.MailLogicImpl
Expand Down Expand Up @@ -40,6 +41,23 @@ fun Application.configureRouting() {
}
}
}
post {
val mail = call.receive<Mail>()

try {
val response = mailLogic.sendMail(mail)
if (response.isSuccessful) {
call.respond(HttpStatusCode.NoContent)
} else {
call.respond(HttpStatusCode.fromValue(response.statusCode), response.body ?: "")
}
} catch (e: Exception) {
when (e) {
is IllegalArgumentException -> call.respond(HttpStatusCode.BadRequest, e.message ?: "")
else -> call.respond(HttpStatusCode.InternalServerError)
}
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/vandeas/service/ConfigLoader.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.vandeas.service

import com.vandeas.dto.ContactFormConfig
import com.vandeas.dto.MailConfig

interface ConfigLoader {
fun getContactFormConfig(id: String): ContactFormConfig
}
fun getMailConfig(id: String): MailConfig
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.vandeas.service.impl
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.vandeas.dto.ContactFormConfig
import com.vandeas.dto.MailConfig
import com.vandeas.service.ConfigLoader

class EnvVariableConfigLoaderImpl : ConfigLoader {
Expand All @@ -13,7 +14,17 @@ class EnvVariableConfigLoaderImpl : ConfigLoader {
}
}

private val mailConfigs: Map<String, MailConfig> = System.getenv("MAIL_CONFIGS").let {
(jacksonObjectMapper().readValue(it, object : TypeReference<List<MailConfig>>() {})).associateBy { config ->
config.id
}
}

override fun getContactFormConfig(id: String): ContactFormConfig {
return configs[id] ?: throw IllegalArgumentException("No config found for id $id")
}
}

override fun getMailConfig(id: String): MailConfig {
return mailConfigs[id] ?: throw IllegalArgumentException("No config found for id $id")
}
}
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
</root>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/>
</configuration>
</configuration>

0 comments on commit 25f88c4

Please sign in to comment.