本文整理匯總了Golang中net/http.Request.ASN方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.ASN方法的具體用法?Golang Request.ASN怎麽用?Golang Request.ASN使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net/http.Request
的用法示例。
在下文中一共展示了Request.ASN方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ApiHandler
func ApiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
neighbors_lock.RLock()
defer neighbors_lock.RUnlock()
type ipasnResult struct {
ASN ASN
ASPath ASPath `json:",omitempty"`
Name string `json:",omitempty"`
Prefix string `json:",omitempty"`
}
switch vars["method"] {
case "ipasn":
_ = r.ParseForm()
ip := net.ParseIP(r.Form.Get("ip"))
if ip == nil {
w.WriteHeader(400)
fmt.Fprintln(w, "Bad IP address")
return
}
result := make(map[string]*ipasnResult)
for neighbor, data := range neighbors {
data.lock.RLock()
defer data.lock.RUnlock()
node := data.FindNode(&ip)
if node.Bits() > 0 {
result[neighbor] = new(ipasnResult)
route := node.Value.(*Route)
r := result[neighbor]
r.ASN = ASN(route.PrimaryASN)
r.ASPath = route.ASPath
r.Name = ""
r.Prefix = nodeToIPNet(node).String()
}
}
json, err := json.Marshal(map[string]interface{}{"result": result})
if err != nil {
w.WriteHeader(500)
log.Println("Error generating json", err)
fmt.Fprintln(w, "Could not generate JSON")
}
fmt.Fprint(w, string(json), "\n")
default:
w.WriteHeader(404)
return
}
}