dnote/pkg/server/mailer/templates/main.go
Sung Won Cho 23a511dbe0
Improve package structure (#207)
* Improve package structure

* Set up travis
2019-06-25 19:20:19 +10:00

89 lines
2.3 KiB
Go

/* Copyright (C) 2019 Monomax Software Pty Ltd
*
* This file is part of Dnote.
*
* Dnote is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dnote is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"net/http"
"github.com/dnote/dnote/pkg/server/database"
"github.com/dnote/dnote/pkg/server/job/digest"
"github.com/dnote/dnote/pkg/server/mailer"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/pkg/errors"
)
func weeklyDigestHandler(w http.ResponseWriter, r *http.Request) {
db := database.DBConn
var user database.User
if err := db.First(&user).Error; err != nil {
http.Error(w, errors.Wrap(err, "Failed to find user").Error(), http.StatusInternalServerError)
return
}
email, err := digest.Make(user, "sung@dnote.io")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body := email.Body
w.Write([]byte(body))
}
func emailVerificationHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Subject string
Token string
}{
"Verify your email",
"testToken",
}
email := mailer.NewEmail("noreply@dnote.io", []string{"sung@dnote.io"}, "Reset your password")
err := email.ParseTemplate(mailer.EmailTypeEmailVerification, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body := email.Body
w.Write([]byte(body))
}
func init() {
err := godotenv.Load(".env.dev")
if err != nil {
panic(err)
}
}
func main() {
database.InitDB()
defer database.CloseDB()
mailer.InitTemplates("./src")
log.Println("Email template debug server running on http://127.0.0.1:2300")
http.HandleFunc("/weekly-digest", weeklyDigestHandler)
http.HandleFunc("/email-verification", emailVerificationHandler)
log.Fatal(http.ListenAndServe(":2300", nil))
}