本文整理匯總了Golang中github.com/gin-gonic/gin.Context.Abort方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Abort方法的具體用法?Golang Context.Abort怎麽用?Golang Context.Abort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gin-gonic/gin.Context
的用法示例。
在下文中一共展示了Context.Abort方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetResources
func (rc *ResourceController) GetResources(ctx *gin.Context) {
req := ctx.Request
resourceType := getResourceType(req.URL)
logger.Log.WithFields(
logrus.Fields{"resource type": resourceType}).Info("GetResources")
resources := ptm_models.NewSliceForResourceName(resourceType, 0, 0)
c := rc.Database().C(ptm_models.GetCollectionName(resourceType))
// retrieve all documents in the collection
// TODO Restrict this to resource type, just to be extra safe
query := buildSearchQuery(resourceType, ctx)
logger.Log.WithFields(
logrus.Fields{"query": query}).Info("GetResources")
err := c.Find(query).All(resources)
if err != nil {
if err == mgo.ErrNotFound {
ctx.String(http.StatusNotFound, "Not Found")
ctx.Abort()
return
} else {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
}
ctx.JSON(http.StatusOK, resources)
}
示例2: UserDelete
// UserDelete removes a specific user.
func UserDelete(c *gin.Context) {
record := session.User(c)
err := store.DeleteUser(
c,
record,
)
if err != nil {
logrus.Warnf("Failed to delete user. %s", err)
c.JSON(
http.StatusBadRequest,
gin.H{
"status": http.StatusBadRequest,
"message": "Failed to delete user",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
gin.H{
"status": http.StatusOK,
"message": "Successfully deleted user",
},
)
}
示例3: AuthRefresh
// AuthRefresh represents the refresh handler.
func AuthRefresh(c *gin.Context) {
record := session.Current(c)
token := token.New(token.SessToken, record.Username)
result, err := token.SignExpiring(record.Hash, config.Session.Expire)
if err != nil {
logrus.Warnf("Failed to refresh token. %s", err)
c.JSON(
http.StatusUnauthorized,
gin.H{
"status": http.StatusUnauthorized,
"message": "Failed to refresh token",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
result,
)
}
示例4: OrgTeamIndex
// OrgTeamIndex retrieves all teams related to a org.
func OrgTeamIndex(c *gin.Context) {
records, err := store.GetOrgTeams(
c,
&model.OrgTeamParams{
Org: c.Param("org"),
},
)
if err != nil {
logrus.Warnf("Failed to fetch org teams. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to fetch teams",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
records,
)
}
示例5: ProfileToken
// ProfileToken displays the users token.
func ProfileToken(c *gin.Context) {
record := session.Current(c)
token := token.New(token.UserToken, record.Username)
result, err := token.SignUnlimited(record.Hash)
if err != nil {
logrus.Warnf("Failed to generate token. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to generate token",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
result,
)
}
示例6: TeamUserIndex
// TeamUserIndex retrieves all users related to a team.
func TeamUserIndex(c *gin.Context) {
records, err := store.GetTeamUsers(
c,
&model.TeamUserParams{
Team: c.Param("team"),
},
)
if err != nil {
logrus.Warnf("Failed to fetch team users. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to fetch users",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
records,
)
}
示例7: UserIndex
// UserIndex retrieves all available users.
func UserIndex(c *gin.Context) {
records, err := store.GetUsers(
c,
)
if err != nil {
logrus.Warnf("Failed to fetch users. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to fetch users",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
records,
)
}
示例8: UserOrgIndex
// UserOrgIndex retrieves all orgs related to a user.
func UserOrgIndex(c *gin.Context) {
records, err := store.GetUserOrgs(
c,
&model.UserOrgParams{
User: c.Param("user"),
},
)
if err != nil {
logrus.Warnf("Failed to fetch user orgs. %s", err)
c.JSON(
http.StatusInternalServerError,
gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to fetch orgs",
},
)
c.Abort()
return
}
c.JSON(
http.StatusOK,
records,
)
}
示例9: checkSSL
func (p *policy) checkSSL(c *gin.Context) bool {
if !p.config.SSLRedirect {
return true
}
req := c.Request
isSSLRequest := strings.EqualFold(req.URL.Scheme, "https") || req.TLS != nil
if isSSLRequest {
return true
}
// TODO
// req.Host vs req.URL.Host
url := req.URL
url.Scheme = "https"
url.Host = req.Host
if len(p.config.SSLHost) > 0 {
url.Host = p.config.SSLHost
}
status := http.StatusMovedPermanently
if p.config.SSLTemporaryRedirect {
status = http.StatusTemporaryRedirect
}
c.Redirect(status, url.String())
c.Abort()
return false
}
示例10: GetResource
func (rc *ResourceController) GetResource(ctx *gin.Context) {
var id bson.ObjectId
req := ctx.Request
resourceType := getResourceType(req.URL)
// Validate id as a bson Object ID
id, err := toBsonObjectID(ctx.Param("id"))
if err != nil {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
logger.Log.WithFields(
logrus.Fields{"resource type": resourceType, "id": id}).Info("GetResource")
resource, err := rc.LoadResource(resourceType, id)
if err != nil {
if err == mgo.ErrNotFound {
ctx.String(http.StatusNotFound, "Not Found")
ctx.Abort()
return
} else {
ctx.AbortWithError(http.StatusBadRequest, err)
return
}
}
logger.Log.WithFields(logrus.Fields{"resource": resource}).Info("GetResource")
ctx.JSON(http.StatusOK, resource)
}
示例11: Handler
func (w *Whitelist) Handler(c *gin.Context) {
if w.interval > 0 {
w.mutex.RLock()
defer w.mutex.RUnlock()
}
if len(w.cidrs) == 0 {
return
}
remoteip := remoteIP(c)
if len(remoteip) == 0 {
return
}
netip := net.ParseIP(remoteip)
if netip == nil {
return
}
for _, cidr := range w.cidrs {
if cidr.Contains(netip) {
return
}
}
c.String(http.StatusInternalServerError, "Bad Host")
c.Abort()
}
示例12: RestError
func RestError(c *gin.Context, err interface{}) error {
restError := BuildError(err)
c.JSON(restError.Code, models.JSON{"error": restError.Message})
c.Abort()
return errors.New(restError.String())
}
示例13: Authenticate
func (this PlayersController) Authenticate(ginContext *gin.Context) {
_, err := models.Player{}.Find(this.Context.AeContext, this.Context.AccessToken)
if err.Any() {
this.RespondWith.Error(http.StatusForbidden, api.UserError("Invalid Access Token"))
ginContext.Abort()
}
}
示例14: RestrictInputContent
//Middleware for restricting input content
func RestrictInputContent(c *gin.Context) {
fmt.Println(c.Request.ContentLength)
if c.Request.ContentLength > myConstants.MaxInputContentLength {
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"eror": true, "message": myMessages.ContentTooLarge})
c.Abort()
return
}
}
示例15: respondWithError
func respondWithError(code int, message string, c *gin.Context) {
resp := map[string]string{"error": message}
if code == http.StatusForbidden {
go GlobalSessions.SessionDestroy(c.Writer, c.Request)
}
c.JSON(code, resp)
c.Abort()
}