This commit is contained in:
parent
fa73364107
commit
e62006f4e1
@ -45,22 +45,14 @@ func (h handler) Handle(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeError[T any](t T, err error) func(w http.ResponseWriter) (T, bool) {
|
func writeJSONerror(w http.ResponseWriter, err string, status int) {
|
||||||
return func(w http.ResponseWriter) (T, bool) {
|
writeJSON(w, struct{ Status, Reason string }{Status: "error", Reason: err}, status)
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
return t, err == nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeNoBody(w http.ResponseWriter, status int) {
|
|
||||||
w.WriteHeader(status)
|
|
||||||
}
|
|
||||||
func writeJSON(w http.ResponseWriter, b any, status int) {
|
func writeJSON(w http.ResponseWriter, b any, status int) {
|
||||||
bytes, err := json.Marshal(b)
|
bytes, err := json.Marshal(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
@ -102,15 +94,13 @@ func (router *AdminRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func badMethod(w http.ResponseWriter) {
|
func badMethod(w http.ResponseWriter) {
|
||||||
writeJSON(w,
|
writeJSONerror(w, "method not supported", http.StatusMethodNotAllowed)
|
||||||
struct{ Error string }{Error: "method not supported"},
|
|
||||||
http.StatusMethodNotAllowed)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBooks(l Library, w http.ResponseWriter, r *http.Request) {
|
func getBooks(l Library, w http.ResponseWriter, r *http.Request) {
|
||||||
books, err := l.GetAllBooks(r.Context())
|
books, err := l.GetAllBooks(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSON(w, books, http.StatusOK)
|
writeJSON(w, books, http.StatusOK)
|
||||||
@ -118,77 +108,70 @@ func getBooks(l Library, w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func addBook(l Library, w http.ResponseWriter, r *http.Request) {
|
func addBook(l Library, w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
http.Error(w, "no body provided", http.StatusBadRequest)
|
writeJSONerror(w, "no body provided", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
b, err := io.ReadAll(r.Body)
|
b, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error reading body", http.StatusBadRequest)
|
writeJSONerror(w, "error reading body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
book := &media.Book{}
|
book := &media.Book{}
|
||||||
err = json.Unmarshal(b, book)
|
err = json.Unmarshal(b, book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error parsing body", http.StatusBadRequest)
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = l.AddBook(r.Context(), book)
|
err = l.AddBook(r.Context(), book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error parsing body", http.StatusBadRequest)
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeNoBody(w, http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteBook(l Library, w http.ResponseWriter, r *http.Request) {
|
func deleteBook(l Library, w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
http.Error(w, "no body provided", http.StatusBadRequest)
|
writeJSONerror(w, "no body provided", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
b, err := io.ReadAll(r.Body)
|
b, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error reading body", http.StatusBadRequest)
|
writeJSONerror(w, "error reading body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
book := &media.Book{}
|
book := &media.Book{}
|
||||||
err = json.Unmarshal(b, book)
|
err = json.Unmarshal(b, book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error parsing body", http.StatusBadRequest)
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = l.DeleteBook(r.Context(), book)
|
err = l.DeleteBook(r.Context(), book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error parsing body", http.StatusBadRequest)
|
writeJSONerror(w, "error deleting book", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeNoBody(w, http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecords(l RecordCollection, w http.ResponseWriter, r *http.Request) {
|
func getRecords(l RecordCollection, w http.ResponseWriter, r *http.Request) {
|
||||||
records, err := l.GetAllRecords(r.Context())
|
records, err := l.GetAllRecords(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSON(w, records, http.StatusOK)
|
writeJSON(w, records, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWhoAmI(ts *tailscale.LocalClient, w http.ResponseWriter, r *http.Request) {
|
func getWhoAmI(ts *tailscale.LocalClient, w http.ResponseWriter, r *http.Request) {
|
||||||
whois, ok := writeError(ts.WhoIs(r.Context(), r.RemoteAddr))(w)
|
whois, err := ts.WhoIs(r.Context(), r.RemoteAddr)
|
||||||
if !ok {
|
if err != nil {
|
||||||
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSON(w, struct {
|
writeJSON(w, whois.UserProfile, http.StatusOK)
|
||||||
Username string `json:"Username"`
|
|
||||||
DisplayName string `json:"DisplayName"`
|
|
||||||
ProfilePicURL string `json:"ProfilePicURL"`
|
|
||||||
}{
|
|
||||||
Username: whois.UserProfile.LoginName,
|
|
||||||
DisplayName: whois.UserProfile.DisplayName,
|
|
||||||
ProfilePicURL: whois.UserProfile.ProfilePicURL,
|
|
||||||
}, http.StatusOK)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func static(f fs.FS) http.Handler {
|
func static(f fs.FS) http.Handler {
|
||||||
|
Loading…
Reference in New Issue
Block a user