コード例 #1
0
ファイル: pastebin.go プロジェクト: RossBabcock3/viewfunc2
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
ファイル: create.go プロジェクト: RossBabcock3/viewfunc1
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
ファイル: create.go プロジェクト: RossBabcock3/viewfunc1
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),
	})
}
コード例 #4
0
ファイル: testing.go プロジェクト: gernest/gopastebin3
func Testing(w http.ResponseWriter, r *http.Request) {
	publicKey, e := ioutil.ReadFile("static/demo.rsa.pub")
	if e != nil {
		fmt.Println("Failure to read public key: %v", e)
		os.Exit(1)
	}
	var user models.User
	cookie, err := r.Cookie("jwtoauth1")
	if err == nil {

		tokenString := cookie.Value
		token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
			return publicKey, nil
		})
		obj := token.Claims["User"]

		j, _ := json.Marshal(obj)
		json.Unmarshal(j, &user)
	} else {
		user = defaultUser
	}
	/*	cookie2 := http.Cookie{Name: "jwtoauth1",
			Value:   tokenString,
			Expires: time.Now(),
		}
	http.SetCookie(w, &cookie2) */ // Keep this code around for logging off
	templ, _ := template.ParseFiles("views/layout.tpl", "views/testing.tpl")
	templ.Execute(w, struct {
		Title        string
		User         models.User
		Languages    map[string]string
		Publicpastes []models.Paste
		Mypastes     []models.Paste
	}{Title: "Add A Paste",
		User:         user,
		Languages:    models.Languages,
		Publicpastes: models.PublicPastes(),
		Mypastes:     models.MyPastes(user.Id),
	})
}
コード例 #5
0
ファイル: paste.go プロジェクト: gernest/gopastebin3
func Show(w http.ResponseWriter, r *http.Request) {
	publicKey, e := ioutil.ReadFile("static/demo.rsa.pub")
	if e != nil {
		fmt.Println("Failure to read public key: %v", e)
		os.Exit(1)
	}
	var user models.User
	cookie, err := r.Cookie("jwtoauth1")
	if err == nil {
		tokenString := cookie.Value
		token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
			return publicKey, nil
		})
		obj := token.Claims["User"]

		j, _ := json.Marshal(obj)
		json.Unmarshal(j, &user)
	} else {
		user = defaultUser
	}
	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)
	log.Println("realId =", realId)
	session, q := mgo.Dial("localhost")
	if q != nil {
		panic(q)
	}
	collection := session.DB("gopastebin3-3").C("pastes")
	var result models.Paste
	e2 := collection.Find(bson.M{"id": realId}).One(&result)
	if e2 != nil {
		panic(e2)
	}

	prism := models.Languages[result.Language]
	t, err := template.ParseFiles("views/layout.tpl", "views/create.tpl")
	if err != nil {
		log.Fatalln("template parse error:", err)
	}
	t.Execute(w, struct {
		Title        string
		User         models.User
		Paste        models.Paste
		Prism        string
		Publicpastes []models.Paste
		Mypastes     []models.Paste
	}{Title: "Show Paste",
		User:         user,
		Paste:        result,
		Prism:        prism,
		Publicpastes: models.PublicPastes(),
		Mypastes:     models.MyPastes(user.Id),
	})

}
コード例 #6
0
ファイル: paste.go プロジェクト: gernest/gopastebin3
func Create(w http.ResponseWriter, r *http.Request) {
	funcmap := template.FuncMap{
		"PublicPastes": models.PublicPastes,
	}
	publicKey, e := ioutil.ReadFile("static/demo.rsa.pub")
	if e != nil {
		fmt.Println("Failure to read public key: %v", e)
		os.Exit(1)
	}
	var user models.User
	cookie, err := r.Cookie("jwtoauth1")
	if err == nil {
		tokenString := cookie.Value
		token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
			return publicKey, nil
		})
		obj := token.Claims["User"]

		j, _ := json.Marshal(obj)
		json.Unmarshal(j, &user)
	} else {
		user = defaultUser
	}
	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")
	collection.Insert(&paste)
	log.Println("New id:", paste.Id)
	log.Println("After insert, paste =", paste)
	t, err := template.ParseFiles("views/layout.tpl", "views/create.tpl")
	if err != nil {
		log.Fatalln("template parse error:", err)
	}
	t2 := t.Funcs(funcmap)
	t2.Execute(w, struct {
		Title        string
		User         models.User
		Paste        models.Paste
		Prism        string
		Publicpastes []models.Paste
		Mypastes     []models.Paste
	}{Title: "Verify Paste",
		User:         user,
		Paste:        paste,
		Prism:        prism,
		Publicpastes: models.PublicPastes(),
		Mypastes:     models.MyPastes(user.Id),
	})
}
コード例 #7
0
ファイル: google.go プロジェクト: RossBabcock3/viewfunc1
func Callback(w http.ResponseWriter, r *http.Request) {
	//Get the code from the response
	code := r.FormValue("code")

	t := &oauth.Transport{Config: &conf.OauthCfg}

	// Exchange the received code for a token
	t.Exchange(code)

	//now get user data based on the Transport which has the token
	resp, _ := t.Client().Get(profileInfoURL)
	// strip null byte off the tail end of the resoponse body
	buf := make([]byte, 1024)
	resp.Body.Read(buf)
	str := string(buf)
	str = strings.Trim(str, "\x00")
	b := []byte(str)
	// convert the body to a Google user account
	var account models.User
	err := json.Unmarshal(b, &account)
	if err != nil {
		fmt.Println("Unmarshal error:", err)
		os.Exit(1)
	}
	// convert the account data to a JSON web token
	var privateKey []byte
	privateKey, _ = ioutil.ReadFile("static/demo.rsa") // location of demo.rsa
	token := jwt.New(jwt.GetSigningMethod("RS256"))
	token.Claims["User"] = account
	tokenString, err := token.SignedString(privateKey)
	if err != nil {
		panic(err)
	}
	cookie := http.Cookie{Name: conf.CookieName,
		Value:   tokenString,
		Expires: time.Now().AddDate(0, 0, 1),
	}
	http.SetCookie(w, &cookie)
	view, q := template.New("layout.html").Funcs(models.TplFuncs).ParseFiles("views/layout.html", "views/pastebin.html")
	//view, q := template.ParseFiles("views/pastebin.html", "views/layout.html")
	if q != nil {
		fmt.Println(q)
		os.Exit(1)
	}
	if e := view.Execute(w, struct {
		Title         string
		User          models.User
		Languages     map[string]string
		PublicPastes  []models.Paste
		PrivatePastes []models.Paste
	}{
		Title:         "Golang Pastebin",
		User:          account,
		Languages:     models.Languages,
		PublicPastes:  models.PublicPastes(),
		PrivatePastes: models.PrivatePastes(account.Id),
	}); e != nil {
		fmt.Println(e)
		os.Exit(1)
	}
}