本文整理汇总了Golang中github.com/grffn/movier/db.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CategoriesHandler
func CategoriesHandler(context *gin.Context, database *db.Context) {
result, err := database.Categories()
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
}
context.JSON(http.StatusOK, result)
}
示例2: RegistrationHandler
//RegistrationHandler Hndler for register method
func RegistrationHandler(context *gin.Context, database *db.Context) {
model := models.RegisterModel{}
err := context.BindJSON(&model)
if err != nil {
context.AbortWithStatus(http.StatusBadRequest)
return
}
salt := make([]byte, 128)
_, err = rand.Read(salt)
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
return
}
var password []byte
password, err = util.GeneratePassword([]byte(model.Password), salt)
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
return
}
user := db.User{
ID: bson.NewObjectId(),
Username: model.Username,
Email: model.Email,
Password: base64.URLEncoding.EncodeToString(password),
Salt: base64.URLEncoding.EncodeToString(salt),
}
database.DB().C("users").Insert(user)
context.JSON(http.StatusOK, "")
}
示例3: LoginHandler
//LoginHandler Hndler for login method
func LoginHandler(context *gin.Context, database *db.Context) {
var model models.LoginModel
err := context.BindJSON(&model)
if err != nil {
log.Println(err)
context.AbortWithError(http.StatusBadRequest, err)
return
}
user := database.FindUser(model.UserID)
storedPassword, _ := base64.URLEncoding.DecodeString(user.Password)
salt, _ := base64.URLEncoding.DecodeString(user.Salt)
checkPassword, _ := util.GeneratePassword([]byte(model.Password), salt)
if bytes.Compare(storedPassword, checkPassword) == 0 {
token := jwt_lib.New(jwt_lib.GetSigningMethod("HS256"))
token.Claims["ID"] = user.Username
token.Claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
tokenString, err := token.SignedString([]byte(secret))
if err != nil {
context.JSON(500, gin.H{"message": "Could not generate token"})
return
}
context.JSON(200, gin.H{"token": tokenString})
} else {
context.JSON(http.StatusUnauthorized, gin.H{"status": "Login or password is incorrect"})
}
}
示例4: DocumentsHandler
func DocumentsHandler(context *gin.Context, database *db.Context) {
result, err := database.Documents()
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
return
}
context.JSON(http.StatusOK, result)
}
示例5: DocumentsByCategoryHandler
func DocumentsByCategoryHandler(context *gin.Context, database *db.Context) {
category := context.Param("category")
result, err := database.DocumentsByCategory(category)
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
}
context.JSON(http.StatusOK, result)
}
示例6: DocumentsByTagHandler
func DocumentsByTagHandler(context *gin.Context, database *db.Context) {
tag := context.Param("tag")
println(tag)
result, err := database.DocumentsByTag(tag)
if err != nil {
context.AbortWithError(http.StatusInternalServerError, err)
}
context.JSON(http.StatusOK, result)
}
示例7: CreateHandler
//CreateHandler Create new item
func CreateHandler(context *gin.Context, database *db.Context) {
model := models.DocModel{}
err := context.BindJSON(&model)
if err != nil {
context.AbortWithStatus(http.StatusBadRequest)
return
}
userID := context.MustGet("userId").(string)
user := database.FindUser(userID)
document := db.Document{
ID: bson.NewObjectId(),
UserID: user.ID,
Name: model.Name,
Tags: model.Tags,
Category: model.Category,
URL: model.URL,
MimeType: model.MimeType,
Authors: model.Authors,
}
database.NewDocument(document)
}