2021-08-15 19:37:11 +00:00
|
|
|
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)
|
2021-08-22 12:36:55 +00:00
|
|
|
r.HandleFunc("/update", handleUpdate)
|
2021-08-15 19:37:11 +00:00
|
|
|
|
|
|
|
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>")
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:36:55 +00:00
|
|
|
func handleUpdate(w http.ResponseWriter, r *http.Request) {
|
2021-08-15 19:37:11 +00:00
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:34:45 +00:00
|
|
|
ur, err := createUpdateRequestFromForm(r.Form)
|
2021-08-15 19:37:11 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-22 12:36:55 +00:00
|
|
|
uc, err := service.LoadConfigForUser(ur.UserName, ur.Password)
|
2021-08-21 18:34:45 +00:00
|
|
|
if err != nil {
|
2021-08-22 12:36:55 +00:00
|
|
|
if _, ok := err.(*service.UnauthorizedError); ok {
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
} else {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2021-08-21 18:34:45 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-22 12:36:55 +00:00
|
|
|
v := uc.DB
|
2021-08-21 18:34:45 +00:00
|
|
|
|
2021-08-15 19:37:11 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
fmt.Fprintln(w, "OK")
|
2021-08-21 18:34:45 +00:00
|
|
|
fmt.Fprintln(w, ur.PrettyPrint())
|
|
|
|
log.Println(ur.PrettyPrint())
|
|
|
|
log.Println(ur.IPv6Net.IP)
|
|
|
|
log.Println(ur.IPv6Net.Mask)
|
|
|
|
log.Println(v.AllSettings())
|
|
|
|
log.Printf("Request PW: %s, Config PW: %s", ur.Password, v.GetString("password"))
|
2021-08-22 12:36:55 +00:00
|
|
|
fmt.Fprintln(w, "Unmarshalled userconfig:")
|
|
|
|
fmt.Fprintln(w, uc.PrettyPrint())
|
2021-08-15 19:37:11 +00:00
|
|
|
}
|