Example #1
0
func ServeJSON(w http.ResponseWriter, code int, d ContentData) {
	b, err := json.Marshal(d)
	gonuts.PanicIfErr(err)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	_, err = w.Write(b)
	gonuts.PanicIfErr(err)
}
Example #2
0
func aboutHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	var content bytes.Buffer
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, "about.html", ""))

	bd := BaseData{
		Tabtitle: "About",
		Title:    "About",
		Content:  template.HTML(content.String()),
	}
	gonuts.PanicIfErr(Base.Execute(w, &bd))
}
Example #3
0
func nutsHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	d := make(ContentData)
	apiCall := r.Header.Get("Accept") == "application/json"

	// TODO: no need to load all, then render all - replace with chunking
	var nuts []gonuts.Nut
	var err error
	var title string
	vendor := r.URL.Query().Get(":vendor")
	q := r.URL.Query().Get("q")
	if vendor != "" {
		title = fmt.Sprintf("%s's Nuts", vendor)
		_, err = datastore.NewQuery("Nut").Filter("Vendor=", vendor).Order("Name").GetAll(c, &nuts)
	} else if q == "" {
		title = "All Nuts"
		_, err = datastore.NewQuery("Nut").Order("Vendor").Order("Name").GetAll(c, &nuts)
	} else {
		title = fmt.Sprintf("Search %q", q)
		res, err := gonuts.SearchIndex(c, q)
		gonuts.LogError(c, err)
		keys := make([]*datastore.Key, len(res))
		for i, pair := range res {
			keys[i] = gonuts.NutKey(c, pair[0], pair[1])
		}
		nuts = make([]gonuts.Nut, len(keys))
		err = datastore.GetMulti(c, keys, nuts)
	}
	gonuts.LogError(c, err)
	d["Nuts"] = nuts
	status := http.StatusOK
	if len(nuts) == 0 {
		status = http.StatusNotFound
	}

	if apiCall {
		d["Message"] = title
		ServeJSON(w, status, d)
		return
	}

	var content bytes.Buffer
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, "nuts.html", d))

	bd := BaseData{
		Tabtitle: title,
		Title:    title,
		Content:  template.HTML(content.String()),
	}

	w.WriteHeader(status)
	gonuts.PanicIfErr(Base.Execute(w, &bd))
}
Example #4
0
func docHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	var content bytes.Buffer

	section := r.URL.Query().Get(":section")
	file := "doc.html"
	if section != "" {
		file = fmt.Sprintf("doc_%s.html", section)
	}
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, file, ""))

	bd := BaseData{
		Tabtitle: "Documentation",
		Title:    "Documentation",
		Content:  template.HTML(content.String()),
	}
	gonuts.PanicIfErr(Base.Execute(w, &bd))
}
Example #5
0
func registerHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	u := gaeuser.Current(c)
	if u != nil && u.ID != "" {
		var err error
		vendor := r.FormValue("vendor")
		if !nutp.VendorRegexp.MatchString(vendor) {
			err = fmt.Errorf("Vendor name should match %s.", nutp.VendorRegexp.String())
			WriteError(w, http.StatusBadRequest, err)
			return
		}

		v := &gonuts.Vendor{Vendor: vendor}
		user := &gonuts.User{Id: u.ID, Email: u.Email, FederatedIdentity: u.FederatedIdentity}
		gonuts.PanicIfErr(user.GenerateToken())

		err = datastore.Get(c, gonuts.VendorKey(c, vendor), v)
		if err != datastore.ErrNoSuchEntity {
			if err == nil {
				err = fmt.Errorf("Vendor name %q is already registered.", vendor)
				WriteError(w, http.StatusForbidden, err)
				return
			}

			gonuts.LogError(c, err)
			WriteError(w, http.StatusInternalServerError, err)
			return
		}

		err = datastore.Get(c, gonuts.UserKey(c, u), user)
		if err != datastore.ErrNoSuchEntity {
			gonuts.LogError(c, err)
			WriteError(w, http.StatusInternalServerError, err)
			return
		}

		c.Infof("Adding user %s (%s) to vendor %s.", user.Id, user.Identifier(), v.Vendor)
		user.AddVendor(v)
		_, err = datastore.Put(c, gonuts.VendorKey(c, vendor), v)
		gonuts.PanicIfErr(err)
		_, err = datastore.Put(c, gonuts.UserKey(c, u), user)
		gonuts.PanicIfErr(err)
	}

	http.Redirect(w, r, "/-/me", http.StatusSeeOther)
}
Example #6
0
func myHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	d := make(ContentData)
	u := gaeuser.Current(c)

	if u == nil || u.ID == "" {
		url, err := gaeuser.LoginURL(c, "/-/me")
		gonuts.LogError(c, err)
		d["LoginURL"] = url
		d["OpenIDURL"] = "/-/me/openid"
	} else {
		user := new(gonuts.User)
		err := datastore.Get(c, gonuts.UserKey(c, u), user)
		if err == nil {
			url, err := gaeuser.LogoutURL(c, "/")
			gonuts.LogError(c, err)
			d["LogoutURL"] = url
			d["Identifier"] = user.Identifier()
			d["Token"] = user.Token
			d["GenerateURL"] = "/-/me/generate"
			d["Vendors"] = user.Vendors
		} else if err == datastore.ErrNoSuchEntity {
			user = &gonuts.User{Id: u.ID, Email: u.Email, FederatedIdentity: u.FederatedIdentity}
			url, err := gaeuser.LogoutURL(c, "/-/me")
			gonuts.LogError(c, err)
			d["LogoutURL"] = url
			d["Identifier"] = user.Identifier()
			d["RegisterURL"] = "/-/me/register"
		} else {
			panic(err)
		}
	}

	var content bytes.Buffer
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, "me.html", d))

	bd := BaseData{
		Tabtitle: "Me",
		Title:    "Me",
		Content:  template.HTML(content.String()),
	}
	gonuts.PanicIfErr(Base.Execute(w, &bd))
}
Example #7
0
func welcomeHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	d := make(ContentData)

	nuts, err := datastore.NewQuery("Version").Count(c)
	gonuts.LogError(c, err)
	d["VersionCount"] = nuts

	nuts, err = datastore.NewQuery("Nut").Count(c)
	gonuts.LogError(c, err)
	d["NutsCount"] = nuts

	users, err := datastore.NewQuery("User").Count(c)
	gonuts.LogError(c, err)
	d["UsersCount"] = users

	var content bytes.Buffer
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, "welcome.html", d))

	bd := BaseData{
		Content: template.HTML(content.String()),
	}
	gonuts.PanicIfErr(Base.Execute(w, &bd))
}
Example #8
0
func generateHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	u := gaeuser.Current(c)
	if u != nil && u.ID != "" {
		key := gonuts.UserKey(c, u)
		user := gonuts.User{}
		err := datastore.Get(c, key, &user)
		if err == nil {
			gonuts.PanicIfErr(user.GenerateToken())
			_, err = datastore.Put(c, key, &user)
		}
		gonuts.LogError(c, err)
	}
	http.Redirect(w, r, "/-/me", http.StatusSeeOther)
}
Example #9
0
func WriteError(w http.ResponseWriter, code int, err error) {
	w.WriteHeader(code)
	_, err = w.Write([]byte(fmt.Sprintf("%s", err)))
	gonuts.PanicIfErr(err)
}
Example #10
0
func nutShowHandler(c appengine.Context, w http.ResponseWriter, r *http.Request) {
	d := make(ContentData)
	getNut := r.Header.Get("Accept") == "application/zip"

	vendor := r.URL.Query().Get(":vendor")
	name := r.URL.Query().Get(":name")
	ver := r.URL.Query().Get(":version")

	if vendor == "" || !nutp.VendorRegexp.MatchString(vendor) || name == "" || (ver != "" && !nutp.VersionRegexp.MatchString(ver)) {
		err := fmt.Errorf("Invalid vendor %q, name %q or version %q.", vendor, name, ver)
		ServeJSONError(w, http.StatusBadRequest, err, d)
		return
	}

	current := new(gonuts.Version)
	var err error
	q := datastore.NewQuery("Version").Filter("Vendor=", vendor).Filter("Name=", name)
	if ver == "" {
		_, err = q.Order("-VersionNum").Limit(1).Run(c).Next(current)
	} else {
		key := gonuts.VersionKey(c, vendor, name, ver)
		err = datastore.Get(c, key, current)
	}
	gonuts.LogError(c, err)

	var title string
	if current.BlobKey != "" {
		// send nut file and exit
		if getNut {
			current.Downloads++
			key := gonuts.VersionKey(c, current.Vendor, current.Name, current.Version)
			_, err := datastore.Put(c, key, current)
			gonuts.LogError(c, err)
			blobstore.Send(w, current.BlobKey)
			return
		}

		// find all versions
		var all []gonuts.Version
		_, err = q.Order("-CreatedAt").GetAll(c, &all)
		gonuts.LogError(c, err)

		// prepare data for template
		cm := make(map[string]interface{})
		cm["Vendor"] = current.Vendor
		cm["Name"] = current.Name
		cm["Version"] = current.Version
		cm["Doc"] = current.Doc
		cm["Homepage"] = current.Homepage
		d["Current"] = cm
		d["All"] = all
		title = fmt.Sprintf("%s/%s %s", current.Vendor, current.Name, current.Version)
	} else {
		w.WriteHeader(http.StatusNotFound)
		if getNut {
			return
		}
		title = fmt.Sprintf("Nut %s/%s version %s not found", vendor, name, ver)
	}

	var content bytes.Buffer
	gonuts.PanicIfErr(Base.ExecuteTemplate(&content, "nut.html", d))

	bd := BaseData{
		Tabtitle: title,
		Title:    title,
		Content:  template.HTML(content.String()),
	}

	gonuts.PanicIfErr(Base.Execute(w, &bd))
}