当前位置: 首页>>代码示例>>Golang>>正文


Golang Render.Text方法代码示例

本文整理汇总了Golang中github.com/martini-contrib/render.Render.Text方法的典型用法代码示例。如果您正苦于以下问题:Golang Render.Text方法的具体用法?Golang Render.Text怎么用?Golang Render.Text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/martini-contrib/render.Render的用法示例。


在下文中一共展示了Render.Text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: newAccountHandler

//Adds a new account to the db. Passwords are generated using bcrypt
func newAccountHandler(req *http.Request, db *gorm.DB, rendr render.Render) {
	username := req.FormValue("username")
	password := req.FormValue("password")

	if username == "" {
		log.Println(http.StatusBadRequest, "Username blank")
		rendr.Text(http.StatusBadRequest, "Supply valid username")
		return
	}
	if password == "" {
		log.Println(http.StatusBadRequest, "Password blank")
		rendr.Text(http.StatusBadRequest, "Supply valid password")
		return
	}

	hash, err := bcrypt.GenerateFromPassword([]byte(password), 0)
	if err != nil {
		log.Println(err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	acc := &account{Username: username, Password: hash, Funds: 0}
	err = db.Create(acc).Error
	if err != nil {
		log.Println(err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}
	rendr.Text(http.StatusOK, "Account created")
}
开发者ID:danihodovic,项目名称:lendico,代码行数:32,代码来源:routes.go

示例2: deleteAccountHandler

//Deletes a certain account from the db
func deleteAccountHandler(db *gorm.DB, acc account, rendr render.Render) {
	if err := db.Delete(&acc).Error; err != nil {
		rendr.Text(http.StatusInternalServerError, err.Error())
	} else {
		rendr.Text(http.StatusOK, "Account deleted")
	}
}
开发者ID:danihodovic,项目名称:lendico,代码行数:8,代码来源:routes.go

示例3: getAllUsersHandler

// Returns a json list of all the users
func getAllUsersHandler(db *gorm.DB, ren render.Render) {
	users := make([]User, 0, 0)
	if err := db.Find(&users).Error; err != nil {
		ren.Text(http.StatusInternalServerError, err.Error())
		return
	}
	ren.JSON(http.StatusOK, users)
}
开发者ID:danihodovic,项目名称:backend-test,代码行数:9,代码来源:routes.go

示例4: getUserHandler

//Gets a single user as specified in the `id` path param
func getUserHandler(db *gorm.DB, params martini.Params, ren render.Render) {
	u, err := findUserFromIDParam(db, params["id"])
	if err != nil {
		ren.Text(err.toMartiniRender())
		return
	}
	ren.JSON(http.StatusOK, u)
}
开发者ID:danihodovic,项目名称:backend-test,代码行数:9,代码来源:routes.go

示例5: authHandler

//Simple auth handler based on a global key. This may have to be rewritten...
func authHandler(r *http.Request, ctx martini.Context, ren render.Render) {
	log.Println(r.Header)
	if r.Header.Get("Authorization") != APIKey {
		ren.Text(http.StatusUnauthorized, "Invalid authorization")
		return
	}
	//Call the next handler
	ctx.Next()
}
开发者ID:danihodovic,项目名称:backend-test,代码行数:10,代码来源:routes.go

示例6: GetUsers

//Get /users
func (u *UserHandler) GetUsers(request *http.Request, rd render.Render, params martini.Params) {

	users, err := u.userService.GetUsers()
	if err != nil {
		rd.Text(500, err.Error())
		return
	}

	rd.JSON(200, users)
}
开发者ID:Arthurli,项目名称:RestfulDemo,代码行数:11,代码来源:user.go

示例7: transferHandler

//Transfer funds between two accounts. `from account` is retrieved from the context of the
//authorization handler. `to account` is retrieved from the request query parameters
func transferHandler(rendr render.Render, fromAcc account, req *http.Request, db *gorm.DB) {
	toAccountName := req.FormValue("to_account")
	amount, err := strconv.ParseFloat(req.FormValue("amount"), 64)
	if err != nil {
		log.Println(http.StatusBadRequest, err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	toAcc, err := findAccount(db, &account{Username: toAccountName})
	if err != nil {
		log.Println(http.StatusNotFound, "Other account not found")
		rendr.Text(http.StatusNotFound, "Other account not found")
		return
	}

	err = transferFunds(db, fromAcc.ID, toAcc.ID, amount)
	if err != nil {
		log.Println(err.Error())
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	rendr.Text(http.StatusOK, "Transaction successful")
}
开发者ID:danihodovic,项目名称:lendico,代码行数:27,代码来源:routes.go

示例8: GetAllRelations

//Get /users/:user_id/relationships
func (u *UserHandler) GetAllRelations(request *http.Request, rd render.Render, params martini.Params) {
	// 获取 url 参数
	userid, err := strconv.ParseInt(params["user_id"], 10, 64)
	if err != nil {
		rd.Text(400, "The request cannot be fulfilled due to bad syntax.")
		return
	}

	//验证 user 是否存在
	_, err = u.userService.GetUser(userid)
	if err != nil {
		if err == pg.ErrNoRows {
			rd.Text(400, fmt.Sprintln("user", userid, "not exist"))
		} else {
			rd.Text(500, err.Error())
		}
		return
	}

	rs, err := u.userService.GetUserRelations(userid)
	if err != nil {
		rd.Text(500, err.Error())
		return
	}

	relations := make([]interface{}, 0, 0)
	for _, r := range rs {
		relations = append(relations, fmtRelationShip(r))
	}

	rd.JSON(200, relations)
}
开发者ID:Arthurli,项目名称:RestfulDemo,代码行数:33,代码来源:user.go

示例9: deleteTodoHandler

func deleteTodoHandler(params martini.Params, rendr render.Render, db *gorm.DB) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		panic(err)
	}

	t, err := findTodo(db, &todo{ID: id})
	if err != nil {
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	db.Delete(&t)
	rendr.JSON(http.StatusOK, t)
}
开发者ID:danihodovic,项目名称:go-martini-gorm-angular-todo,代码行数:15,代码来源:server.go

示例10: addUserHandler

//Adds a user using POST data. Expects `Content-Type` to be `application/x-www-form-urlencoded`
func addUserHandler(db *gorm.DB, r *http.Request, ren render.Render) {
	name := r.FormValue("name")
	dob := r.FormValue("dob")
	address := r.FormValue("address")
	description := r.FormValue("description")

	toCheck := []string{name, dob, address, description}
	for _, key := range toCheck {
		if key == "" {
			ren.Text(http.StatusBadRequest, "Provide 'name', 'dob', 'address' and 'description' when creating a new user")
			return
		}
	}

	u := User{Name: name, Dob: dob, Address: address, Description: description, CreatedAt: time.Now()}
	db.Create(&u)
}
开发者ID:danihodovic,项目名称:backend-test,代码行数:18,代码来源:routes.go

示例11: newTodoHandler

func newTodoHandler(r *http.Request, rendr render.Render, db *gorm.DB) {
	todoText := r.FormValue("text")
	if todoText == "" {
		rendr.Text(http.StatusBadRequest, "Provide a `text` parameter")
		return
	}
	completedBool, err := strconv.ParseBool(r.FormValue("completed"))
	if err != nil {
		rendr.Text(http.StatusBadRequest, "Provide a `completed` paramter as a boolean")
		return
	}

	t := todo{Text: todoText, Completed: completedBool}
	db.Create(&t)

	rendr.JSON(http.StatusOK, t)
}
开发者ID:danihodovic,项目名称:go-martini-gorm-angular-todo,代码行数:17,代码来源:server.go

示例12: oneTodoHandler

func oneTodoHandler(params martini.Params, rendr render.Render, db *gorm.DB, r *http.Request) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		panic(err)
	}

	t, err := findTodo(db, &todo{ID: id})
	if err == gorm.RecordNotFound {
		rendr.Text(http.StatusNotFound, "Resource not found")
		return
	}
	if err != nil {
		rendr.Text(http.StatusInternalServerError, err.Error())
		return
	}

	rendr.JSON(http.StatusOK, t)
}
开发者ID:danihodovic,项目名称:go-martini-gorm-angular-todo,代码行数:18,代码来源:server.go

示例13: CreateUser

//Post /users
func (u *UserHandler) CreateUser(request *http.Request, rd render.Render, params martini.Params) {

	// 获取 json body 参数
	decoder := json.NewDecoder(request.Body)
	p := struct {
		Name string `json:"name"`
	}{}
	err := decoder.Decode(&p)
	if err != nil || len(p.Name) == 0 {
		rd.Text(400, "The request cannot be fulfilled due to bad syntax.")
		return
	}

	user, err := u.userService.CreateUser(p.Name)
	if err != nil {
		rd.Text(500, err.Error())
		return
	}

	rd.JSON(200, user)
}
开发者ID:Arthurli,项目名称:RestfulDemo,代码行数:22,代码来源:user.go

示例14: updateTodoHandler

func updateTodoHandler(r *http.Request, params martini.Params, rendr render.Render, db *gorm.DB) {
	id, err := strconv.Atoi(params["id"])
	if err != nil {
		rendr.Text(http.StatusBadRequest, "Provide a `completed` paramter as a boolean")
		return
	}

	t, err := findTodo(db, &todo{ID: id})
	if err != nil {
		rendr.Text(http.StatusBadRequest, err.Error())
		return
	}

	text := r.FormValue("text")
	if text != "" {
		t.Text = text
	}

	completed := r.FormValue("completed")
	if completed != "" {
		completedBool, err := strconv.ParseBool(completed)
		if err != nil {
			rendr.Text(http.StatusBadRequest, err.Error())
			return
		}
		t.Completed = completedBool
	}

	db.Save(t)
	rendr.JSON(http.StatusOK, t)
}
开发者ID:danihodovic,项目名称:go-martini-gorm-angular-todo,代码行数:31,代码来源:server.go

示例15: loginHandler

//Logs a user in. If successful returns the authorization token
//Note that since we don't have a logout handler, a token for a deleted account can be valid. This
//is partially migitated by the 5 minute exp mark.
func loginHandler(req *http.Request, db *gorm.DB, rendr render.Render) {
	username := req.FormValue("username")
	password := req.FormValue("password")

	acc, err := findAccount(db, &account{Username: username})
	if err != nil {
		rendr.Text(http.StatusNotFound, "Username not found")
		return
	}

	err = bcrypt.CompareHashAndPassword(acc.Password, []byte(password))
	if err != nil {
		rendr.Text(http.StatusUnauthorized, "Invalid password")
		return
	}

	token := jwt.New(signingMethod)
	token.Claims["exp"] = time.Now().Add(tokenExpires).Unix()
	token.Claims["id"] = acc.ID

	tokenStr, err := token.SignedString([]byte(secretKey))
	if err != nil {
		rendr.Text(http.StatusInternalServerError, err.Error())
		return
	}

	tokenJSON := make(map[string]string)
	tokenJSON["token"] = tokenStr

	rendr.JSON(http.StatusOK, tokenJSON)
}
开发者ID:danihodovic,项目名称:lendico,代码行数:34,代码来源:routes.go


注:本文中的github.com/martini-contrib/render.Render.Text方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。