예제 #1
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),
	})
}
예제 #2
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),
	})
}
예제 #3
0
func PrivatePastes(id string) []Paste {
	/*	session, e := mgo.Dial("localhost")
		if e != nil {
			panic(e)
		}
		collection := session.DB("gopastebin3-3").C("pastes")
	*/
	session, collection, err := conf.GetCollection(conf.PASTES)
	defer session.Close()
	if err != error(nil) {
		panic(err)
	}
	var pastes []Paste
	err = collection.Find(bson.M{"userid": id, "ispublic": false}).All(&pastes)
	if err != error(nil) {
		panic(err)
	}
	return pastes
}
예제 #4
0
func PublicPastes() []Paste {
	/*	session, e := mgo.Dial("localhost")
		if e != nil {
			panic(e)
		}
		collection := session.DB("gopastebin3-3").C("pastes")
	*/
	session, collection, err := conf.GetCollection(conf.PASTES)
	defer session.Close()
	if err != nil {
		panic(err)
	}
	var pastes []Paste
	err = collection.Find(bson.M{"ispublic": true}).All(&pastes)
	if err != nil {
		panic(err)
	}
	return pastes
}
예제 #5
0
func init() {
	var index []mgo.Index

	index = append(index, mgo.Index{
		Key:        []string{"ispublic"},
		Unique:     false,
		DropDups:   false,
		Background: true,
		Sparse:     false,
	},
		mgo.Index{
			Key:        []string{"userid"},
			Unique:     false,
			DropDups:   false,
			Background: true,
			Sparse:     false,
		},
		mgo.Index{
			Key:        []string{"id"},
			Unique:     true,
			DropDups:   true,
			Background: true,
			Sparse:     true,
		})

	session, collection, err := conf.GetCollection(conf.PASTES)
	defer session.Close()
	if err != nil {
		panic(err)
	}

	for _, i := range index {
		err = collection.EnsureIndex(i)
		if err != nil {
			panic(err)
		}
	}
}