github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/network/simulation/http.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:47</date>
    10  //</624342673725067264>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package simulation
    29  
    30  import (
    31  	"fmt"
    32  	"net/http"
    33  
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/p2p/simulations"
    36  )
    37  
    38  //
    39  var (
    40  	DefaultHTTPSimAddr = ":8888"
    41  )
    42  
    43  //
    44  //
    45  func (s *Simulation) WithServer(addr string) *Simulation {
    46  //
    47  	if addr == "" {
    48  		addr = DefaultHTTPSimAddr
    49  	}
    50  	log.Info(fmt.Sprintf("Initializing simulation server on %s...", addr))
    51  //
    52  	s.handler = simulations.NewServer(s.Net)
    53  	s.runC = make(chan struct{})
    54  //
    55  	s.addSimulationRoutes()
    56  	s.httpSrv = &http.Server{
    57  		Addr:    addr,
    58  		Handler: s.handler,
    59  	}
    60  	go func() {
    61  		err := s.httpSrv.ListenAndServe()
    62  		if err != nil {
    63  			log.Error("Error starting the HTTP server", "error", err)
    64  		}
    65  	}()
    66  	return s
    67  }
    68  
    69  //
    70  func (s *Simulation) addSimulationRoutes() {
    71  	s.handler.POST("/runsim", s.RunSimulation)
    72  }
    73  
    74  //
    75  func (s *Simulation) RunSimulation(w http.ResponseWriter, req *http.Request) {
    76  	log.Debug("RunSimulation endpoint running")
    77  	s.runC <- struct{}{}
    78  	w.WriteHeader(http.StatusOK)
    79  }
    80