mail contact

This commit is contained in:
Ruidy Nemausat 2020-03-16 19:42:52 +01:00
parent 85af1381ef
commit 80c914238b
5 changed files with 17 additions and 27 deletions

View file

@ -16,21 +16,13 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
} }
func contactHandler(w http.ResponseWriter, r *http.Request) { func contactHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
views.Template(w, "contact", nil) views.Template(w, "contact", nil)
} return
}
func postContactHandler(w http.ResponseWriter, r *http.Request) { mail := services.NewMail(r.PostFormValue("email"), r.PostFormValue("message"))
err := r.ParseForm()
checkError(err, w) // bad error handling
mail := parseContactForm(r)
// mail.Send()
log.Println(mail) log.Println(mail)
views.Template(w, "contact_sent", nil) views.Template(w, "contact", struct{ Success bool }{true})
}
func parseContactForm(r *http.Request) services.Mail {
log.Println(r.PostForm)
return services.NewMail(r.PostFormValue("email"), r.PostFormValue("message"))
} }
func checkError(err error, w http.ResponseWriter) { func checkError(err error, w http.ResponseWriter) {

View file

@ -12,7 +12,6 @@ func Router() {
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/contact/", contactHandler) http.HandleFunc("/contact/", contactHandler)
http.HandleFunc("/contact/post/", postContactHandler)
http.HandleFunc("/", homeHandler) http.HandleFunc("/", homeHandler)
} }

View file

@ -5,11 +5,10 @@ import (
"net/http" "net/http"
"text/template" "text/template"
"github.com/rjNemo/go-wiki/models"
"github.com/rjNemo/go-wiki/settings" "github.com/rjNemo/go-wiki/settings"
) )
func Template(w http.ResponseWriter, tmpl string, p *models.Page) { func Template(w http.ResponseWriter, tmpl string, p interface{}) {
// err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p) // err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
t, err := template.ParseFiles(getTmplName("base"), getTmplName(tmpl)) t, err := template.ParseFiles(getTmplName("base"), getTmplName(tmpl))

View file

@ -1,7 +1,15 @@
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}} {{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
{{if.Success}}
<h1>Thanks for your mail</h1>
<div class="section">
<a class="btn btn-primary" href="/">Return to Home</a>
</div>
{{else}}
<h1>Contact us</h1> <h1>Contact us</h1>
<form action="/contact/post" method="POST"> <form method="POST">
<div class="form-group"> <div class="form-group">
<label for="email">Email</label> <label for="email">Email</label>
<input type="email" class="form-control" name="email" /> <input type="email" class="form-control" name="email" />
@ -14,4 +22,4 @@
<input type="submit" value="Save" class="btn btn-primary" /> <input type="submit" value="Save" class="btn btn-primary" />
</div> </div>
</form> </form>
{{end}} {{end}}{{end}}

View file

@ -1,8 +0,0 @@
{{define "title"}} Contact Us | Go-Wiki {{end}} {{define "content"}}
<h1>Thanks for your mail</h1>
<div class="section">
<a class="btn btn-primary" href="/">Return to Home</a>
</div>
{{end}}