github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/http.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package simulation
    26  
    27  import (
    28  	"fmt"
    29  	"net/http"
    30  
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/p2p/simulations"
    33  )
    34  
    35  //
    36  var (
    37  	DefaultHTTPSimAddr = ":8888"
    38  )
    39  
    40  //
    41  //
    42  func (s *Simulation) WithServer(addr string) *Simulation {
    43  //
    44  	if addr == "" {
    45  		addr = DefaultHTTPSimAddr
    46  	}
    47  	log.Info(fmt.Sprintf("Initializing simulation server on %s...", addr))
    48  //
    49  	s.handler = simulations.NewServer(s.Net)
    50  	s.runC = make(chan struct{})
    51  //
    52  	s.addSimulationRoutes()
    53  	s.httpSrv = &http.Server{
    54  		Addr:    addr,
    55  		Handler: s.handler,
    56  	}
    57  	go func() {
    58  		err := s.httpSrv.ListenAndServe()
    59  		if err != nil {
    60  			log.Error("Error starting the HTTP server", "error", err)
    61  		}
    62  	}()
    63  	return s
    64  }
    65  
    66  //
    67  func (s *Simulation) addSimulationRoutes() {
    68  	s.handler.POST("/runsim", s.RunSimulation)
    69  }
    70  
    71  //
    72  func (s *Simulation) RunSimulation(w http.ResponseWriter, req *http.Request) {
    73  	log.Debug("RunSimulation endpoint running")
    74  	s.runC <- struct{}{}
    75  	w.WriteHeader(http.StatusOK)
    76  }