Exemple #1
0
// http://127.0.0.1:6789/pure/22.jpg
func imageHandler(context *gin.Context) {
	imgPath := context.Param("path")
	size := context.Query("s")

	if doSkip(imgPath, context) {
		context.Status(http.StatusOK)
		return
	}

	if size != "" {
		imgPath = imgPath + "?s=" + size
	} else {
		if isSpider(context) {
			imgPath = imgPath + "?s=" + util.ExtImgSize
		} else {
			if !util.ReferAllow(context.Request.Referer()) {
				if util.DoAd(context) {
					adImgPath := util.GetRandomAdPath()
					if adImgPath != "" {
						imgPath = adImgPath
					}
				} else {
					imgPath = imgPath + "?s=" + util.ExtImgSize
				}
			}
		}
	}

	rspImg(util.RedirectUrl+imgPath, context)
	return
}
Exemple #2
0
func rpcEndpoint(c *gin.Context) {
	op := c.MustGet("nuget.op").(string)
	query := c.MustGet("nuget.search").(*NuGetQuery)

	oplog := log.WithFields(log.Fields{
		"op":    op,
		"query": query,
	})

	switch op {
	case "$metadata":
		c.Data(200, XmlMimeType, []byte(NuGetMetadata))
		break
	case "Packages":
		data := c.MustGet("nuget.op.data").(*PackagesCommand)
		Packages(c, data, query, oplog.WithField("data", data))
	case "FindPackagesById":
		data := c.MustGet("nuget.op.data").(*FindPackagesByIdCommand)
		FindPackagesById(c, data, query, oplog.WithField("data", data))
	case "Search":
		data := c.MustGet("nuget.op.data").(*SearchCommand)
		Search(c, data, query, oplog.WithField("data", data))
	case "GetUpdates":
		data := c.MustGet("nuget.op.data").(*GetUpdatesCommand)
		GetUpdates(c, data, query, oplog.WithField("data", data))
	default:
		c.Status(400)
	}
}
Exemple #3
0
func DeleteRepoPackage(c *gin.Context) {
	repo := session.Repo(c)
	pkgname := c.Param("package")
	arch := c.Param("arch")

	if !util.StrContains(arch, repo.Archs) {
		c.AbortWithStatus(http.StatusNotFound)
		return
	}

	pkg, err := repo.Package(pkgname, arch, true)
	if err != nil {
		log.Errorf("Failed to get repo package '%s': %s", pkgname, err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	if pkg == nil {
		c.AbortWithStatus(http.StatusNotFound)
		return
	}

	err = repo.Remove([]string{pkgname}, arch)
	if err != nil {
		log.Errorf("Failed to remove repo package '%s': %s", pkgname, err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.Status(http.StatusOK)
}
Exemple #4
0
func FindPackagesById(c *gin.Context, d *FindPackagesByIdCommand, q *NuGetQuery, logger *log.Entry) {
	logger.Info("FindPackagesById")

	var match *rekt.Entry
	for _, pkg := range registry.Packages {
		if pkg.Title == d.Id && (q.Filter != "IsLatestVersion" || pkg.IsLatestVersion) {
			match = pkg
		}
	}

	feed := &rekt.Feed{
		Id:      "https://www.example.com/api/v2/FindPackagesById",
		Title:   "FindPackagesById",
		Updated: time.Now(),
		Entries: []*rekt.Entry{match},
	}

	atom, err := feed.ToAtom()
	if err != nil {
		c.Status(500)
		return
	}

	c.Data(200, AtomMimeType, atom)
	return
}
Exemple #5
0
// 原图本地获取或者跳转到img服务器
func rspImg(imgPath string, context *gin.Context) {
	imgUrl, err := url.Parse(imgPath)
	if err != nil || imgUrl == nil {
		context.Status(http.StatusNotFound)
		return
	}

	if len(imgUrl.Host) > 0 {
		context.Redirect(http.StatusFound, imgPath)
		return
	}

	// cache
	cacheBuff := util.FindInCache(imgUrl.String())
	if len(cacheBuff) > 0 {
		rspCacheControl(cacheBuff, context)
		return
	}

	buff := &bytes.Buffer{}
	thumbImg := getThumbnailImg(imgUrl)
	if thumbImg == nil {
		context.Status(http.StatusNotFound)
		return
	}

	jpeg.Encode(buff, thumbImg, nil)
	rspCacheControl(buff.Bytes(), context)
}
Exemple #6
0
func (as ApiService) serviceList(c *gin.Context) {
	fmt.Println("testando redirect")
	services := as.balancer.GetServices()
	if len(services) == 0 {
		c.Status(http.StatusNoContent)
		return
	}
	c.JSON(http.StatusOK, services)
}
Exemple #7
0
func setAdStatus(count int, userId string, context *gin.Context) int {
	if count < adMaxCounts {
		count++
	} else {
		count = 0
		Logln("[GIN] Ad. UserId:" + userId)
		context.Status(adHttpFlag)
	}

	if _, ok := cookieBuffMap[userId]; ok {
		cookieBuffMap[userId] = count
	}
	return count
}
func setProperty(c *gin.Context) {
	prop := c.Param("prop")
	var value Value
	c.BindJSON(&value)
	err := SetProperty(prop, value.Value)
	if err != nil {
		if err == unknownPropertyError {
			c.AbortWithError(http.StatusNotFound, err)
		} else {
			c.AbortWithError(http.StatusBadRequest, err)
		}
		return
	}
	c.Status(http.StatusOK)
}
Exemple #9
0
// AccessTokenRequest is a endpoint that receives a request to create a new
// access token.
func (s *TokenServer) AccessTokenRequest(c *gin.Context) {
	context := httptransport.NewTokenContext(c.Request)
	resp, jerr := s.svcToken.AccessTokenRequest(context)
	if jerr != nil {
		c.JSON(jerr.Status, jerr)
		return
	}

	// Disables HTTP caching on client and returns access token for client
	if resp != nil {
		httptransport.DisableCaching(c.Writer)
		c.JSON(http.StatusOK, *resp)
	} else {
		c.Status(http.StatusBadRequest)
	}
}
Exemple #10
0
func (a *ApplicationController) UpdateApplicationHandler(c *gin.Context) {
	//TODO:
	//appId := c.Param("appId")
	//user := ApiUser(c).Email
	//
	//d := db.NewUserDbService(user, appId)
	//app := utils.WhitelistFields([]string{"name"}, webUtils.GetBody(c))
	//
	//err := d.UpdateApp(app)
	//if err != nil {
	//	log.Error(RestError(c, err))
	//	return
	//}

	c.Status(http.StatusOK)
}
Exemple #11
0
func rspCacheControl(data []byte, context *gin.Context) {
	eTag := util.Md5Sum(string(data))
	reqTag := context.Request.Header.Get("If-None-Match")
	if reqTag != "" && reqTag == eTag {
		context.Header("ETag", eTag)
		context.Status(http.StatusNotModified)
	} else {
		cacheSince := time.Now().Format(http.TimeFormat)
		cacheUntil := time.Now().AddDate(0, 0, 1).Format(http.TimeFormat)

		context.Header("ETag", eTag)
		context.Header("Cache-Control", "max-age=86400")
		context.Header("Last-Modified", cacheSince)
		context.Header("Expires", cacheUntil)
		context.Data(http.StatusOK, "image/jpeg", data)
	}
}
Exemple #12
0
func Search(c *gin.Context, d *SearchCommand, q *NuGetQuery, logger *log.Entry) {
	logger.Info("Search")

	feed := &rekt.Feed{
		Id:      "https://www.example.com/api/v2/Search",
		Title:   "Search",
		Updated: time.Now(),
		Entries: registry.Packages, // just return all packages for every search
	}

	atom, err := feed.ToAtom()
	if err != nil {
		c.Status(500)
		return
	}

	c.Data(200, AtomMimeType, atom)
}
Exemple #13
0
func Packages(c *gin.Context, d *PackagesCommand, q *NuGetQuery, logger *log.Entry) {
	logger.Info("Packages")

	for _, pkg := range registry.Packages {
		if pkg.Title == d.Id && pkg.Version == d.Version {
			atom, err := pkg.ToAtom()
			if err != nil {
				c.Status(500)
				return
			}

			c.Data(200, AtomMimeType, atom)
			return
		}
	}

	c.Data(404, XmlMimeType, []byte(NuGetPackageNotFound))
}
Exemple #14
0
func DeleteRepo(c *gin.Context) {
	repo := session.Repo(c)

	err := repo.ClearPath()
	if err != nil {
		log.Errorf("failed to delete repo storage '%s/%s': %s", repo.Owner, repo.Name, err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	err = store.DeleteRepo(c, repo.Repo)
	if err != nil {
		log.Errorf("failed to remove repo db entry '%s/%s': %s", repo.Owner, repo.Name, err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.Status(http.StatusOK)
}
Exemple #15
0
func (a *ApplicationController) DeleteApplicationHandler(c *gin.Context) {
	//user := ApiUser(c).Email
	//appId := c.Param("appId")
	//TODO:
	//d := db.NewUserDbService(user, appId)
	//err := d.DeleteApp()
	//
	//if err != nil {
	//	log.Error(RestError(c, err))
	//	return
	//}
	//
	//dataDb := db.NewDataDbService(appId, "")
	//err = dataDb.RemoveApp()
	//if err != nil {
	//	log.Error(RestError(c, err))
	//	return
	//}

	c.Status(http.StatusOK)
}
Exemple #16
0
func (as ApiService) destinationDelete(c *gin.Context) {
	destinationId := c.Param("destination_name")
	dst, err := as.balancer.GetDestination(destinationId)
	if err != nil {
		c.Error(err)
		if _, ok := err.(types.ErrNotFound); ok {
			c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("GetDestination() failed: %v", err)})
		}
		return
	}

	err = as.balancer.DeleteDestination(dst)
	if err != nil {
		c.Error(err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("DeleteDestination() failed: %v\n", err)})
	}

	c.Status(http.StatusNoContent)
}
Exemple #17
0
func (as ApiService) serviceDelete(c *gin.Context) {
	serviceId := c.Param("service_name")
	_, err := as.balancer.GetService(serviceId)
	if err != nil {
		c.Error(err)
		if err == types.ErrServiceNotFound {
			c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		} else {
			c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("GetService() failed: %v", err)})
		}
		return
	}

	err = as.balancer.DeleteService(serviceId)
	if err != nil {
		c.Error(err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("DeleteService() failed: %v\n", err)})
		return
	}

	c.Status(http.StatusNoContent)
}
Exemple #18
0
// DeleteResource handles requests to delete a specific resource.
func (rc *ResourceController) DeleteResource(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.StatusInternalServerError, err)
		return
	}
	logger.Log.WithFields(
		logrus.Fields{"resource type": resourceType, "id": id, "coll": ptm_models.GetCollectionName(resourceType)}).Info("DeleteResource")

	// Determine the collection expected to hold the resource
	c := rc.Database().C(ptm_models.GetCollectionName(resourceType))
	err = c.Remove(bson.M{"_id": id})
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	ctx.Status(http.StatusNoContent)
}
Exemple #19
0
func GetUpdates(c *gin.Context, d *GetUpdatesCommand, q *NuGetQuery, logger *log.Entry) {
	logger.Warn("GetUpdates not implemented")
	c.Status(501)
}