本文整理汇总了Golang中github.com/coocood/jas.Context.ParseForm方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.ParseForm方法的具体用法?Golang Context.ParseForm怎么用?Golang Context.ParseForm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/coocood/jas.Context
的用法示例。
在下文中一共展示了Context.ParseForm方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetAll
// GetAll dumps the entire database of nodes, including cached
// ones. If the form value `since` is supplied with a valid RFC3339
// timestamp, only nodes updated or cached more recently than that
// will be dumped. If 'geojson' is present, then the "data" field
// contains the dump in GeoJSON compliant form.
func (*Api) GetAll(ctx *jas.Context) {
// We must invoke ParseForm() so that we can access ctx.Form.
ctx.ParseForm()
// In order to access this at the end, we need to declare nodes
// here, so the results from the dump don't go out of scope.
var nodes []*Node
var err error
// If the form value "since" was supplied, we will be doing a dump
// based on update/retrieve time.
if tstring := ctx.FormValue("since"); len(tstring) > 0 {
var t time.Time
t, err = time.Parse(time.RFC3339, tstring)
if err != nil {
ctx.Data = err.Error()
ctx.Error = jas.NewRequestError("invalidTime")
return
}
// Now, perform the time-based dump. Errors will be handled
// outside the if block.
nodes, err = Db.DumpChanges(t)
} else {
// If there was no "since," provide a simple full-database
// dump.
nodes, err = Db.DumpNodes()
}
// Handle any database errors here.
if err != nil {
ctx.Error = jas.NewInternalError(err)
l.Err(err)
return
}
// If the form value 'geojson' is included, dump in GeoJSON
// form. Otherwise, just dump with normal marhshalling.
if _, ok := ctx.Form["geojson"]; ok {
ctx.Data = FeatureCollectionNodes(nodes)
} else {
mappedNodes, err := Db.CacheFormatNodes(nodes)
if err != nil {
ctx.Error = jas.NewInternalError(err)
l.Err(err)
return
}
ctx.Data = mappedNodes
}
}
示例2: GetNode
// GetNode retrieves a single node from the database, removes
// sensitive data (such as an email address) and sets ctx.Data to
// it. If `?geojson` is set, then it returns it in geojson.Feature
// form.
func (*Api) GetNode(ctx *jas.Context) {
ip := IP(net.ParseIP(ctx.RequireStringLen(0, 40, "address")))
if ip == nil {
// If this is encountered, the address was incorrectly
// formatted.
ctx.Error = jas.NewRequestError("addressInvalid")
return
}
node, err := Db.GetNode(ip)
if err != nil {
// If there has been a database error, log it and report the
// failure.
ctx.Error = jas.NewInternalError(err)
l.Err(err)
return
}
if node == nil {
// If there are simply no matching nodes, set the error and
// return.
ctx.Error = jas.NewRequestError("No matching node")
return
}
// We must invoke ParseForm() so that we can access ctx.Form.
ctx.ParseForm()
// If the form value 'geojson' is included, dump in GeoJSON
// form. Otherwise, just dump with normal marhshalling.
if _, ok := ctx.Form["geojson"]; ok {
ctx.Data = node.Feature()
return
} else {
// Only after removing any sensitive data, though.
node.OwnerEmail = ""
// Finally, set the data and exit.
ctx.Data = node
}
}