Exemplo n.º 1
0
func main() {
	goopt.Parse(nil)
	cmdArgs := make([]string, 0)
	for _, arg := range os.Args[1:] {
		if arg[0] != '-' {
			cmdArgs = append(cmdArgs, arg)
		}
	}
	fmt.Printf("cmd args: %v\n", cmdArgs)
	config, err := makeConf(*fMinFee, *fMinCoins, *fMaxWork)
	if err != nil {
		log.Fatal(err.Error())
	}

	config.AddPolicy(&conf.Policy{
		Selector: conf.PolicySelector{},
		Cmd:      conf.MAX_BALANCE,
		Args:     []interface{}{getPaymentValue("", *fMaxBalance)},
	})

	// TODO(ortutay): s/"store"/store.SERVICE_NAME/
	config.AddPolicy(&conf.Policy{
		Selector: conf.PolicySelector{Service: "store"},
		Cmd:      conf.STORE_DIR,
		Args:     []interface{}{*fStoreDir},
	})
	config.AddPolicy(&conf.Policy{
		Selector: conf.PolicySelector{Service: "store"},
		Cmd:      conf.STORE_MAX_SPACE,
		Args:     []interface{}{getSpace("", *fStoreMaxSpace)},
	})
	config.AddPolicy(&conf.Policy{
		Selector: conf.PolicySelector{Service: "store"},
		Cmd:      conf.STORE_GB_PRICE_PER_MO,
		Args:     []interface{}{getPaymentValue("", *fStoreGbPricePerMo)},
	})
	fmt.Printf("running with conf: %v\n", config)

	util.SetAppDir(*fAppDir)
	ocCred, err := cred.NewOcCredLoadOrCreate("")
	if err != nil {
		log.Fatal(err.Error())
	}

	// TODO(ortutay): really, this should be flags to the binary so that we don't
	// spend people's coins without explicit intent
	bConf, err := util.LoadBitcoindConf("")
	if err != nil {
		log.Fatal(err.Error())
	}

	addr := fmt.Sprintf(":%v", *fPort)

	// TODO(ortutay): configure which services to run from command line args
	calcService := calc.CalcService{Conf: config}
	paymentService := payment.PaymentService{Conf: config, BitcoindConf: bConf}
	storeService := store.StoreService{Conf: config}

	services := make(map[string]node.Handler)
	services[calc.SERVICE_NAME] = &calcService
	services[payment.SERVICE_NAME] = &paymentService
	services[store.SERVICE_NAME] = &storeService
	mux := node.ServiceMux{
		Services: services,
	}

	wakers := []node.PeriodicWaker{&storeService}

	s := node.Server{
		Cred: &cred.Cred{
			OcCred:  *ocCred,
			BtcConf: bConf,
			Coins:   []cred.BtcCred{},
		},
		BtcConf:        bConf,
		Conf:           config,
		Addr:           addr,
		Handler:        &mux,
		PeriodicWakers: wakers,
	}
	err = s.ListenAndServe()
	if err != nil {
		log.Fatal(err.Error())
	}
}
Exemplo n.º 2
0
func main() {
	goopt.Parse(nil)
	util.SetAppDir(*fAppDir)

	ocCred, err := cred.NewOcCredLoadOrCreate("")
	if err != nil {
		log.Fatal(err.Error())
	}

	bConf, err := util.LoadBitcoindConf("")
	if err != nil {
		log.Fatal(err.Error())
	}

	pvLower, err := msg.NewPaymentValueParseString(*fCoinsLower)
	if err != nil {
		log.Fatal(err)
	}
	pvUpper, err := msg.NewPaymentValueParseString(*fCoinsUpper)
	if err != nil {
		log.Fatal(err)
	}
	coins, err := cred.GetBtcCredInRange(pvLower.Amount, pvUpper.Amount, bConf)
	if err != nil {
		log.Fatal(err.Error())
	}

	c := node.Client{
		BtcConf: bConf,
		Cred: cred.Cred{
			OcCred:  *ocCred,
			BtcConf: bConf,
			Coins:   *coins,
		},
	}

	var body []byte
	if !termutil.Isatty(os.Stdin.Fd()) {
		var err error
		body, err = ioutil.ReadAll(os.Stdin)
		util.Ferr(err)
	}

	cmdArgs := make([]string, 0)
	for _, arg := range os.Args[1:] {
		if arg[0] != '-' {
			cmdArgs = append(cmdArgs, arg)
		}
	}
	if len(cmdArgs) == 0 {
		// TODO(ortutay): print usage info
		return
	}

	var req *msg.OcReq
	switch cmdArgs[0] {
	case "quote":
		qReq, err := makeQuoteReq(cmdArgs[1:])
		if err != nil {
			log.Fatal(err.Error())
		}
		resp := sendRequest(&c, qReq)
		if resp.Status == msg.OK {
			var pv msg.PaymentValue
			err := json.Unmarshal(resp.Body, &pv)
			if err != nil {
				panic(err)
			}
			fmt.Printf("\n%v%v\n", util.S2B(pv.Amount), pv.Currency)
		}
	case "call":
		req, err = makeReq(cmdArgs[1:], body)
		if err != nil {
			log.Fatal(err.Error())
		}
		resp := sendRequest(&c, req)
		switch fmt.Sprintf("%v.%v", req.Service, req.Method) {
		case "payment.balance":
			{
				var br payment.BalanceResponse
				err := json.Unmarshal(resp.Body, &br)
				if err != nil {
					log.Fatalf("malformed response")
				}
				fmt.Printf("\nServer reports balance of %v%v (max allowed is %v%v)\n",
					util.S2B(br.Balance.Amount), br.Balance.Currency,
					util.S2B(br.MaxBalance.Amount), br.MaxBalance.Currency)
			}
		}
	case "pay":
		payBtc(&c, cmdArgs)
	case "listrep":
		sel := rep.Record{}
		if len(cmdArgs) > 1 {
			selJson := cmdArgs[1]
			err := json.Unmarshal([]byte(selJson), &sel)
			if err != nil {
				log.Fatal(err.Error())
			}
			fmt.Printf("sel json: %v %v\n", selJson, sel)
		}
		err := rep.PrettyPrint(&sel)
		if err != nil {
			log.Fatal(err.Error())
		}
	default:
		fmt.Printf("unrecognized command: %v", cmdArgs)
	}
}