func Start(pipe *xeth.XEth, config RpcConfig) error { if rpclistener != nil { if fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort) != rpclistener.Addr().String() { return fmt.Errorf("RPC service already running on %s ", rpclistener.Addr().String()) } return nil // RPC service already running on given host/port } l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) if err != nil { glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err) return err } rpclistener = l var handler http.Handler if len(config.CorsDomain) > 0 { var opts cors.Options opts.AllowedMethods = []string{"POST"} opts.AllowedOrigins = []string{config.CorsDomain} c := cors.New(opts) handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) } else { handler = newStoppableHandler(JSONRPC(pipe), l.stop) } go http.Serve(l, handler) return nil }
func StartHttp(cfg HttpConfig, codec codec.Codec, api shared.EthereumApi) error { if httpListener != nil { if fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort) != httpListener.Addr().String() { return fmt.Errorf("RPC service already running on %s ", httpListener.Addr().String()) } return nil // RPC service already running on given host/port } l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", cfg.ListenAddress, cfg.ListenPort)) if err != nil { glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", cfg.ListenAddress, cfg.ListenPort, err) return err } httpListener = l var handler http.Handler if len(cfg.CorsDomain) > 0 { var opts cors.Options opts.AllowedMethods = []string{"POST"} opts.AllowedOrigins = strings.Split(cfg.CorsDomain, " ") c := cors.New(opts) handler = newStoppableHandler(c.Handler(gethHttpHandler(codec, api)), l.stop) } else { handler = newStoppableHandler(gethHttpHandler(codec, api), l.stop) } go http.Serve(l, handler) return nil }
func (s *Server) corsHandler(h http.Handler) http.Handler { dirs := s.conf.GetDirectives() opts := cors.Options{} opts.AllowedOrigins = dirs.Server.CORSAccessControlAllowOrigin opts.AllowedMethods = dirs.Server.CORSAccessControlAllowMethods opts.AllowedHeaders = dirs.Server.CORSAccessControlAllowHeaders return cors.New(opts).Handler(h) }