Ejemplo n.º 1
0
func api(api *gin.RouterGroup) {
	api.POST("/point", func(c *gin.Context) {
		p := point.Point{}
		err := c.Bind(&p)
		hash, err := hash.NewHashPoint(p)
		if err != nil || !p.IsValid() {
			c.JSON(http.StatusBadRequest, "Invalid point")
		} else {
			c.JSON(http.StatusOK, gin.H{
				"point":  p,
				"hash":   reverseString(hash.String),
				"zorder": reverseString(hash.GetZorder().HashString(32)),
			})
		}
	})

	api.POST("/hash", func(c *gin.Context) {
		hash := &hash.Hash{}
		err := c.Bind(&hash)
		hash.String = reverseString(hash.String)
		if err != nil || hash.InitFromString() != nil {
			c.JSON(http.StatusBadRequest, "Invalid hash")
		} else {
			c.JSON(http.StatusOK, gin.H{
				"hash":   reverseString(hash.String),
				"point":  hash.GenPoint(),
				"zorder": reverseString(hash.GetZorder().HashString(32)),
			})
		}
	})
}
Ejemplo n.º 2
0
func NewHashPoint(p point.Point) (Hash, error) {
	if !p.IsValid() {
		return Hash{}, errors.New("Invalid Point")
	}
	zorder := genZorder(p)
	hilbert := genHilbert(zorder)
	return Hash{hilbert: hilbert,
			zorder: zorder,
			String: hilbert.HashString(32)},
		nil
}