Add API v1, help, etc
This commit is contained in:
parent
5dd03786b0
commit
3715e7af5a
32
info.go
Normal file
32
info.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HandleHTTPHelp() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request){
|
||||
content := `<html><body><pre>
|
||||
Whois42d JSON API:
|
||||
|
||||
Paths:
|
||||
/ This message, help.
|
||||
/api/1/text Query with text response
|
||||
/api/1/json Query with json response
|
||||
/api/1/version Server version query
|
||||
/api/1/types Server types query ( list all schema types )
|
||||
|
||||
Query parameter:
|
||||
The URL variable 'q' should be filled in with your query
|
||||
|
||||
Ex:
|
||||
/api/1/json?q=10.0.0.0/8
|
||||
/api/1/json?q=SOMEONE-MNT
|
||||
</pre></body></html>
|
||||
`
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, content)
|
||||
})
|
||||
}
|
@ -194,7 +194,11 @@ func main() {
|
||||
|
||||
// create HTTP server
|
||||
httpRouter := http.NewServeMux()
|
||||
httpRouter.Handle("/", http.HandlerFunc(server.registry.HandleHTTPJSON))
|
||||
httpRouter.Handle("/", HandleHTTPHelp())
|
||||
httpRouter.Handle("/api/1/text", server.registry.HandleHTTPBoth("text"))
|
||||
httpRouter.Handle("/api/1/json", server.registry.HandleHTTPBoth("json"))
|
||||
httpRouter.Handle("/api/1/version", server.registry.HandleHTTPVersion())
|
||||
httpRouter.Handle("/api/1/types", server.registry.HandleHTTPTypes())
|
||||
|
||||
go func() {
|
||||
address := opts.Address + ":" + strconv.Itoa(int(opts.HttpPort))
|
||||
|
@ -3,13 +3,19 @@ package whois
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func (r *Registry) HandleHTTPJSON(w http.ResponseWriter, req *http.Request) {
|
||||
func (r *Registry) HandleHTTPBoth(t string) http.Handler {
|
||||
// t := "json"
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request){
|
||||
content := []byte("")
|
||||
var m []map[string]string
|
||||
q, ok := req.URL.Query()["q"]
|
||||
if !ok || len(q[0]) < 1 {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
http.Error(w, "Bad request", 400)
|
||||
return
|
||||
}
|
||||
o := parseObject(q[0])
|
||||
@ -20,9 +26,75 @@ func (r *Registry) HandleHTTPJSON(w http.ResponseWriter, req *http.Request) {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
m = append(m,WhoisToMap(c))
|
||||
content = append(content[:], c[:]...)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(content))
|
||||
switch t {
|
||||
case "json":
|
||||
j, jerr := json.Marshal(m)
|
||||
if jerr != nil {
|
||||
http.Error(w, "Bad request", 400)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(j))
|
||||
}
|
||||
return
|
||||
case "text":
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(content))
|
||||
return
|
||||
default:
|
||||
fmt.Println(t)
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func WhoisToMap(b []byte) map[string]string {
|
||||
r := make(map[string]string)
|
||||
for _, l := range bytes.Split(b, []byte("\n")) {
|
||||
i := bytes.Index(l, []byte(":"))
|
||||
if i > 0 {
|
||||
r[string(bytes.TrimSpace(l[0:i]))] = string(bytes.TrimSpace(l[i+1:]))
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Registry) HandleHTTPVersion() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request){
|
||||
m := map[string]string{
|
||||
"version": strconv.FormatInt(VERSION, 10),
|
||||
}
|
||||
j, jerr := json.Marshal(m)
|
||||
if jerr != nil {
|
||||
http.Error(w, "Bad request", 400)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(j))
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Registry) HandleHTTPTypes() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request){
|
||||
var m []string
|
||||
for _, t := range r.whoisTypes {
|
||||
m = append(m,t.Name)
|
||||
}
|
||||
j, jerr := json.Marshal(m)
|
||||
if jerr != nil {
|
||||
http.Error(w, "Bad request", 400)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(j))
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user