Пример #1
0
func Pastebin(w http.ResponseWriter, r *http.Request) {
	/*	TplFuncs := template.FuncMap{
			"Publicpastes":  models.PublicPastes,
			"Privatepastes": models.PrivatePastes,
		}
	*/
	user := models.GetUser(r)
	//	templ, _ := template.ParseFiles("views/layout.html", "views/create.tpl")
	templ, err := template.New("layout.html").Funcs(models.TplFuncs).ParseFiles("views/layout.html", "views/pastebin.html")
	if err != error(nil) {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	templ.Execute(w, struct {
		Title         string
		User          models.User
		Languages     map[string]string
		Publicpastes  []models.Paste
		Privatepastes []models.Paste
	}{
		Title:         "Add A Paste",
		User:          user,
		Languages:     models.Languages,
		Publicpastes:  models.PublicPastes(),
		Privatepastes: models.PrivatePastes(user.Id),
	})
}
Пример #2
0
func Create(w http.ResponseWriter, r *http.Request) {
	/*	TplFuncs := template.FuncMap{
			"Publicpastes":  models.PublicPastes,
			"Privatepastes": models.PrivatePastes,
		}
	*/
	user := models.GetUser(r)
	title := r.FormValue("title")
	content := r.FormValue("content")
	language := r.FormValue("language")
	isPublicString := r.FormValue("ispublic")
	var isPublic bool
	if isPublicString == "true" || user.Id == "0" {
		isPublic = true
	} else {
		isPublic = false
	}
	userId := user.Id
	paste := models.Paste{
		Id:       bson.NewObjectId(),
		UserId:   userId,
		Title:    title,
		Content:  content,
		Language: language,
		IsPublic: isPublic,
	}
	prism := models.Languages[language]
	//	session, _ := mgo.Dial("localhost")
	//	collection := session.DB("gopastebin3-3").C("pastes")
	session, collection, _ := conf.GetCollection(conf.PASTES)
	defer session.Close()
	collection.Insert(&paste)
	log.Println("New id:", paste.Id)
	log.Println("After insert, paste =", paste)
	//	t, err := template.ParseFiles("views/layout.html", "views/create.tpl")
	t, err := template.New("layout.html").Funcs(models.TplFuncs).ParseFiles("views/layout.html", "views/create.tpl")
	if err != nil {
		log.Fatalln("template parse error:", err)
	}
	//	t2 := t.Funcs(funcmap)
	t.Execute(w, struct {
		Title         string
		User          models.User
		Paste         models.Paste
		Prism         string
		PublicPastes  []models.Paste
		PrivatePastes []models.Paste
	}{Title: "Verify Paste",
		User:          user,
		Paste:         paste,
		Prism:         prism,
		PublicPastes:  models.PublicPastes(),
		PrivatePastes: models.PrivatePastes(user.Id),
	})
}
Пример #3
0
func Show(w http.ResponseWriter, r *http.Request) {
	/*	TplFuncs := template.FuncMap{
			"Publicpastes":  models.PublicPastes,
			"Privatepastes": models.PrivatePastes,
		}
	*/
	user := models.GetUser(r)
	url := r.URL.Path
	parts := strings.Split(url, "/")
	log.Println("parts[0]:", parts[0], "parts[1]:", parts[1], "parts[2]:", parts[2])
	rawId := parts[2]
	pasteId := strings.TrimLeft(rawId, "ObjectIdHex(")
	pasteId = strings.TrimRight(pasteId, ")")
	pasteId = strings.Trim(pasteId, "\"")
	realId := bson.ObjectIdHex(pasteId)
	session, collection, _ := conf.GetCollection(conf.PASTES)
	defer session.Close()
	var result models.Paste
	err := collection.Find(bson.M{"id": realId}).One(&result)
	if err != nil {
		panic(err)
	}

	prism := models.Languages[result.Language]
	//	t, err2 := template.ParseFiles("views/layout.html", "views/create.tpl")
	t, err2 := template.New("layout.html").Funcs(models.TplFuncs).ParseFiles("views/layout.html", "views/create.tpl")
	if err2 != nil {
		log.Fatalln("template parse error:", err2)
	}
	t.Execute(w, struct {
		Title         string
		User          models.User
		Paste         models.Paste
		Prism         string
		PublicPastes  []models.Paste
		PrivatePastes []models.Paste
	}{Title: "Show Paste",
		User:          user,
		Paste:         result,
		Prism:         prism,
		PublicPastes:  models.PublicPastes(),
		PrivatePastes: models.PrivatePastes(user.Id),
	})
}
func getCurrentUser(r *http.Request, t *oauth.Transport) (*models.User, error) {
	// Make the request.
	request, err := t.Client().Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")

	if err != nil {
		return nil, err
	}

	if userInfo, err := ioutil.ReadAll(request.Body); err == nil {
		var gu models.GoogleUser

		if err := json.Unmarshal(userInfo, &gu); err == nil {
			// create new user if he doesn't already exist
			if currentUser, err = models.GetUser(r, gu.Id, gu.Email); err != nil {
				currentUser, err = models.NewUser(r, gu.Id, gu.Email, gu.Name, "google")
			}

			return currentUser, err
		}
	}

	return nil, err
}