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


Golang db.Context類代碼示例

本文整理匯總了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)
}
開發者ID:grffn,項目名稱:movier,代碼行數:7,代碼來源:search.go

示例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, "")
}
開發者ID:grffn,項目名稱:movier,代碼行數:31,代碼來源:auth.go

示例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"})
	}
}
開發者ID:grffn,項目名稱:movier,代碼行數:27,代碼來源:auth.go

示例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)
}
開發者ID:grffn,項目名稱:movier,代碼行數:8,代碼來源:search.go

示例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)
}
開發者ID:grffn,項目名稱:movier,代碼行數:8,代碼來源:search.go

示例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)
}
開發者ID:grffn,項目名稱:movier,代碼行數:9,代碼來源:search.go

示例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)
}
開發者ID:grffn,項目名稱:movier,代碼行數:22,代碼來源:items.go


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