示例#1
0
func view(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	q := datastore.NewQuery("Person").Order("BirthYear")

	html := ""

	iterator := q.Run(ctx)
	for {
		var person Person
		_, err := iterator.Next(&person)
		if err == datastore.Done {
			break
		} else if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		html += person.Name + " - " + person.BirthYear + "\n"
	}

	tpl, err := template.ParseFiles("view.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "view.html", html)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
示例#2
0
func CreateUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)

	// Form validation skeleton
	//
	//	var PassVerify = req.FormValue("passWordVerify")
	//	var Password = req.FormValue("passWord")
	//
	//	if(PassVerify != Password){
	//
	//	}

	var password, _ = bcrypt.GenerateFromPassword([]byte(req.FormValue("passWord")), bcrypt.DefaultCost)
	user := User{
		FirstName: req.FormValue("firstName"),
		LastName:  req.FormValue("lastName"),
		UserName:  req.FormValue("userName"),
		Email:     req.FormValue("email"),
		Password:  string(password),
	}

	key := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
	key, err := datastore.Put(ctx, key, &user)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	var sd SessionData
	sd.LoggedIn = true

	tpl.ExecuteTemplate(res, "homePage.html", &sd)
}
示例#3
0
func handleIndex(res http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		name := req.FormValue("name")
		year := req.FormValue("birthYear")

		ctx := appengine.NewContext(req)
		key := datastore.NewKey(ctx, "Person", name, 0, nil)

		person := &Person{
			Name:      name,
			BirthYear: year,
		}

		_, err := datastore.Put(ctx, key, person)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
	}
	tpl, err := template.ParseFiles("home.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "home.html", nil)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
示例#4
0
func sendFunc(res http.ResponseWriter, req *http.Request) {
	var emails []string
	if req.Method == "POST" {
		ctx := appengine.NewContext(req)
		q := datastore.NewQuery("Email").Order("Name")

		iterator := q.Run(ctx)
		for {
			var person Email
			_, err := iterator.Next(&person)
			if err == datastore.Done {
				break
			} else if err != nil {
				http.Error(res, err.Error(), 500)
				return
			}
			emails = append(emails, person.EmailAddr)
		}
	}

	tpl, err := template.ParseFiles("saved.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "saved.html", emails)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
示例#5
0
func mainFunc(res http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		addr := req.FormValue("email")
		name := req.FormValue("name")

		c := appengine.NewContext(req)
		key := datastore.NewKey(c, "Email", addr, 0, nil)

		person := &Email{
			Name:      name,
			EmailAddr: addr,
		}

		_, err := datastore.Put(c, key, person)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
	}

	tpl, err := template.ParseFiles("main.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "main.html", nil)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}
示例#6
0
func ExampleAppEngineTokenSource() {
	var req *http.Request // from the ServeHTTP handler
	ctx := appengine.NewContext(req)
	client := &http.Client{
		Transport: &oauth2.Transport{
			Source: google.AppEngineTokenSource(ctx, "https://www.googleapis.com/auth/bigquery"),
			Base: &urlfetch.Transport{
				Context: ctx,
			},
		},
	}
	client.Get("...")
}
示例#7
0
func UserLogin(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)
	var sd SessionData

	key := datastore.NewKey(ctx, "Users", req.FormValue("userName"), 0, nil)
	var user User
	err := datastore.Get(ctx, key, &user)
	if err != nil || bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.FormValue("passWord"))) != nil {
		sd.LogInFail = true
		tpl.ExecuteTemplate(res, "loginPage.html", sd)
		return
	} else {
		sd.LoggedIn = true
		sd.UserName = user.UserName
		tpl.ExecuteTemplate(res, "homePage.html", &sd)
	}
}
示例#8
0
func sentFunc(res http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		ctx := appengine.NewContext(req)

		q := datastore.NewQuery("Email").Order("Name")

		var emails []Email
		_, err := q.GetAll(ctx, &emails)
		if err != nil {
			err.Error()
		}

		tplt, err := template.ParseFiles("saved.html")
		if err != nil {
			log.Fatalln(err)
		}

		for _, email := range emails {
			name := email.Name
			email := email.EmailAddr

			msg := &mail.Message{
				Sender:   "neworganization.net Support <*****@*****.**>",
				To:       []string{email},
				Subject:  "Welcome to the club " + name + "!",
				HTMLBody: fmt.Sprint(tplt),
			}
			if err := mail.Send(ctx, msg); err != nil {
				ctx.Err()
			}
		}
	}
	tpl, err := template.ParseFiles("sent.html")
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(res, "sent.html", nil)
	if err != nil {
		http.Error(res, err.Error(), 500)
	}
}