示例#1
0
func (s *MixedService) GetMostPopular(r *http.Request) (int, interface{}, error) {
	resourceType := web.Vars(r)["resourceType"]
	section := web.Vars(r)["section"]
	timeframe := web.GetUInt64Var(r, "timeframe")
	res, err := s.client.GetMostPopular(resourceType, section, uint(timeframe))
	if err != nil {
		return http.StatusInternalServerError, nil, &jsonErr{err.Error()}
	}
	return http.StatusOK, res, nil
}
示例#2
0
func (s *RPCService) GetMostPopularJSON(ctx context.Context, r *http.Request) (int, interface{}, error) {
	res, err := s.GetMostPopular(
		ctx,
		&MostPopularRequest{
			web.Vars(r)["resourceType"],
			web.Vars(r)["section"],
			uint32(web.GetUInt64Var(r, "timeframe")),
		})
	if err != nil {
		return http.StatusInternalServerError, nil, err
	}
	return http.StatusOK, res.Result, nil
}
示例#3
0
// GetIP returns the IP address for the given request.
func GetIP(r *http.Request) (string, error) {
	ip, ok := web.Vars(r)["ip"]
	if ok {
		return ip, nil
	}

	// check real ip header first
	ip = r.Header.Get("X-Real-IP")
	if len(ip) > 0 {
		return ip, nil
	}

	// no nginx reverse proxy?
	// get IP old fashioned way
	ip, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		return "", fmt.Errorf("%q is not IP:port", r.RemoteAddr)
	}

	userIP := net.ParseIP(ip)
	if userIP == nil {
		return "", fmt.Errorf("%q is not IP:port", r.RemoteAddr)
	}
	return userIP.String(), nil
}
示例#4
0
func (s *benchmarkSimpleService) GetSimple(w http.ResponseWriter, r *http.Request) {
	something := web.Vars(r)["something"]
	fmt.Fprint(w, something)
}
示例#5
0
func (s *benchmarkContextService) GetSimple(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	something := web.Vars(r)["something"]
	fmt.Fprint(w, something)
}
示例#6
0
func (s *benchmarkJSONService) GetJSONParam(r *http.Request) (int, interface{}, error) {
	something := web.Vars(r)["something"]
	return http.StatusOK, &testJSON{"hi", something}, nil
}