dyndns/webapi/update.go
Torben Nehmer fbf10f9d1b Next implementation steps
- Simplified UpdateRequest to the minimum required
- renamed test user to example
- removed user property from yml config, it is given by the file name.
- splitted server code analogous to command line code so that each handler has its own file.
- made viper confgi in userconfig internal
- added validation to userconfig
- added helper to combaine an IID with a v6net in userconfig
2021-08-23 20:41:32 +02:00

59 lines
1.4 KiB
Go

package webapi
import (
"fmt"
"log"
"net/http"
"gitea.nehmer.net/torben/dyndns/service"
)
func init() {
router.HandleFunc("/update", handleUpdate)
}
func handleUpdate(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ur, err := createUpdateRequestFromForm(r.Form)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
uc, err := service.LoadConfigForUser(ur.UserName, ur.Password)
if err != nil {
if _, ok := err.(*service.UnauthorizedError); ok {
http.Error(w, err.Error(), http.StatusUnauthorized)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Printf("Authorization failed: %v", err)
return
}
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "OK")
fmt.Fprintln(w, ur.PrettyPrint())
log.Println(ur.PrettyPrint())
log.Println(ur.IPv6Net.IP)
log.Println(ur.IPv6Net.Mask)
log.Printf("Request PW: %s, Config PW: %s", ur.Password, uc.PassWord)
fmt.Fprintln(w, "Unmarshalled userconfig:")
fmt.Fprintln(w, uc.PrettyPrint())
fmt.Fprintln(w, "WebAPI Config:")
fmt.Fprintln(w, C.PrettyPrint())
fmt.Fprintln(w, "Service Config:")
fmt.Fprintln(w, service.C.PrettyPrint())
for _, other := range uc.Others {
log.Printf("Other %s, IID %v, UR V6NEt: %v, merged: %v",
other.DNS, other.V6IID, ur.IPv6Net,
other.ConvertIIDToAddress(ur.IPv6Net))
}
}