Add test handler and net/http server
This commit is contained in:
parent
5226cd0c61
commit
dc9b388197
14
server.go
14
server.go
@ -4,6 +4,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@ -71,6 +72,7 @@ func (s *Server) handleConn(conn *net.TCPConn) {
|
||||
|
||||
type options struct {
|
||||
Port uint
|
||||
HttpPort uint
|
||||
Address string
|
||||
Registry string
|
||||
SocketTimeout float64
|
||||
@ -79,6 +81,7 @@ type options struct {
|
||||
func parseFlags() options {
|
||||
var o options
|
||||
flag.UintVar(&o.Port, "port", 43, "port to listen")
|
||||
flag.UintVar(&o.HttpPort, "httpport", 80, "port to listen on for http")
|
||||
flag.StringVar(&o.Address, "address", "*", "address to listen")
|
||||
flag.StringVar(&o.Registry, "registry", ".", "path to dn42 registry")
|
||||
msg := "timeout in seconds before suspending the service when using socket activation"
|
||||
@ -161,6 +164,17 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
httpRouter := http.NewServeMux()
|
||||
httpRouter.Handle("/", http.HandlerFunc(server.registry.HandleHTTPJSON))
|
||||
|
||||
go func() {
|
||||
address := opts.Address + ":" + strconv.Itoa(int(opts.HttpPort))
|
||||
if err := http.ListenAndServe(address, httpRouter); err != nil && err != http.ErrServerClosed {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, os.Interrupt)
|
||||
signal.Notify(signals, syscall.SIGTERM)
|
||||
|
28
whois/json.go
Normal file
28
whois/json.go
Normal file
@ -0,0 +1,28 @@
|
||||
package whois
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (r *Registry) HandleHTTPJSON(w http.ResponseWriter, req *http.Request) {
|
||||
content := []byte("")
|
||||
q, ok := req.URL.Query()["q"]
|
||||
if !ok || len(q[0]) < 1 {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
o := parseObject(q[0])
|
||||
paths := r.findObjectPaths(o)
|
||||
for _, p := range paths {
|
||||
c, _, err := r.retrieveObject(p.objtype, p.obj)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
content = append(content[:], c[:]...)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, string(content))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user