示例#1
0
文件: server.go 项目: insionng/vodka
func login(c vodka.Context) error {
	username := c.FormValue("username")
	password := c.FormValue("password")

	if username == "jon" && password == "shhh!" {
		// Create token
		token := jwt.New(jwt.SigningMethodHS256)

		// Set claims
		claims := token.Claims.(jwt.MapClaims)
		claims["name"] = "Jon Snow"
		claims["admin"] = true
		claims["exp"] = time.Now().Add(time.Hour * 72).Unix()

		// Generate encoded token and send it as response.
		t, err := token.SignedString([]byte("secret"))
		if err != nil {
			return err
		}
		return c.JSON(http.StatusOK, map[string]string{
			"token": t,
		})
	}

	return vodka.ErrUnauthorized
}
示例#2
0
文件: server.go 项目: insionng/vodka
func login(c vodka.Context) error {
	username := c.FormValue("username")
	password := c.FormValue("password")

	if username == "jon" && password == "shhh!" {

		// Set custom claims
		claims := &jwtCustomClaims{
			"Jon Snow",
			true,
			jwt.StandardClaims{
				ExpiresAt: time.Now().Add(time.Hour * 72).Unix(),
			},
		}

		// Create token with claims
		token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

		// Generate encoded token and send it as response.
		t, err := token.SignedString([]byte("secret"))
		if err != nil {
			return err
		}
		return c.JSON(http.StatusOK, map[string]string{
			"token": t,
		})
	}

	return vodka.ErrUnauthorized
}
示例#3
0
// Render 渲染
func (r *Renderer) Render(w io.Writer, name string, ctx vodka.Context) error {
	template, err := r.getTemplate(name)
	if err != nil {
		return err
	}
	err = template.ExecuteWriter(ctx.GetStore(), w)
	return err
}
示例#4
0
文件: users.go 项目: insionng/vodka
func createUser(c vodka.Context) error {
	u := new(user)
	if err := c.Bind(u); err != nil {
		return err
	}
	users[u.ID] = *u
	return c.JSON(http.StatusCreated, u)
}
示例#5
0
func NodeEditGetHandler(self *vodka.Context) error {
	data := make(map[string]interface{})
	nid, _ := self.ParamInt64(":nid")
	nid_handler := models.GetNode(nid)
	data["inode"] = nid_handler
	data["icategory"] = models.GetCategory(nid_handler.Pid)

	return self.Render(http.StatusOK, "node_edit.html", data)
}
示例#6
0
func TopicEditGetHandler(self *vodka.Context) error {
	data := make(map[string]interface{})
	tid, _ := self.ParamInt64(":tid")
	tid_handler := models.GetTopic(tid)
	data["topic"] = tid_handler
	data["inode"] = models.GetNode(tid_handler.Nid)

	return self.Render(http.StatusOK, "topic_edit.html", data)
}
示例#7
0
文件: server.go 项目: hiproz/vodka
func upload(c *vodka.Context) error {
	mr, err := c.Request().MultipartReader()
	if err != nil {
		return err
	}

	// Read form field `name`
	part, err := mr.NextPart()
	if err != nil {
		return err
	}
	defer part.Close()
	b, err := ioutil.ReadAll(part)
	if err != nil {
		return err
	}
	name := string(b)

	// Read form field `email`
	part, err = mr.NextPart()
	if err != nil {
		return err
	}
	defer part.Close()
	b, err = ioutil.ReadAll(part)
	if err != nil {
		return err
	}
	email := string(b)

	// Read files
	i := 0
	for {
		part, err := mr.NextPart()
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}
		defer part.Close()

		file, err := os.Create(part.FileName())
		if err != nil {
			return err
		}
		defer file.Close()

		if _, err := io.Copy(file, part); err != nil {
			return err
		}
		i++
	}
	return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
		name, email, i)
}
func (ceh *customVodkaHandler) mustWrapHandleFunc(c vodka.Context) vodka.HandlerFunc {
	if _, ok := c.Request().(*standard.Request); ok {
		return standard.WrapHandler(ceh.httpHandler)
	} else if _, ok = c.Request().(*fasthttp.Request); ok {
		return NewFastHTTPVodkaAdaptor(ceh.httpHandler)
	}

	log.Fatal("Unknown HTTP implementation")
	return nil
}
示例#9
0
文件: app.go 项目: xqbumu/zenpress
func TokenHandler(self *vodka.Context) error {

	var claims = map[string]interface{}{}
	claims["username"] = "******"
	token, err := jwt.NewToken(key, claims)
	if err != nil {
		return err
	}
	// show the token use for test
	return self.String(http.StatusOK, "%s", token)
}
示例#10
0
文件: server.go 项目: insionng/vodka
func createUser(c vodka.Context) error {
	u := &user{
		ID: seq,
	}
	if err := c.Bind(u); err != nil {
		return err
	}
	users[u.ID] = u
	seq++
	return c.JSON(http.StatusCreated, u)
}
示例#11
0
func TokenHandler(self *vodka.Context) error {

	var claims = map[string]interface{}{}
	claims["token"] = "insion's-Token"
	token, err := NewToken(JWTContextKey, claims)
	if err != nil {
		return err
	}
	// show the token use for test
	return self.String(http.StatusOK, "%s", token)
}
示例#12
0
func SigninGetHandler(self *vodka.Context) error {

	session := sessions.Default(self)

	val := session.Get("user")
	if val != nil {
		return self.Redirect(302, "/")
	} else {
		return self.Render(http.StatusOK, "signin.html", nil)
	}

}
示例#13
0
文件: server.go 项目: insionng/vodka
func upload(c vodka.Context) error {
	// Read form fields
	name := c.FormValue("name")
	email := c.FormValue("email")

	//-----------
	// Read file
	//-----------

	// Source
	file, err := c.FormFile("file")
	if err != nil {
		return err
	}
	src, err := file.Open()
	if err != nil {
		return err
	}
	defer src.Close()

	// Destination
	dst, err := os.Create(file.Filename)
	if err != nil {
		return err
	}
	defer dst.Close()

	// Copy
	if _, err = io.Copy(dst, src); err != nil {
		return err
	}

	return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s uploaded successfully with fields name=%s and email=%s.</p>", file.Filename, name, email))
}
示例#14
0
文件: server.go 项目: hiproz/vodka
func upload(c *vodka.Context) error {
	req := c.Request()

	// req.ParseMultipartForm(16 << 20) // Max memory 16 MiB

	// Read form fields
	name := c.Form("name")
	email := c.Form("email")

	// Read files
	files := req.MultipartForm.File["files"]
	for _, f := range files {
		// Source file
		src, err := f.Open()
		if err != nil {
			return err
		}
		defer src.Close()

		// Destination file
		dst, err := os.Create(f.Filename)
		if err != nil {
			return err
		}
		defer dst.Close()

		if _, err = io.Copy(dst, src); err != nil {
			return err
		}
	}
	return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
		name, email, len(files))
}
示例#15
0
func ModifyCatPostHandler(self *vodka.Context) error {

	cid, _ := self.ParamInt64("categoryid")

	cat_title := self.ParamEscape("title")
	cat_content := self.ParamEscape("content")
	if cid != 0 && cat_title != "" && cat_content != "" {
		var cat models.Category
		cat.Id = int64(cid)
		cat.Title = cat_title
		cat.Content = cat_content
		cat.Created = time.Now()
		models.UpdateCategory(cat.Id, cat)
		return self.Redirect(302, fmt.Sprintf("/category/%v/", cid))
	} else {
		return self.Redirect(302, "/")
	}
}
示例#16
0
文件: server.go 项目: insionng/vodka
func stream(c vodka.Context) error {
	res := c.Response().(*standard.Response).ResponseWriter
	gone := res.(http.CloseNotifier).CloseNotify()
	res.Header().Set(vodka.HeaderContentType, vodka.MIMETextHTMLCharsetUTF8)
	res.WriteHeader(http.StatusOK)
	ticker := time.NewTicker(1 * time.Second)
	defer ticker.Stop()

	fmt.Fprint(res, "<pre><strong>Clock Stream</strong>\n\n<code>")
	for {
		fmt.Fprintf(res, "%v\n", time.Now())
		res.(http.Flusher).Flush()
		select {
		case <-ticker.C:
		case <-gone:
			break
		}
	}
}
示例#17
0
func NewTopicPostHandler(self *vodka.Context) error {
	nid, _ := self.ParamInt64("nodeid")
	cid := models.GetNode(nid).Pid
	session := sessions.Default(self)
	var user models.User
	val := session.Get("user")
	if val != nil {
		user = val.(models.User)
	}
	uid := user.Id
	author := user.Nickname

	tid_title := self.FormEscape("title")
	tid_content := self.FormEscape("content")

	if tid_title != "" && tid_content != "" {
		models.AddTopic(tid_title, tid_content, cid, nid, uid, author)
		return self.Redirect(302, fmt.Sprintf("/node/", nid))
	} else {
		return self.Redirect(302, "/")
	}
}
示例#18
0
文件: server.go 项目: insionng/vodka
func updateUser(c vodka.Context) error {
	u := new(user)
	if err := c.Bind(u); err != nil {
		return err
	}
	id, _ := strconv.Atoi(c.Param("id"))
	users[id].Name = u.Name
	return c.JSON(http.StatusOK, users[id])
}
示例#19
0
func NewCategoryPostHandler(self *vodka.Context) error {

	t := self.FormEscape("title")
	c := self.FormEscape("content")
	if t != "" && c != "" {
		models.AddCategory(t, c)
	}
	return self.Redirect(302, "/")
}
示例#20
0
func SignupPostHandler(self *vodka.Context) error {

	data := make(map[string]interface{})

	username := self.FormEscape("username")
	password := self.FormEscape("password")
	usererr := helper.CheckUsername(username)

	if usererr == false {
		e := errors.New("Username error, Please to again")
		data["UsernameErr"] = e.Error()
		return e
	}

	passerr := helper.CheckPassword(password)
	if passerr == false {
		e := errors.New("Password error, Please to again")
		data["PasswordErr"] = e.Error()
		return e
	}

	pwd := helper.EncryptHash(password, nil)

	//now := torgo.Date(time.Now(), "Y-m-d H:i:s")

	userInfo := models.CheckUserByNickname(username)

	fmt.Println(userInfo.Nickname)

	//  检查该用户是否已经被注册
	if userInfo.Nickname == "" {

		//注册用户
		regErr := models.AddUser(username+"@insion.co", username, "", pwd, 1)
		fmt.Println("reg:s")
		fmt.Println(regErr)
		fmt.Println("reg:e ")
		//注册成功设置session
		//	self.SetSession("userid", userInfo.Id)
		//	self.SetSession("username", userInfo.Nickname)
		//	self.SetSession("userrole", userInfo.Role)
		//	self.SetSession("useremail", userInfo.Email)
		return self.Redirect(302, "/signin/")

	} else {
		e := errors.New("User already exists")
		data["UsernameErr"] = e.Error()
		return e
	}
	return self.Render(http.StatusOK, "signup.html", data)
}
示例#21
0
文件: server.go 项目: insionng/vodka
func upload(c vodka.Context) error {
	// Read form fields
	name := c.FormValue("name")
	email := c.FormValue("email")

	//------------
	// Read files
	//------------

	// Multipart form
	form, err := c.MultipartForm()
	if err != nil {
		return err
	}
	files := form.File["files"]

	for _, file := range files {
		// Source
		src, err := file.Open()
		if err != nil {
			return err
		}
		defer src.Close()

		// Destination
		dst, err := os.Create(file.Filename)
		if err != nil {
			return err
		}
		defer dst.Close()

		// Copy
		if _, err = io.Copy(dst, src); err != nil {
			return err
		}

	}

	return c.HTML(http.StatusOK, fmt.Sprintf("<p>Uploaded successfully %d files with fields name=%s and email=%s.</p>", len(files), name, email))
}
示例#22
0
func NodeHandler(self *vodka.Context) error {
	data := make(map[string]interface{})
	page, _ := self.ParamInt64("page")
	nid, _ := self.ParamInt64(":nid")

	nid_handler := models.GetNode(nid)
	nid_handler.Views = nid_handler.Views + 1
	models.UpdateNode(nid, nid_handler)

	limit := 25
	rcs := len(models.GetAllTopicByNid(nid, 0, 0, 0, "hotness"))
	pages, pageout, beginnum, endnum, offset := helper.Pages(rcs, int(page), limit)
	data["pagesbar"] = helper.Pagesbar("", rcs, pages, pageout, beginnum, endnum, 1)
	data["nodeid"] = nid
	data["topics_hotness"] = models.GetAllTopicByNid(nid, offset, limit, 0, "hotness")
	data["topics_latest"] = models.GetAllTopicByNid(nid, offset, limit, 0, "id")

	if nid != 0 {
		return self.Render(http.StatusOK, "node.html", data)
	} else {
		return self.Redirect(302, "/")
	}

}
示例#23
0
func SearchHandler(self *vodka.Context) error {
	data := make(map[string]interface{})
	if keyword := self.FormEscape("keyword"); keyword != "" {
		page, _ := self.ParamInt("page")
		limit := 25

		rcs := len(*models.SearchTopic(keyword, 0, 0, "id"))
		pages, pageout, beginnum, endnum, offset := helper.Pages(rcs, int(page), limit)
		data["search_hotness"] = models.SearchTopic(keyword, offset, limit, "hotness")

		keywordz := "keyword=" + keyword + "&"
		data["pagesbar"] = helper.Pagesbar(keywordz, rcs, pages, pageout, beginnum, endnum, 1)

	}

	return self.Render(http.StatusOK, "search.html", data)
}
示例#24
0
func ViewHandler(self *vodka.Context) error {

	data := make(map[string]interface{})
	tid, _ := self.ParamInt64(":tid")
	tid_handler := models.GetTopic(tid)

	if tid_handler.Id > 0 {

		tid_handler.Views = tid_handler.Views + 1
		models.UpdateTopic(tid, tid_handler)

		data["article"] = tid_handler
		data["replys"] = models.GetReplyByPid(tid, 0, 0, "id")

		tps := models.GetAllTopicByCid(tid_handler.Cid, 0, 0, 0, "asc")

		if tps != nil && tid != 0 {

			for i, v := range tps {

				if v.Id == tid {
					prev := i - 1
					next := i + 1

					for i, v := range tps {
						if prev == i {
							data["previd"] = v.Id
							data["prev"] = v.Title
						}
						if next == i {
							data["nextid"] = v.Id
							data["next"] = v.Title
						}
					}
				}
			}
		}

		return self.Render(http.StatusOK, "view.html", data)
	}

	return self.Redirect(302, "/")

}
示例#25
0
func SigninPostHandler(self *vodka.Context) error {
	username := self.FormEscape("username")
	password := self.FormEscape("password")

	if username != "" && password != "" {

		if userInfo := models.GetUserByNickname(username); userInfo.Password != "" {

			if helper.ValidateHash(userInfo.Password, password) {
				session := sessions.Default(self)
				session.Set("user", userInfo)

				if e := session.Save(); e == nil {
					return self.Redirect(302, "/")
				}

			}
		}
	}
	return self.Redirect(302, "/signin/")
}
示例#26
0
func NodeEditPostHandler(self *vodka.Context) error {
	nid, _ := self.ParamInt64(":nid")
	cid, _ := self.ParamInt64("categoryid")

	session := sessions.Default(self)
	var user models.User
	val := session.Get("user")
	if val != nil {
		user = val.(models.User)
	}
	uid := user.Id

	nid_title := self.FormEscape("title")
	nid_content := self.FormEscape("content")
	if nid_title != "" && nid_content != "" {
		models.EditNode(nid, cid, uid, nid_title, nid_content)
		return self.Redirect(302, fmt.Sprintf("/node/%v/", nid))
	} else {
		return self.Redirect(302, "/")
	}
}
示例#27
0
文件: server.go 项目: insionng/vodka
func restricted(c vodka.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(jwt.MapClaims)
	name := claims["name"].(string)
	return c.String(http.StatusOK, "Welcome "+name+"!")
}
示例#28
0
文件: server.go 项目: insionng/vodka
func accessible(c vodka.Context) error {
	return c.String(http.StatusOK, "Accessible")
}
示例#29
0
func ModifyNodePostHandler(self *vodka.Context) error {

	cid, _ := self.ParamInt64("categoryid")
	nid, _ := self.ParamInt64("nodeid")

	nd_title := self.FormEscape("title")
	nd_content := self.FormEscape("content")
	if cid != 0 && nid != 0 && nd_title != "" && nd_content != "" {
		nd := new(models.Node)
		nd.Id = int64(nid)
		nd.Pid = int64(cid)
		nd.Title = nd_title
		nd.Content = nd_content
		nd.Created = time.Now()
		models.UpdateNode(nd.Id, nd)
		return self.Redirect(302, fmt.Sprintf("/node/%v/", nid))
	} else {
		return self.Redirect(302, "/")
	}
}
示例#30
0
func ModifyNodeGetHandler(self *vodka.Context) error {
	return self.Render(http.StatusOK, "modify_node.html", nil)
}