dhcp4d: serve lease json data at /lease/<hostname>
This commit is contained in:
parent
3707ba290c
commit
4558cb61b4
@ -21,12 +21,14 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -249,6 +251,36 @@ func logic() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
http.HandleFunc("/lease/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
hostname := strings.TrimPrefix(r.URL.Path, "/lease/")
|
||||||
|
if hostname == "" {
|
||||||
|
http.Error(w, "syntax: /lease/<hostname>", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
leasesMu.Lock()
|
||||||
|
defer leasesMu.Unlock()
|
||||||
|
var lease *dhcp4d.Lease
|
||||||
|
for _, l := range leases {
|
||||||
|
if l.Hostname != hostname {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lease = l
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if lease == nil {
|
||||||
|
http.Error(w, "no lease found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(lease)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(w, bytes.NewReader(b)); err != nil {
|
||||||
|
log.Printf("/lease/%s: %v", hostname, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user