當前位置: 首頁>>代碼示例>>Golang>>正文


Golang GeoIP.GetCountry方法代碼示例

本文整理匯總了Golang中github.com/abh/geoip.GeoIP.GetCountry方法的典型用法代碼示例。如果您正苦於以下問題:Golang GeoIP.GetCountry方法的具體用法?Golang GeoIP.GetCountry怎麽用?Golang GeoIP.GetCountry使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/abh/geoip.GeoIP的用法示例。


在下文中一共展示了GeoIP.GetCountry方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: parseQueries

func parseQueries(queries <-chan *store.Query, geolocator *geoip.GeoIP) <-chan *store.ParsedQuery {
	parsedQueries := make(chan *store.ParsedQuery)
	go func() {
		for query := range queries {
			request, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(query.RawRequest)))
			if err != nil {
				log.Printf("error parsing result request: %v", err)
			}
			clientIp := request.Header.Get("X-Real-Ip")
			if clientIp == "" {
				clientIp = query.RemoteAddr
			}

			var parameters map[string]string
			if err := json.Unmarshal(query.ParametersJson, &parameters); err != nil {
				log.Printf("error parsing query parameters: %v", err)
			}

			parametersNullable := make(map[string]sql.NullString)
			for k, v := range parameters {
				parametersNullable[k] = sql.NullString{
					String: v,
					Valid:  true,
				}
			}

			host, _, err := net.SplitHostPort(clientIp)
			if err != nil {
				host = clientIp
			}

			country, _ := geolocator.GetCountry(host)

			parsedQueries <- &store.ParsedQuery{
				Query:          query.Id,
				MeasurementId:  parameters["measurementId"],
				Timestamp:      query.Timestamp,
				ClientIp:       net.ParseIP(host),
				ClientLocation: country,
				Substrate:      query.Substrate,
				Parameters:     parametersNullable,
			}
		}
		close(parsedQueries)
	}()
	return parsedQueries
}
開發者ID:a-jain,項目名稱:encore,代碼行數:47,代碼來源:parser.go

示例2: parseResults

func parseResults(results <-chan *store.Result, geolocator *geoip.GeoIP) <-chan *store.ParsedResult {
	parsedResults := make(chan *store.ParsedResult)
	go func() {
		for result := range results {
			request, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(result.RawRequest)))
			if err != nil {
				log.Printf("error parsing result request: %v", err)
			}
			measurementId := request.URL.Query().Get("cmh-id")
			outcome := request.URL.Query().Get("cmh-result")
			message := request.URL.Query().Get("cmh-message")
			userAgent := request.Header.Get("User-Agent")
			origin := request.Header.Get("Origin")
			referer := request.Header.Get("Referer")
			clientIp := request.Header.Get("X-Real-Ip")
			if clientIp == "" {
				clientIp = result.RemoteAddr
			}

			host, _, err := net.SplitHostPort(clientIp)
			if err != nil {
				host = clientIp
			}

			country, _ := geolocator.GetCountry(host)

			parsedResults <- &store.ParsedResult{
				Result:         result.Id,
				Timestamp:      result.Timestamp,
				MeasurementId:  measurementId,
				Outcome:        outcome,
				Message:        message,
				Origin:         origin,
				Referer:        referer,
				ClientIp:       net.ParseIP(host),
				ClientLocation: country,
				UserAgent:      userAgent,
			}
		}
		close(parsedResults)
	}()
	return parsedResults

}
開發者ID:a-jain,項目名稱:encore,代碼行數:44,代碼來源:parser.go

示例3: test4

func test4(g geoip.GeoIP, ip string) {
	test(func(s string) string { return g.GetCountry(s) }, ip)
}
開發者ID:araddon,項目名稱:geoip,代碼行數:3,代碼來源:geoip-demo.go

示例4: test4

func test4(g *geoip.GeoIP, ip string) {
	test(func(s string) (string, int) { return g.GetCountry(s) }, ip)
}
開發者ID:aziontech,項目名稱:geoip,代碼行數:3,代碼來源:geoip-demo.go


注:本文中的github.com/abh/geoip.GeoIP.GetCountry方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。