From 541746574958dcb83fa94879946672d0953724a5 Mon Sep 17 00:00:00 2001 From: aronerben Date: Tue, 30 Mar 2021 23:00:23 +0200 Subject: [PATCH] Add same smtp mail backend for marketing and for info --- .env | 9 +------ app/context/pizza/pizza.ml | 4 +-- config/mail.cfg | 7 +++++ dune-project | 7 ++--- pizza.opam | 7 ++--- run/run.ml | 2 ++ service/dune | 2 +- service/service.ml | 54 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 config/mail.cfg diff --git a/.env b/.env index d05ffdf..16b2988 100644 --- a/.env +++ b/.env @@ -1,10 +1,3 @@ -SIHL_SECRET=secret +SIHL_SECRET=secretsecretsecret DATABASE_URL=postgres://admin:password@127.0.0.1:5432/dev DATABASE_POOL_SIZE=10 -SMTP_SENDER=sender@example.com -SMTP_PORT=587 -SMTP_USERNAME=user -SMTP_HOST=smtp.example.com -SMTP_START_TLS=true -SMTP_PASSWORD=yourpassword -CA_DIR=/etc/ssl/certs diff --git a/app/context/pizza/pizza.ml b/app/context/pizza/pizza.ml index c3d9433..c855635 100644 --- a/app/context/pizza/pizza.ml +++ b/app/context/pizza/pizza.ml @@ -2,14 +2,14 @@ include Model exception Exception of string -let clean = +let clean () = if Sihl.Configuration.is_production () then raise @@ Exception "Can not clean repository in production, this is most likely not what \ you want" - else Repo.clean + else Repo.clean () ;; let find_ingredient name = Repo.find_ingredient name diff --git a/config/mail.cfg b/config/mail.cfg new file mode 100644 index 0000000..d1d1474 --- /dev/null +++ b/config/mail.cfg @@ -0,0 +1,7 @@ +SMTP_SENDER=sender@example.com +SMTP_USERNAME=user +SMTP_PASSWORD=yourpassword +SMTP_HOST=smtp.example.com +SMTP_PORT=587 +SMTP_START_TLS=true +CA_DIR=/etc/ssl/certs \ No newline at end of file diff --git a/dune-project b/dune-project index 7551e7e..3ebc0ba 100644 --- a/dune-project +++ b/dune-project @@ -26,9 +26,10 @@ A restaurant serving Pizza and sometimes Lasagna (depends (ocaml (>= 4.08.0)) dune - (sihl (= 0.4.0)) - (sihl-user (= 0.4.0)) - (sihl-queue (= 0.4.0)) + (sihl (= 0.4.1)) + (sihl-user (= 0.4.1)) + (sihl-queue (= 0.4.1)) + (sihl-email (= 0.4.1)) (tyxml-ppx (>= 4.4.0)) (caqti-driver-postgresql (>= 1.2.1)) (alcotest-lwt :with-test) diff --git a/pizza.opam b/pizza.opam index 0a583e4..5107774 100644 --- a/pizza.opam +++ b/pizza.opam @@ -13,9 +13,10 @@ bug-reports: "https://github.com/oxidizing/pizza/issues" depends: [ "ocaml" {>= "4.08.0"} "dune" - "sihl" {= "0.4.0"} - "sihl-user" {= "0.4.0"} - "sihl-queue" {= "0.4.0"} + "sihl" {= "0.4.1"} + "sihl-user" {= "0.4.1"} + "sihl-queue" {= "0.4.1"} + "sihl-email" {= "0.4.1"} "tyxml-ppx" {>= "4.4.0"} "caqti-driver-postgresql" {>= "1.2.1"} "alcotest-lwt" {with-test} diff --git a/run/run.ml b/run/run.ml index 122bf3b..32cb89e 100644 --- a/run/run.ml +++ b/run/run.ml @@ -19,6 +19,8 @@ let services = ~jobs: [ Sihl_queue.hide Job.cook_pizza; Sihl_queue.hide Job.order_ingredient ] () + ; Service.MarketingMail.register () + ; Service.InfoMail.register () ] ;; diff --git a/service/dune b/service/dune index a5bcff5..1be7af3 100644 --- a/service/dune +++ b/service/dune @@ -1,3 +1,3 @@ (library (name service) - (libraries sihl sihl-user sihl-queue)) + (libraries sihl sihl-user sihl-queue sihl-email)) diff --git a/service/service.ml b/service/service.ml index 799570b..adc3ee0 100644 --- a/service/service.ml +++ b/service/service.ml @@ -1,3 +1,57 @@ module Migration = Sihl.Database.Migration.PostgreSql module User = Sihl_user.PostgreSql module Queue = Sihl_queue.PostgreSql + +module MarketingSmtpConfig = struct + let config () = + Lwt.return + Sihl_email. + { sender = "marketing@vinniespiz.za" + ; username = "vinnie" + ; password = "pinapple0nPizz4" + ; hostname = "smtp.example.com" + ; port = Some 587 + ; start_tls = true + ; ca_path = Some "/etc/ssl/certs" + ; ca_cert = None + ; console = Some true + } + ;; +end + +module InfoSmtpConfig = struct + let config () = + let open Lwt.Syntax in + Lwt_io.with_file ~mode:Lwt_io.Input "config/mail.cfg" (fun file -> + let* content = Lwt_stream.to_list @@ Lwt_io.read_lines file in + let config = + content + |> Stdlib.List.map (Stdlib.String.split_on_char '=') + |> Stdlib.List.map (function + | [] -> "", "" + | [ key ] -> key, "" + | [ key; value ] -> key, value + | key :: values -> key, Stdlib.String.concat "" values) + in + Lwt.return + Sihl_email. + { sender = Stdlib.List.assoc "SMTP_SENDER" config + ; username = Stdlib.List.assoc "SMTP_USERNAME" config + ; password = Stdlib.List.assoc "SMTP_PASSWORD" config + ; hostname = Stdlib.List.assoc "SMTP_HOST" config + ; port = + Option.map int_of_string + @@ Stdlib.List.assoc_opt "SMTP_SENDER" config + ; start_tls = + bool_of_string @@ Stdlib.List.assoc "SMTP_SENDER" config + ; ca_path = Stdlib.List.assoc_opt "SMTP_SENDER" config + ; ca_cert = Stdlib.List.assoc_opt "SMTP_SENDER" config + ; console = + Option.map bool_of_string + @@ Stdlib.List.assoc_opt "SMTP_SENDER" config + }) + ;; +end + +module MarketingMail = Sihl_email.MakeSmtp (MarketingSmtpConfig) +module InfoMail = Sihl_email.MakeSmtp (InfoSmtpConfig)