small pixel drawing of a pufferfish nshc-form

main.go

package main

import (
	"html/template"
	"log"
	"net/http"
	"net/smtp"
	"os"
)

func main() {
	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/confirmation", confirmationHandler)

	log.Fatal(http.ListenAndServe(":8080", nil))
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		render(w, "templates/form.html", nil)
	case "POST":
		body := "> If you're a medic, did you or your medic buddy do something that made you uncomfortable?\r\n\r\n" + r.PostFormValue("question1") +
			"\r\n\r\n> Are there things you would like to see Northstar do differently to better care for, and support our communities?\r\n\r\n" + r.PostFormValue("question2") +
			"\r\n\r\n> Have you had an encounter with a medic that made you feel uncomfortable or disrespected?\r\n\r\n" + r.PostFormValue("question3") +
			"\r\n\r\n> Do you want us to follow up with a specific person re: accountability?\r\n\r\n" + r.PostFormValue("question4") +
			"\r\n\r\n> Are there any other comments you would like to share with us?\r\n\r\n" + r.PostFormValue("question5") +
			"\r\n\r\n> Would you like us to follow up with you? If yes, please leave your phone number or email here.\r\n\r\n" + r.PostFormValue("question6") +
			"\r\n\r\n> Anything else you'd like us to know or specific requests?\r\n\r\n" + r.PostFormValue("question7") +
			"\r\n\r\n\r\ndelivered securely by Cyberia"
		sendMessage(body)
		http.Redirect(w, r, "/confirmation", http.StatusSeeOther)
	}
}

func confirmationHandler(w http.ResponseWriter, r *http.Request) {
	render(w, "templates/confirmation.html", nil)
}

func render(w http.ResponseWriter, filename string, data interface{}) {
	tmpl, err := template.ParseFiles(filename)

	if err != nil {
		log.Println(err)
		http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
	}

	if err := tmpl.Execute(w, data); err != nil {
		log.Println(err)
		http.Error(w, "Sorry, something went wrong", http.StatusInternalServerError)
	}
}

func sendMessage(message string) {
	auth := smtp.PlainAuth("", os.Getenv("SMTP_FROM"), os.Getenv("SMTP_PASS"), os.Getenv("SMTP_SERVER"))

	to := []string{os.Getenv("SMTP_TO")}
	msg := []byte("From: " + os.Getenv("SMTP_FROM") + "\r\n" +
		"To: " + os.Getenv("SMTP_TO") + "\r\n" +
		"Content-Type: text/plain\r\n" +
		"Subject: Form completion\r\n" +
		"\r\n" +
		message + "\r\n")
	err := smtp.SendMail(os.Getenv("SMTP_SERVER")+":"+os.Getenv("SMTP_PORT"), auth, os.Getenv("SMTP_FROM"), to, msg)
	if err != nil {
		log.Fatal(err)
	}
}