dyndns/webapi/server.go

52 lines
1.1 KiB
Go
Raw Normal View History

package webapi
import (
"fmt"
"log"
"net/http"
"gitea.nehmer.net/torben/dyndns/service"
"github.com/gorilla/mux"
)
func Server() {
r := mux.NewRouter()
r.StrictSlash(true)
r.HandleFunc("/hello", handleHello)
r.HandleFunc("/update", handleDNSUpdate)
log.Printf("Listening to: %s", C.ListenAddress)
go log.Fatal(http.ListenAndServe(C.ListenAddress, r))
}
func handleHello(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
log.Printf("handleHello %s", r.Host)
log.Println(r.URL)
log.Println(r.Form.Get("Lorem"))
log.Println(r.Form)
fmt.Fprint(w, "<html><body><p>Hello World</p></body></html>")
}
func handleDNSUpdate(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ci, err := service.CreateClientInfoFromForm(r.Form)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "OK")
fmt.Fprintln(w, ci)
log.Println(ci)
log.Println(ci.IPv6Net.IP)
log.Println(ci.IPv6Net.Mask)
}