Example #1
0
func main() {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(StockService), "")
	http.Handle("/stock", s)
	http.ListenAndServe(":8080", nil)
}
func main() {

	//Creating stock records
	var st = (new(StockRecords))

	//TradeID Initialization
	tradeId = 0

	router := mux.NewRouter()
	server := rpc.NewServer()
	server.RegisterCodec(json.NewCodec(), "application/json")
	server.RegisterService(st, "")

	chain := alice.New(
		func(h http.Handler) http.Handler {
			return handlers.CombinedLoggingHandler(os.Stdout, h)
		},
		handlers.CompressHandler,
		func(h http.Handler) http.Handler {
			return recovery.Handler(os.Stderr, h, true)
		})

	router.Handle("/rpc", chain.Then(server))
	log.Fatal(http.ListenAndServe(":1333", server))

}
Example #3
0
//Serve adds the rpc service t to the net/http DefaultServeMux at the given path
//and allows future lookup with name.
func Serve(t interface{}, name string, path string) {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(t, name)
	http.Handle(path, s)
	servers[name] = path
}
Example #4
0
File: main.go Project: Invacio/rex
func main() {
	flag.Parse()

	session, err := r.Connect(r.ConnectOpts{
		Address:  *rethinkdbAddress,
		Database: *rethinkdbDatabase,
	})
	if err != nil {
		log.Fatal(err)
	}

	r.DB(*rethinkdbDatabase).TableCreate("scripts").Exec(session)
	r.DB(*rethinkdbDatabase).TableCreate("tokens").Exec(session)

	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(&service.Service{
		Session: session,
	}, "Rexd")
	http.Handle("/rpc", s)

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("lavab/rexd 0.1.0\n"))
	})

	log.Printf("Binding to %s", *bindAddress)
	if err := http.ListenAndServe(*bindAddress, nil); err != nil {
		log.Fatal(err)
	}
}
Example #5
0
func main() {

	//initialize the stock account
	var st = (new(StockAccounts))

	//initialize a tradeId with random number
	tradeId = rand.Intn(10000) + 1

	// //register the stock account data and start server with HTTP protocol
	// rpc.Register(&st)
	// rpc.HandleHTTP()

	// //start listening
	// err := http.ListenAndServe(":1234", nil) //nil, no need for handler
	router := mux.NewRouter()
	server := rpc.NewServer()
	server.RegisterCodec(json.NewCodec(), "application/json")
	server.RegisterService(st, "")

	chain := alice.New(
		func(h http.Handler) http.Handler {
			return handlers.CombinedLoggingHandler(os.Stdout, h)
		},
		handlers.CompressHandler,
		func(h http.Handler) http.Handler {
			return recovery.Handler(os.Stderr, h, true)
		})

	router.Handle("/rpc", chain.Then(server))
	log.Fatal(http.ListenAndServe(":1234", server))

	// checkError(err)

}
Example #6
0
func TestService(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	s.RegisterService(new(Service1), "")

	var res Service1Response
	if _, err := execute(t, s, "Service1.Multiply", &Service1Request{4, 2}, &res); err != nil {
		t.Error("Expected err to be nil, but got:", err)
	}
	if res.Result != 8 {
		t.Errorf("Wrong response: %v.", res.Result)
	}
	if res.ErrorMessage != "" {
		t.Errorf("Expected error_message to be empty, but got:", res.ErrorMessage)
	}

	if code, err := execute(t, s, "Service1.ResponseError", &Service1Request{4, 2}, &res); err != nil || code != 500 {
		t.Errorf("Expected code to be 500 and error to be nil, but got", code, err)
	}
	if res.ErrorMessage == "" {
		t.Errorf("Expected error_message to be %q, but got %q", ErrResponseError, res.ErrorMessage)
	}

	if code, _ := execute(t, s, "Service1.Multiply", nil, &res); code != 400 {
		t.Errorf("Expected http response code 400, but got %v", code)
	}
}
Example #7
0
func Initialize(config *api.ApiConfig) error {

	e := localdb.Initialize(config.Log, config.LocalDB)

	if e != nil {
		return e
	}

	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	service := new(EMPService)
	service.Config = config
	s.RegisterService(service, "EMPService")

	// Register RPC Services
	http.Handle("/rpc", s)

	// Register JS Client
	http.Handle("/", http.FileServer(http.Dir(config.HttpRoot)))

	l, e := net.Listen("tcp", fmt.Sprintf(":%d", config.RPCPort))
	if e != nil {
		config.Log <- fmt.Sprintf("RPC Listen Error: %s", e)
		return e
	}

	go http.Serve(l, nil)

	go register(config)

	portStr := fmt.Sprintf(":%d", config.RPCPort)

	config.Log <- fmt.Sprintf("Started RPC Server on: %s", portStr)
	return nil
}
Example #8
0
//New returns a new Runner ready to be Announced and run tests on the
//heroku dyno grid.
func New(app, api string, tracker, hosted string) *Runner {
	n := &Runner{
		app:  app,
		api:  api,
		tcl:  client.New(tracker, http.DefaultClient, client.JsonCodec),
		base: hosted,
		rpc:  gorpc.NewServer(),
		rq:   rpc.NewRunnerQueue(),
		mc:   heroku.NewManaged(app, api, 2, 2*time.Minute),
		tm:   &runnerTaskMap{items: map[string]*runnerTask{}},
	}

	//register the run service in the rpc
	if err := n.rpc.RegisterService(n.rq, ""); err != nil {
		panic(err)
	}

	//register the pinger
	if err := n.rpc.RegisterService(pinger.Pinger{}, ""); err != nil {
		panic(err)
	}

	//register ourselves in the rpc
	if err := n.rpc.RegisterService(n, ""); err != nil {
		panic(err)
	}

	//register the codec
	n.rpc.RegisterCodec(json.NewCodec(), "application/json")

	//start processing
	go n.run()

	return n
}
Example #9
0
//New returns a new web Builder ready to Announce to the given tracker. It
//announces that it is available at `hosted` which should be the full url of
//where this builder resides on the internet.
func New(b builder.Builder, tracker, hosted string) *Builder {
	//create our new builder
	n := &Builder{
		b:    b,
		base: hosted,
		rpc:  gorpc.NewServer(),
		tcl:  client.New(tracker, http.DefaultClient, client.JsonCodec),
		bq:   rpc.NewBuilderQueue(),
		mux:  http.NewServeMux(),
		dler: newDownloader(),
	}

	//register the build service in the rpc
	if err := n.rpc.RegisterService(n.bq, ""); err != nil {
		panic(err)
	}

	//make sure we respond to pings
	if err := n.rpc.RegisterService(pinger.Pinger{}, ""); err != nil {
		panic(err)
	}

	//register the codec
	n.rpc.RegisterCodec(json.NewCodec(), "application/json")

	//add the handlers to our mux
	n.mux.Handle("/", n.rpc)
	n.mux.Handle("/download/", http.StripPrefix("/download/", n.dler))

	//start processing tasks
	go n.run()

	return n
}
Example #10
0
func TestServiceBeforeAfter(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	service := &Service1{}
	service.beforeAfterContext = map[string]string{}
	s.RegisterService(service, "")

	s.RegisterBeforeFunc(func(i *rpc.RequestInfo) {
		service.beforeAfterContext["before"] = "Before is true"
	})
	s.RegisterAfterFunc(func(i *rpc.RequestInfo) {
		service.beforeAfterContext["after"] = "After is true"
	})

	var res Service1Response
	if err := execute(t, s, "Service1.BeforeAfter", &Service1Request{}, &res); err != nil {
		t.Error("Expected err to be nil, but got:", err)
	}

	if res.Result != 1 {
		t.Errorf("Expected Result = 1, got %d", res.Result)
	}

	if afterValue, ok := service.beforeAfterContext["after"]; !ok || afterValue != "After is true" {
		t.Errorf("Expected after in context to be 'After is true', got %s", afterValue)
	}
}
func main() {

	//stock account Initialization
	var st = (new(StockAccounts))

	//Trade Id random generator
	tradeId = rand.Intn(99999) + 1

	//start listening
	router := mux.NewRouter()
	server := rpc.NewServer()
	server.RegisterCodec(json.NewCodec(), "application/json")
	server.RegisterService(st, "")

	chain := alice.New(
		func(h http.Handler) http.Handler {
			return handlers.CombinedLoggingHandler(os.Stdout, h)
		},
		handlers.CompressHandler,
		func(h http.Handler) http.Handler {
			return recovery.Handler(os.Stderr, h, true)
		})

	router.Handle("/rpc", chain.Then(server))
	log.Fatal(http.ListenAndServe(":8070", server))

}
Example #12
0
//New returns a new Runner ready to be Announced and run tests locally.
func New(runner, tracker, hosted string) *Runner {
	n := &Runner{
		tcl:    client.New(tracker, http.DefaultClient, client.JsonCodec),
		base:   hosted,
		runner: runner,
		rpc:    gorpc.NewServer(),
		rq:     rpc.NewRunnerQueue(),
		resp:   make(chan rpc.Output),
	}

	//register the run service in the rpc
	if err := n.rpc.RegisterService(n.rq, ""); err != nil {
		panic(err)
	}

	//register the pinger
	if err := n.rpc.RegisterService(pinger.Pinger{}, ""); err != nil {
		panic(err)
	}

	//register ourselves in the rpc
	if err := n.rpc.RegisterService(n, ""); err != nil {
		panic(err)
	}

	//register the codec
	n.rpc.RegisterCodec(json.NewCodec(), "application/json")

	//start processing
	go n.run()

	return n
}
Example #13
0
func TestFaults(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "text/xml")
	s.RegisterService(new(FaultTest), "")

	var err error

	var res1 FaultTestResponse
	err = execute(t, s, "FaultTest.Multiply", &FaultTestBadRequest{4, 2, 4}, &res1)
	if err == nil {
		t.Error("Expected err to be not nil, but got:", err)
	}
	if err.Error() != "Wrong number of arguments" {
		t.Errorf("Wrong response: %v.", err.Error())
	}

	var res2 FaultTestBadResponse
	err = execute(t, s, "FaultTest.Multiply", &FaultTestRequest{4, 2}, &res2)
	if err == nil {
		t.Error("Expected err to be not nil, but got:", err)
	}
	if !strings.HasPrefix(err.Error(), "Fields type mismatch") {
		t.Errorf("Wrong response: %v.", err.Error())
	}
}
Example #14
0
func main() {
	Portfolios = make(map[float32]Portfolio)

	// HTTP request multiplexer
	// mux.Router matches incoming requests against a list of registered routes
	// and calls a handler for the route that matches the URL
	r := mux.NewRouter()

	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")

	stockDealer := new(StockDealer)
	s.RegisterService(stockDealer, "")

	// middle ware: organizing  hared functionalities
	chain := alice.New(
		func(h http.Handler) http.Handler {
			return handlers.CombinedLoggingHandler(os.Stdout, h)
		},
		handlers.CompressHandler,
		func(h http.Handler) http.Handler {
			return recovery.Handler(os.Stderr, h, true)
		})

	r.Handle("/rpc", chain.Then(s))
	fmt.Println("Server listening on 8080")
	log.Fatal(http.ListenAndServe(":8080", r))
}
func main() {
	createHashTable()
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(StockMarket), "")
	http.Handle("/rpc", s)
	http.ListenAndServe("localhost:10000", nil)
}
Example #16
0
func main() {
	fmt.Println("preparing butler service")
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(butler.Butler), "")
	http.Handle("/rpc", s)
	http.ListenAndServe(":10080", nil)
}
Example #17
0
func jsonRpcService() *rpc.Server {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(KeyService), "")
	s.RegisterService(new(NameService), "")
	s.RegisterService(new(BlockService), "")
	return s
}
func main() {
	fmt.Println("Starting service")
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(StringService), "")
	http.Handle("/rpc", s)
	http.ListenAndServe(":10000", nil)
}
Example #19
0
func GetServer() *rpc.Server {
	s := rpc.NewServer()
	s.RegisterCodec(rpcJSON.NewCodec(), "application/json")

	for _, v := range rpcCtrlList {
		s.RegisterService(v, "")
	}
	return s
}
Example #20
0
func main() {
	server := rpc.NewServer()
	codec := json.NewCodec()
	server.RegisterCodec(codec, "application/json")
	server.RegisterCodec(codec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
	server.RegisterService(new(Service), "")
	http.Handle("/rpc", server)
	http.ListenAndServe("127.0.0.1:8080", nil)
}
Example #21
0
func main() {
	rpcHandler := rpc.NewServer()
	codec := json.NewCodec()
	rpcHandler.RegisterCodec(codec, "application/json")
	rpcHandler.RegisterCodec(codec, "application/json; charset=UTF-8")
	rpcHandler.RegisterService(new(Service), "")
	http.Handle("/rpc", rpcHandler)
	http.ListenAndServe("127.0.0.1:8080", nil)
}
Example #22
0
func main() {
	RPC := rpc.NewServer()
	xmlrpcCodec := xml.NewCodec()
	RPC.RegisterCodec(xmlrpcCodec, "text/xml")
	RPC.RegisterService(new(HelloService), "")
	http.Handle("/RPC2", RPC)

	log.Println("Starting XML-RPC server on localhost:1234/RPC2")
	log.Fatal(http.ListenAndServe(":1234", nil))
}
Example #23
0
func main() {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	//this will register the trading service use to trade for stocks and getting portfolio info
	s.RegisterService(new(TradingService), "TradingService")
	http.Handle("/rpc", s)
	//running it at port 8082
	fmt.Println("Starting server at port 8082")
	log.Fatal(http.ListenAndServe(":8082", nil))
}
func main() {
	jsonRPC := rpc.NewServer()
	jsonCodec := json.NewCodec()
	jsonRPC.RegisterCodec(jsonCodec, "application/json")
	jsonRPC.RegisterCodec(jsonCodec, "application/json; charset=UTF-8") // For firefox 11 and other browsers which append the charset=UTF-8
	jsonRPC.RegisterService(new(s.BuyingStocksService), "")
	jsonRPC.RegisterService(new(s.CheckingPortfolioService), "")
	http.Handle("/rpc", jsonRPC)
	http.ListenAndServe(":8080", jsonRPC)
}
Example #25
0
func main() {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
	arith := new(Arith)
	s.RegisterService(arith, "")
	r := mux.NewRouter()
	r.Handle("/rpc", s)
	http.ListenAndServe(":1234", r)
}
Example #26
0
func main() {
    s := rpc.NewServer()
    s.RegisterCodec(json.NewCodec(), "application/json")
    s.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
    config := new(Config)
    s.RegisterService(config, "")
    r := mux.NewRouter()
    r.Handle("/v1", s)
    http.ListenAndServe(":7000", r)
}
Example #27
0
func main() {
	fmt.Println("Starting HTTP server")
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(FinanceService), "")
	http.Handle("/rpc", s)
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
	log.Println("Starting JSON-RPC server on localhost:8080/rpc")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
Example #28
0
func JsonRpcService(srv *context.T, log log15.Logger) *rpc.Server {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(&KeyService{srv: srv, log: log}, "")
	s.RegisterService(&NameService{srv: srv, log: log}, "")
	s.RegisterService(&BlockService{srv: srv, log: log}, "")
	s.RegisterService(&TransactionService{srv: srv, log: log}, "")
	s.RegisterService(&RepositoryService{srv: srv, log: log}, "")
	s.RegisterService(&NetService{srv: srv, log: log}, "")
	return s
}
Example #29
0
func main() {
	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(HelloService), "")
	http.Handle("/rpc", s)

	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		log.Fatal(err)
	}
}
Example #30
0
func main() {
	r := mux.NewRouter()

	s := rpc.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")

	srv, _ := daemon.NewAstorService()
	s.RegisterService(srv, "")

	r.Handle("/rpc", s)
	log.Fatal(http.ListenAndServe(":8080", r))
}