本文整理匯總了Golang中github.com/gin-gonic/gin.Context.MustGet方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.MustGet方法的具體用法?Golang Context.MustGet怎麽用?Golang Context.MustGet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.MustGet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetTypeDataHandler
func (t *TypesController) GetTypeDataHandler(c *gin.Context) {
appId := c.Param("appId")
typeName := c.Param("typeName")
app := c.MustGet("app").(JSON)
types := app["types"].([]interface{})
found := false
for _, t := range types {
if value, ok := t.(string); ok && value == typeName {
found = true
break
}
}
if !found {
RestErrorNotFound(c)
return
}
d := db.NewTypeDbService(appId, typeName)
typeData, err := d.Find(nil, nil)
if err != nil {
RestError(c, err)
return
}
c.JSON(http.StatusOK, typeData)
}
示例2: EditReviewFormR
func EditReviewFormR(c *gin.Context) {
// dependency injection
var (
reviewStore = c.MustGet(ReviewStoreKey).(ReviewStore)
user = GetUser(c)
)
// only authenticated users can perform this action
if !user.Authenticated() {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
// fetch review
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
review, err := reviewStore.GetReview(id)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "review-form.html", gin.H{
"Action": "Edit",
"User": user,
"Form": review,
"Errors": map[string]string{},
})
}
示例3: DeleteUserInstance
func DeleteUserInstance(c *gin.Context) {
db := c.MustGet("db").(gorm.DB)
consumer := c.MustGet("consumer").(models.User)
id := c.Params.ByName("id")
var user models.User
db.First(&user, id)
if user.Username == "" {
response := make(map[string]string)
response["error"] = "Resource not found."
c.JSON(404, response)
} else {
if (consumer.Admin) || (user.Id == consumer.Id) {
db.Delete(&user)
if user.Id != consumer.Id {
if user.Gravatar == "" {
user.Gravatar = user.Email
}
user.Email = ""
}
user.Password = ""
c.JSON(200, user)
} else {
response := make(map[string]string)
response["error"] = "Invalid credentials."
c.JSON(401, response)
}
}
}
示例4: WhiskyListR
func WhiskyListR(c *gin.Context) {
// dependency injection
var (
whiskyStore = c.MustGet(WhiskyStoreKey).(WhiskyStore)
user = GetUser(c)
)
// get whiskies
list, err := whiskyStore.GetAllWhiskies()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// transform list into alphabet map
whiskies := map[string][]Whisky{}
for _, whisky := range list {
group := strings.ToUpper(whisky.Distillery[0:1])
whiskies[group] = append(whiskies[group], whisky)
}
c.HTML(http.StatusOK, "list.html", gin.H{
"User": user,
"Whiskies": whiskies,
})
}
示例5: UserListR
func UserListR(c *gin.Context) {
// dependency injection
var (
userStore = c.MustGet(UserStoreKey).(UserStore)
user = GetUser(c)
)
// get users
list, err := userStore.GetAllUser()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// transform list into alphabet map
users := map[string][]User{}
for _, user := range list {
group := strings.ToUpper(user.Name[0:1])
users[group] = append(users[group], user)
}
c.HTML(http.StatusOK, "users.html", gin.H{
"User": user,
"Users": users,
})
}
示例6: UpdateTemplate
func UpdateTemplate(c *gin.Context) {
oldTemplate := c.MustGet("template").(models.Template)
var template models.Template
if err := c.Bind(&template); err != nil {
return
}
if _, err := database.Mysql.Exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, playbook=?, arguments=?, override_args=? where id=?", template.SshKeyID, template.InventoryID, template.RepositoryID, template.EnvironmentID, template.Playbook, template.Arguments, template.OverrideArguments, oldTemplate.ID); err != nil {
panic(err)
}
desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
objType := "template"
if err := (models.Event{
ProjectID: &oldTemplate.ProjectID,
Description: &desc,
ObjectID: &oldTemplate.ID,
ObjectType: &objType,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}
示例7: EnvironmentMiddleware
func EnvironmentMiddleware(c *gin.Context) {
project := c.MustGet("project").(models.Project)
envID, err := util.GetIntParam("environment_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("*").
From("project__environment").
Where("project_id=?", project.ID).
Where("id=?", envID).
ToSql()
var env models.Environment
if err := database.Mysql.SelectOne(&env, query, args...); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("environment", env)
c.Next()
}
示例8: showNote
func (p *Engine) showNote(c *gin.Context) (interface{}, error) {
u := c.MustGet("user").(*platform.User)
id := c.Param("id")
var note Note
err := p.Db.Where("id = ? AND user_id = ?", id, u.ID).First(¬e).Error
return note, err
}
示例9: deleteNote
func (p *Engine) deleteNote(c *gin.Context) (interface{}, error) {
u := c.MustGet("user").(*platform.User)
id := c.Param("id")
err := p.Db.Where("id = ? AND user_id = ?", id, u.ID).Delete(Note{}).Error
return web.OK, err
}
示例10: AddUser
func AddUser(c *gin.Context) {
project := c.MustGet("project").(models.Project)
var user struct {
UserID int `json:"user_id" binding:"required"`
Admin bool `json:"admin"`
}
if err := c.Bind(&user); err != nil {
return
}
if _, err := database.Mysql.Exec("insert into project__user set user_id=?, project_id=?, admin=?", user.UserID, project.ID, user.Admin); err != nil {
panic(err)
}
objType := "user"
desc := "User ID " + strconv.Itoa(user.UserID) + " added to team"
if err := (models.Event{
ProjectID: &project.ID,
ObjectType: &objType,
ObjectID: &user.UserID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}
示例11: OpenWebsocket
// Status handler for the /status route
func OpenWebsocket(c *gin.Context) {
userId := c.MustGet("request_user_id").(string)
noteId := c.Param("note_id")
in, note, err := db.GetNoteById(noteId)
if log.Error(err) {
c.Error(errors.NewISE())
return
}
if !in {
c.Error(errors.NewHttp(http.StatusNotFound, "The requested note was not found"))
return
}
if userId != note.Owner {
c.Error(errors.NewHttp(http.StatusUnauthorized, "Only owners can open websockets into their notes"))
return
}
conn, err := websocketUpgrader.Upgrade(c.Writer, c.Request, nil)
if log.Error(err) {
c.Error(errors.NewISE())
return
}
log.Info("Opening ws for user %v on %v", userId, note.Id)
bundle := model.NewContext(userId, noteId)
WrapWebsocket(conn, bundle)
ws.ProcessMessages(bundle)
}
示例12: UserLogin
func UserLogin(c *gin.Context) {
var cookie middleware.CookieManager
cookie = c.MustGet("CM").(middleware.CookieManager)
if c.Request.Method == "GET" {
data := tool.GetTD(c)
fmt.Println(data)
c.HTML(http.StatusOK, "login.html", data)
} else if c.Request.Method == "POST" {
email := c.PostForm("email")
password := c.PostForm("password")
user := model.User{}
model.T.DB.Debug().Where("email = ? and password = ?", email, password).First(&user)
if user.Name != "" {
cookie.Add("user_id", user.ID)
cookie.WriteCookies()
c.Redirect(http.StatusMovedPermanently, "/")
} else {
cookie.Flash("fail_msg", "login failed :(")
c.Redirect(http.StatusMovedPermanently, "/user/login")
}
}
}
示例13: UserLogout
func UserLogout(c *gin.Context) {
var cookie middleware.CookieManager
cookie = c.MustGet("CM").(middleware.CookieManager)
cookie.Delete("user_id")
cookie.WriteCookies()
c.Redirect(http.StatusMovedPermanently, "/")
}
示例14: updateGoogleGet
func updateGoogleGet(c *gin.Context) {
db := c.MustGet("db").(*database.Database)
params := utils.ParseParams(c.Request)
usr := params.GetByName("user")
license := params.GetByName("license")
valid, err := user.CheckLicense(db, license)
if err != nil {
switch err.(type) {
case *database.NotFoundError:
c.AbortWithError(404, err)
default:
c.AbortWithError(500, err)
}
return
}
if !valid {
c.AbortWithError(401, err)
return
}
err = google.Update(db, usr)
if err != nil {
c.AbortWithError(500, err)
return
}
c.String(200, "")
}
示例15: InventoryMiddleware
func InventoryMiddleware(c *gin.Context) {
project := c.MustGet("project").(models.Project)
inventoryID, err := util.GetIntParam("inventory_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("*").
From("project__inventory").
Where("project_id=?", project.ID).
Where("id=?", inventoryID).
ToSql()
var inventory models.Inventory
if err := database.Mysql.SelectOne(&inventory, query, args...); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("inventory", inventory)
c.Next()
}