Example #1
0
// PostNode creates a *Node from the submitted form and queues it for
// addition.
func (*Api) PostNode(ctx *jas.Context) {
	if Db.ReadOnly {
		// If the database is readonly, set that as the error and
		// return.
		ctx.Error = ReadOnlyError
		return
	}

	// Initialize the node and retrieve fields.
	node := new(Node)

	ip := IP(net.ParseIP(ctx.RequireString("address")))
	if ip == nil {
		// If the address is invalid, return that error.
		ctx.Error = jas.NewRequestError("addressInvalid")
		return
	}
	node.Addr = ip
	node.Latitude = ctx.RequireFloat("latitude")
	node.Longitude = ctx.RequireFloat("longitude")
	node.OwnerName = ctx.RequireString("name")
	node.OwnerEmail = ctx.RequireString("email")
	status, _ := ctx.FindInt("status")
	node.Status = int(status)

	// TODO(DuoNoxSol): Authenticate/limit node registration.

	err := Db.AddNode(node)
	if err != nil {
		// If there was an error, log it and report the failure.
		ctx.Error = jas.NewInternalError(err)
		l.Err(err)
		return
	}
	ctx.Data = "successful"
	l.Infof("Node %q registered\n", ip)
}
Example #2
0
// RequireToken uses the finder to retrieve a value named "token", and
// panics with "tokenInvalid" if there is either no such value, or it
// is invalid or expired.
func RequireToken(ctx *jas.Context) {
	tokeni, err := ctx.FindInt("token")
	if err != nil || !CheckToken(ctx.RemoteAddr, uint32(tokeni)) {
		panic(jas.NewRequestError("tokenInvalid"))
	}
}