當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.T方法代碼示例

本文整理匯總了Golang中github.com/gotk/ctx.Context.T方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.T方法的具體用法?Golang Context.T怎麽用?Golang Context.T使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gotk/ctx.Context的用法示例。


在下文中一共展示了Context.T方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: POST

func (lc *Collection) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form = &struct {
		Name       string `json:"name"`
		StaticURL  string `json:"staticURL"`
		StaticPath string `json:"staticPath"`
	}{}
	err := json.NewDecoder(req.Body).Decode(form)
	if err != nil {
		log.Errorf("Could not parse request data: %s", err)
		return ctx.BadRequest(rw, c.T("location.api.could_not_parse_request_data"))
	}

	// create new location
	loc := &Model{
		Name:       form.Name,
		StaticURL:  form.StaticURL,
		StaticPath: form.StaticPath,
	}
	err = db.Create(loc)
	if err != nil {
		log.Errorf("Could not create location %s: %v", form.Name, err)
		return ctx.BadRequest(rw, c.T("location.api.could_not_create_location"))
	}

	return ctx.Created(rw, loc)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:29,代碼來源:collection.go

示例2: POST

func (r *Resource) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	reader, err := req.MultipartReader()
	if err != nil {
		return ctx.BadRequest(rw, c.T("media.uploadresource.could_not_upload_file"))
	}
	var tempFile *os.File
	for {
		part, err := reader.NextPart()
		if err == io.EOF {
			break
		}

		tempFile, err = ioutil.TempFile(os.TempDir(), "spa")
		if err != nil {
			return ctx.InternalServerError(rw, c.T("media.uploadresource.could_not_create_temp_file"))
		}
		defer tempFile.Close()

		_, err = io.Copy(tempFile, part)
		if err != nil {
			break
		}
	}
	return ctx.Created(rw, tempFile.Name())
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:25,代碼來源:resource.go

示例3: Response

func Response(c *ctx.Context, rw http.ResponseWriter, token *jwt.Token) error {
	tokenString, err := ctx.SignToken(token)
	if err != nil {
		return ctx.InternalServerError(rw, c.T("user.token.problem_signing_token"))
	}
	return ctx.OK(rw, map[string]string{"token": tokenString})
}
開發者ID:thiennt,項目名稱:go-spa,代碼行數:7,代碼來源:helper.go

示例4: GET

func (r *Collection) GET(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)
	medias, err := db.FindAll(&Model{}, "")
	if err != nil {
		log.Errorf("Query error: %v", err)
		return ctx.BadRequest(rw, c.T("media.mediaresource.query_error"))
	}
	return ctx.OK(rw, medias)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:9,代碼來源:collection.go

示例5: GET

func (r *Resource) GET(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)
	vars := mux.Vars(req)
	id := vars["id"]

	media, err := db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query media id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("media.mediaitemresource.could_not_query_media"))
	}

	return ctx.OK(rw, media)
}
開發者ID:thiennt,項目名稱:go-spa,代碼行數:13,代碼來源:resource.go

示例6: GET

func (r *Resource) GET(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	vars := mux.Vars(req)
	id := vars["id"]

	db := c.Vars["db"].(*pg.Session)

	grp, err := db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query group id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("group.api.could_not_query_group"))
	}
	return ctx.OK(rw, grp)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:13,代碼來源:resource.go

示例7: POST

func (r *Begin) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		Email string `json:"email"`
	}
	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		return ctx.BadRequest(rw, c.T("reset.begin.could_not_query"))
	}

	// validate email address
	if ok := regexp.MustCompile(cfg.Email.Regex).MatchString(form.Email); !ok {
		return ctx.BadRequest(rw, c.T("reset.begin.invalid_email_address"))
	}

	// get user from database
	var u *user.Model
	u, err = user.GetByEmail(db, form.Email)
	if err != nil {
		return ctx.BadRequest(rw, c.T("reset.begin.user_not_found"))
	}

	go sendEmail(c, u)

	return ctx.OK(rw, c.T("reset.begin.email_sent"))
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:28,代碼來源:begin.go

示例8: POST

func (r *SignUp) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		FirstName     string `json:"firstName"`
		LastName      string `json:"lastName"`
		Email         string `json:"email"`
		Password      string `json:"password"`
		PasswordAgain string `json:"passwordAgain"`
	}
	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		log.Errorf("Could not parse request data: %s", err)
		return ctx.BadRequest(rw, c.T("user.signup.could_not_parse_request_data"))
	}

	// check whether the email address is already taken
	_, err = user.GetByEmail(db, form.Email)
	if err == nil {
		return ctx.BadRequest(rw, c.T("user.signup.email_taken"))
	} else if err != pg.ERecordNotFound {
		log.Errorf("Could not query user: %s", err)
		return ctx.InternalServerError(rw, c.T("user.signup.could_not_query_user"))
	}

	// password validation
	if form.Password != form.PasswordAgain {
		return ctx.BadRequest(rw, c.T("user.signup.passwords_mismatch"))
	}

	// create new user
	u, err := user.Create(
		db,
		form.Email,
		form.Password,
		&user.UserJsonData{
			FirstName: form.FirstName,
			LastName:  form.LastName,
		},
	)
	if err != nil {
		return ctx.InternalServerError(rw, c.T("user.signup.could_not_create_user"))
	}

	// return created user data
	return ctx.Created(rw, u)
}
開發者ID:thiennt,項目名稱:go-spa,代碼行數:48,代碼來源:signup.go

示例9: DELETE

func (r *Resource) DELETE(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)
	vars := mux.Vars(req)
	id := vars["id"]

	media, err := db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query media id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("media.mediaitemresource.could_not_query_media"))
	}
	err = db.Delete(media)
	if err != nil {
		log.Errorf("Could not delete media %s: %v", id, err)
		return ctx.InternalServerError(rw, c.T("media.mediaitemresource.could_not_delete_media"))
	}
	return ctx.NoContent(rw)
}
開發者ID:thiennt,項目名稱:go-spa,代碼行數:17,代碼來源:resource.go

示例10: DELETE

func (r *Resource) DELETE(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	vars := mux.Vars(req)
	id := vars["id"]

	db := c.Vars["db"].(*pg.Session)

	grp, err := db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query group id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("group.api.could_not_query_group"))
	}
	err = db.Delete(grp)
	if err != nil {
		log.Errorf("Could not delete group %s: %v", id, err)
		return ctx.InternalServerError(rw, c.T("group.api.could_not_delete_group"))
	}
	return ctx.NoContent(rw)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:18,代碼來源:resource.go

示例11: GET

func (r *Profile) GET(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// get user id from current token
	userId, found := c.Token.Claims["uid"]
	if !found {
		return ctx.BadRequest(rw, c.T("user.me.could_not_extract"))
	}

	// query user data
	user, err := GetById(db, int64(userId.(float64)))
	if err != nil {
		log.Errorf("Could not query user: %v", err)
		return ctx.InternalServerError(rw, c.T("user.me.could_not_query"))
	}

	// return user data
	return ctx.OK(rw, user)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:19,代碼來源:profile.go

示例12: POST

func (r *ValidateKey) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		Key string `json:"key"`
	}

	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		return ctx.BadRequest(rw, c.T("reset.validate.unable_to_validate_key"))
	}

	resetToken, err := getToken(db, form.Key)
	if err != nil || !resetToken.Valid() {
		return ctx.BadRequest(rw, c.T("reset.validate.invalid_key"))
	}

	return ctx.OK(rw, ValidKey{resetToken.UserId, form.Key})
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:20,代碼來源:validatekey.go

示例13: POST

func (r *Renew) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// get user id from the current token
	userId, found := c.Token.Claims["uid"]
	if !found {
		return ctx.BadRequest(rw, c.T("user.token.could_not_extract"))
	}

	// check if user is still valid
	user, err := user.GetById(db, int64(userId.(float64)))
	if err != nil {
		log.Errorf("Could not query user: %v", err)
		return ctx.InternalServerError(rw, c.T("user.token.could_not_query"))
	}

	// generate new token
	return Response(c, rw, New(user))

}
開發者ID:thiennt,項目名稱:go-spa,代碼行數:20,代碼來源:resource.go

示例14: PUT

func (r *Resource) PUT(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	vars := mux.Vars(req)
	id := vars["id"]

	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form = &struct {
		Name string `json:"name"`
	}{}
	err := json.NewDecoder(req.Body).Decode(form)
	if err != nil {
		log.Errorf("Could not parse request data: %s", err)
		return ctx.BadRequest(rw, c.T("group.api.could_not_parse_request_data"))
	}

	// get group from database
	var grp pg.Entity
	grp, err = db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query group id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("group.api.could_not_query_group"))
	}

	// update the group
	grp.(*Model).Name = form.Name
	err = db.Update(grp)
	if err != nil {
		log.Errorf("Could not edit group %s: %v", form.Name, err)
		return ctx.BadRequest(rw, c.T("group.api.could_not_edit_group"))
	}

	return ctx.OK(rw, grp)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:34,代碼來源:resource.go

示例15: PUT

func (r *Resource) PUT(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	vars := mux.Vars(req)
	id := vars["id"]

	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form = &struct {
		Name string `json:"name"`
	}{}
	err := json.NewDecoder(req.Body).Decode(form)
	if err != nil {
		log.Errorf("Could not parse request data: %s", err)
		return ctx.BadRequest(rw, c.T("mediatype.api.could_not_parse_request"))
	}

	// get media type from database
	var entity pg.Entity
	entity, err = db.FindOne(&Model{}, "id = $1", id)
	if err != nil {
		log.Errorf("Could not query media type id %s: %v", id, err)
		return ctx.BadRequest(rw, c.T("mediatype.api.could_not_query_media_type"))
	}
	mediatype := entity.(*Model)

	// update the media type
	mediatype.Name = form.Name
	err = db.Update(mediatype)
	if err != nil {
		log.Errorf("Could not edit media type %s: %v", form.Name, err)
		return ctx.BadRequest(rw, c.T("mediatype.api.could_not_edit_media_type"))
	}

	return ctx.OK(rw, mediatype)
}
開發者ID:huuzkee-foundation,項目名稱:gospa,代碼行數:35,代碼來源:resource.go


注:本文中的github.com/gotk/ctx.Context.T方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。