本文整理汇总了Golang中github.com/gorilla/rpc/json.NewCodec函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCodec函数的具体用法?Golang NewCodec怎么用?Golang NewCodec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCodec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
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)
}
示例2: main
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)
}
示例3: main
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))
}
示例4: main
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)
}
示例5: main
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))
}
示例6: Initialize
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
}
示例7: Serve
//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
}
示例8: New
//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
}
示例9: New
//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
}
示例10: main
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))
}
示例11: New
//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
}
示例12: main
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)
}
}
示例13: main
func main() {
rpcServer := rpc.NewServer()
rpcServer.RegisterCodec(json.NewCodec(), "application/json")
rpcServer.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
sms := new(SmsService)
email := new(EmailService)
rpcServer.RegisterService(sms, "sms")
rpcServer.RegisterService(email, "email")
router := mux.NewRouter()
router.Handle("/delivery", rpcServer)
http.ListenAndServe(":1337", router)
}
示例14: main
func main() {
s := rpc.NewServer()
s.RegisterCodec(json.NewCodec(), "application/json")
s.RegisterService(new(StockService), "")
http.Handle("/stock", s)
http.ListenAndServe(":8080", nil)
}
示例15: main
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)
}