예제 #1
0
func ResultsIndex(c web.C, w http.ResponseWriter, req *http.Request) {
	user, err := helpers.CurrentUser(c)

	if err != nil {
		renderError(c, w, req, "You need to re-authenticate", http.StatusUnauthorized)
		return
	}

	checkId, err := strconv.ParseInt(c.URLParams["check_id"], 10, 64)
	if err != nil {
		renderError(c, w, req, "Check not found", http.StatusNotFound)
		return
	}

	check, err := user.Check(checkId)

	if err != nil {
		renderError(c, w, req, "Check not found", http.StatusNotFound)
		return
	}

	results, err := check.Results()

	if err != nil {
		renderError(c, w, req, "No results found", http.StatusNotFound)
		return
	}

	render.JSON(w, http.StatusOK, results)
}
예제 #2
0
파일: ip.go 프로젝트: shirkevich/goffee
func IP(w http.ResponseWriter, req *http.Request) {
	db, err := geoip2.Open("geoip/GeoLite2-Country.mmdb")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

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

	// If you are using strings that may be invalid, check that ip is not nil
	ip := net.ParseIP(host)

	record, err := db.Country(ip)

	var responseJSON map[string]string

	if err != nil || record.Country.IsoCode == "" {
		responseJSON = map[string]string{
			"IP": ip.String(),
		}
	} else {
		responseJSON = map[string]string{
			"IP":      ip.String(),
			"Country": record.Country.IsoCode,
		}
	}

	render.JSON(w, http.StatusOK, responseJSON)
}