本文整理匯總了Golang中github.com/GeoNet/web.BadRequest函數的典型用法代碼示例。如果您正苦於以下問題:Golang BadRequest函數的具體用法?Golang BadRequest怎麽用?Golang BadRequest使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BadRequest函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: regions
// just quake regions at the moment.
func regions(w http.ResponseWriter, r *http.Request) {
if err := regionsD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "quake" {
web.BadRequest(w, r, "type must be quake.")
return
}
var d string
err := db.QueryRow(`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, array_to_json(array_agg(f)) as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(q.geom)::json as geometry,
row_to_json((SELECT l FROM
(
SELECT
regionname as "regionID",
title,
groupname as group
) as l
)) as properties FROM qrt.region as q where groupname in ('region', 'north', 'south')) as f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
w.Header().Set("Surrogate-Control", web.MaxAge86400)
b := []byte(d)
web.Ok(w, r, &b)
}
示例2: intensityMeasuredLatest
func intensityMeasuredLatest(w http.ResponseWriter, r *http.Request) {
if err := intensityMeasuredLatestD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "measured" {
web.BadRequest(w, r, "type must be measured.")
return
}
var d string
err := db.QueryRow(
`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, COALESCE(array_to_json(array_agg(f)), '[]') as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(s.location)::json as geometry,
row_to_json(( select l from
(
select mmi
) as l ))
as properties from (select location, mmi
FROM impact.intensity_measured) as s
) As f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
示例3: getYRange
/*
ymin, ymax = 0 - not set
ymin = ymin and != 0 - single range value
ymin != ymax - fixed range
*/
func getYRange(w http.ResponseWriter, r *http.Request) (ymin, ymax float64, ok bool) {
var err error
yr := r.URL.Query().Get("yrange")
switch {
case yr == "":
ok = true
case strings.Contains(yr, `,`):
y := strings.Split(yr, `,`)
if len(y) != 2 {
web.BadRequest(w, r, "invalid yrange query param.")
return
}
ymin, err = strconv.ParseFloat(y[0], 64)
if err != nil {
web.BadRequest(w, r, "invalid yrange query param.")
return
}
ymax, err = strconv.ParseFloat(y[1], 64)
if err != nil {
web.BadRequest(w, r, "invalid yrange query param.")
return
}
default:
ymin, err = strconv.ParseFloat(yr, 64)
if err != nil || ymin <= 0 {
web.BadRequest(w, r, "invalid yrange query param.")
return
}
ymax = ymin
}
ok = true
return
}
示例4: quake
func quake(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query()) != 0 {
web.BadRequest(w, r, "incorrect number of query parameters.")
return
}
publicID := r.URL.Path[quakeLen:]
// TODO bother with this?
if !publicIDRe.MatchString(publicID) {
web.BadRequest(w, r, "invalid publicID: "+publicID)
return
}
var d string
// Check that the publicid exists in the DB. This is needed as the handle method will return empty
// JSON for an invalid publicID.
err := db.QueryRow("select publicid FROM qrt.quake_materialized where publicid = $1", publicID).Scan(&d)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid publicID: "+publicID)
return
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
err = db.QueryRow(
`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, array_to_json(array_agg(f)) as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(q.origin_geom)::json as geometry,
row_to_json((SELECT l FROM
(
SELECT
publicid AS "publicID",
to_char(origintime, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') as "time",
depth,
magnitude,
type,
agency,
locality,
qrt.mmi_to_intensity(maxmmi) as intensity,
qrt.mmi_to_intensity(mmi_newzealand) as "regionIntensity",
qrt.quake_quality(status, usedphasecount, magnitudestationcount) as quality,
to_char(updatetime, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') as "modificationTime"
) as l
)) as properties FROM qrt.quake_materialized as q where publicid = $1 ) As f ) as fc`, publicID).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
示例5: getSites
/*
returns a zero length list if no sites are found.
*/
func getSites(w http.ResponseWriter, r *http.Request) ([]siteQ, bool) {
var sites = make([]siteQ, 0)
for _, ns := range strings.Split(r.URL.Query().Get("sites"), ",") {
nss := strings.Split(ns, ".")
if len(nss) != 2 {
web.BadRequest(w, r, "invalid sites query.")
return sites, false
}
sites = append(sites, siteQ{networkID: nss[0], siteID: nss[1]})
}
for _, s := range sites {
err := db.QueryRow("select name FROM fits.site join fits.network using (networkpk) where siteid = $2 and networkid = $1", s.networkID, s.siteID).Scan(&s.name)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid siteID and networkID combination: "+s.siteID+" "+s.networkID)
return sites, false
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return sites, false
}
}
return sites, true
}
示例6: alertLevel
func alertLevel(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query()) != 0 {
web.BadRequest(w, r, "incorrect number of query parameters.")
return
}
var d string
err := db.QueryRow(`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, array_to_json(array_agg(f)) as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(v.location)::json as geometry,
row_to_json((SELECT l FROM
(
SELECT
id AS "volcanoID",
title AS "volcanoTitle",
alert_level as "level",
activity,
hazards
) as l
)) as properties FROM (qrt.volcano JOIN qrt.volcanic_alert_level using (alert_level)) as v ) As f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
示例7: intensityReportedLatest
func intensityReportedLatest(w http.ResponseWriter, r *http.Request) {
if err := intensityReportedLatestD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
if r.URL.Query().Get("type") != "reported" {
web.BadRequest(w, r, "type must be reported.")
return
}
zoom := r.URL.Query().Get("zoom")
if !zoomRe.MatchString(r.URL.Query().Get("zoom")) {
web.BadRequest(w, r, "Invalid zoom")
return
}
var d string
err := db.QueryRow(
`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, COALESCE(array_to_json(array_agg(f)), '[]') as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(s.location)::json as geometry,
row_to_json(( select l from
(
select max_mmi,
min_mmi,
count
) as l ))
as properties from (select st_pointfromgeohash(geohash` + zoom + `) as location,
min(mmi) as min_mmi,
max(mmi) as max_mmi,
count(mmi) as count
FROM impact.intensity_reported
WHERE time >= (now() - interval '60 minutes')
group by (geohash` + zoom + `)) as s
) As f ) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
示例8: siteType
func siteType(w http.ResponseWriter, r *http.Request) {
if err := siteTypeD.CheckParams(r.URL.Query()); err != nil {
web.BadRequest(w, r, err.Error())
return
}
v := r.URL.Query()
if v.Get("methodID") != "" && v.Get("typeID") == "" {
web.BadRequest(w, r, "typeID must be specified when methodID is specified.")
return
}
var typeID, methodID, within string
if v.Get("typeID") != "" {
typeID = v.Get("typeID")
if !validType(w, r, typeID) {
return
}
if v.Get("methodID") != "" {
methodID = v.Get("methodID")
if !validTypeMethod(w, r, typeID, methodID) {
return
}
}
}
if v.Get("within") != "" {
within = strings.Replace(v.Get("within"), "+", "", -1)
if !validPoly(w, r, within) {
return
}
}
w.Header().Set("Content-Type", web.V1GeoJSON)
b, err := geoJSONSites(typeID, methodID, within)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
web.Ok(w, r, &b)
}
示例9: getStddev
func getStddev(w http.ResponseWriter, r *http.Request) (string, bool) {
t := r.URL.Query().Get("stddev")
switch t {
case ``, `pop`:
return t, true
default:
web.BadRequest(w, r, "invalid stddev type")
return ``, false
}
}
示例10: getSparkLabel
func getSparkLabel(w http.ResponseWriter, r *http.Request) (string, bool) {
t := r.URL.Query().Get("label")
switch t {
case ``, `all`, `none`, `latest`:
return t, true
default:
web.BadRequest(w, r, "invalid label")
return ``, false
}
}
示例11: getPlotType
func getPlotType(w http.ResponseWriter, r *http.Request) (string, bool) {
t := r.URL.Query().Get("type")
switch t {
case ``, `line`, `scatter`:
return t, true
default:
web.BadRequest(w, r, "invalid plot type")
return ``, false
}
}
示例12: region
func region(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query()) != 0 {
web.BadRequest(w, r, "incorrect number of query parameters.")
return
}
regionID := r.URL.Path[regionLen:]
var d string
err := db.QueryRow("select regionname FROM qrt.region where regionname = $1", regionID).Scan(&d)
if err == sql.ErrNoRows {
web.BadRequest(w, r, "invalid regionID: "+regionID)
return
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
err = db.QueryRow(`SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' as type, array_to_json(array_agg(f)) as features
FROM (SELECT 'Feature' as type,
ST_AsGeoJSON(q.geom)::json as geometry,
row_to_json((SELECT l FROM
(
SELECT
regionname as "regionID",
title,
groupname as group
) as l
)) as properties FROM qrt.region as q where regionname = $1 ) as f ) as fc`, regionID).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
w.Header().Set("Surrogate-Control", web.MaxAge86400)
b := []byte(d)
web.Ok(w, r, &b)
}
示例13: getDays
/*
Returns 0 if days not set
*/
func getDays(w http.ResponseWriter, r *http.Request) (int, bool) {
var days int
if r.URL.Query().Get("days") != "" {
var err error
days, err = strconv.Atoi(r.URL.Query().Get("days"))
if err != nil || days > 365000 {
web.BadRequest(w, r, "Invalid days query param.")
return 0, false
}
}
return days, true
}
示例14: router
func router(w http.ResponseWriter, r *http.Request) {
// requests that don't have a specific version header are routed to the latest version.
var latest bool
accept := r.Header.Get("Accept")
switch accept {
case web.V1GeoJSON, web.V1JSON, web.V1CSV:
default:
latest = true
}
switch {
case r.URL.Path == "/plot":
if r.URL.Query().Get("siteID") != "" {
plotSite(w, r)
} else {
plotSites(w, r)
}
case r.URL.Path == "/spark":
spark(w, r)
case r.URL.Path == "/map/site":
if r.URL.Query().Get("siteID") != "" {
siteMap(w, r)
} else if r.URL.Query().Get("sites") != "" {
siteMap(w, r)
} else {
siteTypeMap(w, r)
}
case r.URL.Path == "/observation" && (accept == web.V1CSV || latest):
if r.URL.Query().Get("siteID") != "" {
observation(w, r)
} else {
spatialObs(w, r)
}
case r.URL.Path == "/observation/stats" && (accept == web.V1JSON || latest):
observationStats(w, r)
case r.URL.Path == "/site" && (accept == web.V1GeoJSON || latest):
if r.URL.Query().Get("siteID") != "" {
site(w, r)
} else {
siteType(w, r)
}
case r.URL.Path == "/type" && (accept == web.V1JSON || latest):
typeH(w, r)
case r.URL.Path == "/method" && (accept == web.V1JSON || latest):
method(w, r)
case strings.HasPrefix(r.URL.Path, apidoc.Path):
docs.Serve(w, r)
default:
web.BadRequest(w, r, "Can't find a route for this request. Please refer to /api-docs")
}
}
示例15: getStart
/*
Returns zero time if not set.
*/
func getStart(w http.ResponseWriter, r *http.Request) (time.Time, bool) {
var t time.Time
if r.URL.Query().Get("start") != "" {
var err error
t, err = time.Parse(time.RFC3339, r.URL.Query().Get("start"))
if err != nil {
web.BadRequest(w, r, "Invalid start query param.")
return t, false
}
}
return t, true
}