mirror of
https://github.com/skidoodle/ipinfo.git
synced 2025-02-15 08:29:17 +01:00
chore: callback format
This commit is contained in:
parent
a579c69595
commit
afb88ffeaf
1 changed files with 10 additions and 37 deletions
47
main.go
47
main.go
|
@ -161,29 +161,11 @@ type dataStruct struct {
|
||||||
Loc string `json:"loc"`
|
Loc string `json:"loc"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var nameToField = map[string]func(dataStruct) string{
|
|
||||||
"ip": func(d dataStruct) string { return d.IP },
|
|
||||||
"hostname": func(d dataStruct) string { return d.Hostname },
|
|
||||||
"asn": func(d dataStruct) string { return d.ASN },
|
|
||||||
"organization": func(d dataStruct) string { return d.Organization },
|
|
||||||
"city": func(d dataStruct) string { return d.City },
|
|
||||||
"region": func(d dataStruct) string { return d.Region },
|
|
||||||
"country": func(d dataStruct) string { return d.Country },
|
|
||||||
"country_full": func(d dataStruct) string { return d.CountryFull },
|
|
||||||
"continent": func(d dataStruct) string { return d.Continent },
|
|
||||||
"continent_full": func(d dataStruct) string { return d.ContinentFull },
|
|
||||||
"loc": func(d dataStruct) string { return d.Loc },
|
|
||||||
}
|
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
requestedThings := strings.Split(r.URL.Path, "/")
|
requestedThings := strings.Split(r.URL.Path, "/")
|
||||||
|
|
||||||
var IPAddress, Which string
|
var IPAddress string
|
||||||
switch len(requestedThings) {
|
if len(requestedThings) > 1 {
|
||||||
case 3:
|
|
||||||
Which = requestedThings[2]
|
|
||||||
fallthrough
|
|
||||||
case 2:
|
|
||||||
IPAddress = requestedThings[1]
|
IPAddress = requestedThings[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,29 +247,20 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
Loc: fmt.Sprintf("%.4f,%.4f", cityRecord.Location.Latitude, cityRecord.Location.Longitude),
|
Loc: fmt.Sprintf("%.4f,%.4f", cityRecord.Location.Latitude, cityRecord.Location.Longitude),
|
||||||
}
|
}
|
||||||
|
|
||||||
if Which == "" {
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
callback := r.URL.Query().Get("callback")
|
||||||
callback := r.URL.Query().Get("callback")
|
enableJSONP := callback != "" && len(callback) < 2000 && callbackJSONP.MatchString(callback)
|
||||||
enableJSONP := callback != "" && len(callback) < 2000 && callbackJSONP.MatchString(callback)
|
if enableJSONP {
|
||||||
if enableJSONP {
|
jsonData, _ := json.MarshalIndent(d, "", " ")
|
||||||
w.Write([]byte(fmt.Sprintf("/**/ typeof %s === 'function' && %s(", callback, callback)))
|
response := fmt.Sprintf("/**/ typeof %s === 'function' && %s(%s);", callback, callback, jsonData)
|
||||||
}
|
w.Write([]byte(response))
|
||||||
|
} else {
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
if r.URL.Query().Get("compact") == "true" {
|
if r.URL.Query().Get("compact") == "true" {
|
||||||
enc.SetIndent("", "")
|
enc.SetIndent("", "")
|
||||||
}
|
}
|
||||||
enc.Encode(d)
|
enc.Encode(d)
|
||||||
if enableJSONP {
|
|
||||||
w.Write([]byte(");"))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
||||||
if val := nameToField[Which]; val != nil {
|
|
||||||
w.Write([]byte(val(d)))
|
|
||||||
} else {
|
|
||||||
w.Write([]byte("undefined"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue