Пример #1
0
func handleProxyList(ctx context.Context, w http.ResponseWriter, r *http.Request) (*endpoint.EPErr, context.Context) {
	storage := db.GetProxyStorage(ctx)
	l, err := storage.GetAll()
	if err != nil {
		return endpoint.Err500(fmt.Errorf("Get proxy list: %v", err)), ctx
	}
	conv := mw.GetConv(ctx)
	conv.Write(l)
	if conv.Err != nil {
		return endpoint.Err500(fmt.Errorf("Response marshalling: %v", conv.Err)), ctx
	}
	return nil, ctx
}
Пример #2
0
func handleProxyCheck(ctx context.Context, w http.ResponseWriter, r *http.Request) (*endpoint.EPErr, context.Context) {
	var req model.ProxyCheckRequest
	conv := mw.GetConv(ctx)
	conv.ReadIn(&req)
	if conv.Err != nil {
		return endpoint.Err500(fmt.Errorf("request unmarshalling : %v", conv.Err)), ctx
	}
	if err := req.Validate(); err != nil {
		return endpoint.Err400(fmt.Errorf("request validation : %v", err)), ctx
	}
	logger.Tracef("%s", req)
	result, ping := checks.RunChecks(req.Checks, req.Url)
	resp := model.ProxyCheckResponse{ping, result}

	conv.Write(resp)
	if conv.Err != nil {
		return endpoint.Err500(fmt.Errorf("response marshalling : %v", conv.Err)), ctx
	}
	return nil, ctx
}
Пример #3
0
func handleNewProxyList(ctx context.Context, w http.ResponseWriter, r *http.Request) (*endpoint.EPErr, context.Context) {
	var list NewProxyList
	conv := mw.GetConv(ctx)
	conv.ReadIn(&list)
	logger.Tracef("Handling new proxy list %v", list)
	if conv.Err != nil {
		return endpoint.Err500(fmt.Errorf("Request unmarshalling: %v", conv.Err)), ctx
	}
	storage := db.GetProxyStorage(ctx)
	for _, p := range list {
		proxy, err := p.parse()
		if err != nil {
			logger.Tracef("Invalid proxy %s. Error: %s, Skipping", p, err)
			continue
		}
		//todo batch save
		logger.Tracef("Saving proxy %v", proxy)
		if _, err := storage.SaveProxy(proxy, false); err != nil {
			logger.Errf("Saving proxy to storage: %v", err)
		}
	}
	return nil, ctx
}