import (
	"apps/ch.4.4/nonce"
	"apps/ch.4.4/validator"
	"fmt"
	"html/template"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

const MiB_UNIT = 1 << 20

var t *template.Template
var submissions nonce.Nonces = nonce.New()

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
	err := t.ExecuteTemplate(w, "index", submissions.NewToken())
	checkError(err)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
	var errs validator.Errors
	r.ParseMultipartForm(32 * MiB_UNIT)
	token := r.Form.Get("token")
	if err := submissions.CheckThenMarkToken(token); err != nil {
func init() {
	submissions = nonce.New()
	t = template.Must(template.ParseFiles("profile.gtpl", "submission.gtpl"))
}