Exemplo n.º 1
0
func queueHandler(w http.ResponseWriter, r *http.Request) {
	queue_name := r.Vars[0]
	if queue_name == "" {
		http.Error(w, "Queue name not given", 404)
		return
	}
	qname := queue_name + QUEUE_SUFFIX
	quuid := queue_name + UUID_SUFFIX
	soft := r.FormValue("soft")
	callback := r.FormValue("callback")

	switch r.Method {
	case "GET":
		var (
			p   string
			err error
		)

		if soft != "" {
			p, err = redis_client.LIndex(qname, -1)
		} else {
			p, err = redis_client.RPop(qname)
		}

		if p == "" {
			http.Error(w, "Empty queue", 404)
			return
		}

		if err == nil {
			v, err := redis_client.Get(p)
			if err == nil {
				resp := Response{"key": p, "value": v}

				if err == nil {
					w.Header().Set("Content-Type", "application/json")
					if callback == "" {
						fmt.Fprintf(w, "%s\n", resp)
					} else {
						fmt.Fprintf(w, "%s(%s);\n", callback, resp)
					}
					return
				}
				http.Error(w, "Value not found", 500)
				return
			}
		}
	case "POST":
		value := r.FormValue("value")
		if value == "" {
			http.Error(w, "Empty value", 401)
		}

		uuid, _ := redis_client.Incr(quuid)
		lkey := queue_name + ":" + strconv.Itoa(uuid)
		redis_client.Set(lkey, value)
		redis_client.LPush(qname, lkey)
		resp := Response{"key": lkey, "value": value}
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, "%s\n", resp)
	default:
		http.Error(w, "Method not accepted", 400)
		return

	}
}
Exemplo n.º 2
0
func Lookup(w http.ResponseWriter, req *http.Request, db *sql.DB) {
	format, addr := req.Vars[0], req.Vars[1]
	if addr == "" {
		addr = req.RemoteAddr // port number previously removed
	} else {
		addrs, err := net.LookupHost(addr)
		if err != nil {
			http.Error(w, http.StatusText(404), 404)
			return
		}
		addr = addrs[0]
	}
	IP := net.ParseIP(addr)
	reserved := false
	for _, net := range reservedIPs {
		if net.Contains(IP) {
			reserved = true
			break
		}
	}
	geoip := GeoIP{Ip: addr}
	if reserved {
		geoip.CountryCode = "RD"
		geoip.CountryName = "Reserved"
	} else {
		q := "SELECT " +
			"  city_location.country_code, country_blocks.country_name, " +
			"  city_location.region_code, region_names.region_name, " +
			"  city_location.city_name, city_location.postal_code, " +
			"  city_location.latitude, city_location.longitude, " +
			"  city_location.metro_code, city_location.area_code " +
			"FROM city_blocks " +
			"  NATURAL JOIN city_location " +
			"  INNER JOIN country_blocks ON " +
			"    city_location.country_code = country_blocks.country_code " +
			"  INNER JOIN region_names ON " +
			"    city_location.country_code = region_names.country_code " +
			"    AND " +
			"    city_location.region_code = region_names.region_code " +
			"WHERE city_blocks.ip_start <= ? " +
			"ORDER BY city_blocks.ip_start DESC LIMIT 1"
		stmt, err := db.Prepare(q)
		if err != nil {
			if debug {
				log.Println("[debug] SQLite", err.Error())
			}
			http.Error(w, http.StatusText(500), 500)
			return
		}
		defer stmt.Close()
		var uintIP uint32
		b := bytes.NewBuffer(IP.To4())
		binary.Read(b, binary.BigEndian, &uintIP)
		err = stmt.QueryRow(uintIP).Scan(
			&geoip.CountryCode,
			&geoip.CountryName,
			&geoip.RegionCode,
			&geoip.RegionName,
			&geoip.CityName,
			&geoip.ZipCode,
			&geoip.Latitude,
			&geoip.Longitude,
			&geoip.MetroCode,
			&geoip.AreaCode)
		if err != nil {
			http.Error(w, http.StatusText(404), 404)
			return
		}
	}
	switch format[0] {
	case 'c':
		w.Header().Set("Content-Type", "application/csv")
		fmt.Fprintf(w, `"%s","%s","%s","%s","%s","%s",`+
			`"%s","%0.4f","%0.4f","%s","%s"`+"\r\n",
			geoip.Ip,
			geoip.CountryCode, geoip.CountryName,
			geoip.RegionCode, geoip.RegionName,
			geoip.CityName, geoip.ZipCode,
			geoip.Latitude, geoip.Longitude,
			geoip.MetroCode, geoip.AreaCode)
	case 'j':
		resp, err := json.Marshal(geoip)
		if err != nil {
			if debug {
				log.Println("[debug] JSON", err.Error())
			}
			http.Error(w, http.StatusText(404), 404)
			return
		}
		callback := req.FormValue("callback")
		if callback != "" {
			w.Header().Set("Content-Type", "text/javascript")
			fmt.Fprintf(w, "%s(%s);\n", callback, resp)
		} else {
			w.Header().Set("Content-Type", "application/json")
			fmt.Fprintf(w, "%s\n", resp)
		}
	case 'x':
		w.Header().Set("Content-Type", "application/xml")
		resp, err := xml.MarshalIndent(geoip, "", " ")
		if err != nil {
			if debug {
				log.Println("[debug] XML", err.Error())
			}
			http.Error(w, http.StatusText(500), 500)
			return
		}
		fmt.Fprintf(w, xml.Header+"%s\n", resp)
	}
}