Пример #1
0
func NewProxyHandler(newurl, grabIF string) *ProxyRoute {
	tr := &http.Transport{
		TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
		DisableCompression: true,
		Dial: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
		}).Dial,
		TLSHandshakeTimeout: 10 * time.Second,
	}

	if grabIF != "" {
		go func() {
			for {
				// Wait for 10s.
				time.Sleep(10 * time.Second)
				if (FailNum + SuccNum) > 0 {
					log.Printf("\n\tIF: %s SuccNum:%d FailNum:%d FailureRate:%f\n\n", grabIF, SuccNum, FailNum, float32((FailNum))/float32((FailNum+SuccNum)))
				}
			}
		}()
	}
	newDB, err := db.NewReplayDB()
	if err != nil {
		log.Println("Open ReplayDB Error ", err)
	}
	return &ProxyRoute{
		client: &http.Client{Transport: tr},
		url:    newurl,
		GrabIF: grabIF,
		db:     newDB}
}
Пример #2
0
func NewMiddleware() *offlinemiddleware {
	router := httprouter.New()
	db, err := db.NewReplayDB()
	db.ReadDir("./input")
	if err != nil {
		log.Println("Open replayDB error ", err)
	}

	for _, route := range routes {
		httpHandle := Logger(route.HandleFunc, route.Name)

		router.Handle(
			route.Method,
			route.Pattern,
			httpHandle,
		)
	}

	router.NotFound = LoggerNotFound(NotFoundHandler)
	GlobalDB, err = bolt.Open("./OfflineDB", 0666, nil)
	if err != nil {
		log.Println("Open OfflineDB error ", err)
	}
	return &offlinemiddleware{
		router:   router,
		replaydb: db,
	}
}