2024-01-01 21:26:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-01-01 22:31:18 +00:00
|
|
|
"io"
|
2024-01-01 21:26:36 +00:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.yetaga.in/alazyreader/library/media"
|
|
|
|
"tailscale.com/client/tailscale"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Router struct {
|
2024-01-02 01:38:42 +00:00
|
|
|
static fs.FS
|
|
|
|
lib Library
|
|
|
|
rcol RecordCollection
|
2024-01-03 01:52:21 +00:00
|
|
|
query Query
|
2024-01-02 01:38:42 +00:00
|
|
|
ts *tailscale.LocalClient
|
|
|
|
isAdmin bool
|
2024-01-01 21:26:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 01:38:42 +00:00
|
|
|
type path map[string]func()
|
2024-01-01 22:31:18 +00:00
|
|
|
|
2024-01-02 01:38:42 +00:00
|
|
|
func (h path) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if f, ok := h[r.Method]; ok {
|
2024-01-02 01:10:58 +00:00
|
|
|
f()
|
|
|
|
return
|
2024-01-01 22:31:18 +00:00
|
|
|
}
|
2024-01-03 01:53:59 +00:00
|
|
|
writeJSONerror(w, "method not supported", http.StatusMethodNotAllowed)
|
2024-01-01 22:31:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 22:52:11 +00:00
|
|
|
func writeJSONerror(w http.ResponseWriter, err string, status int) {
|
|
|
|
writeJSON(w, struct{ Status, Reason string }{Status: "error", Reason: err}, status)
|
2024-01-01 21:26:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func writeJSON(w http.ResponseWriter, b any, status int) {
|
|
|
|
bytes, err := json.Marshal(b)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-01 21:26:36 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.WriteHeader(status)
|
2024-01-01 22:31:18 +00:00
|
|
|
w.Write(bytes)
|
2024-01-01 21:26:36 +00:00
|
|
|
w.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
2024-01-02 01:38:42 +00:00
|
|
|
case "/api/mode":
|
|
|
|
path{
|
|
|
|
http.MethodGet: func() {
|
|
|
|
writeJSON(w, struct{ Admin bool }{Admin: router.isAdmin}, http.StatusOK)
|
|
|
|
},
|
|
|
|
}.ServeHTTP(w, r)
|
|
|
|
case "/api/whoami":
|
|
|
|
if !router.isAdmin {
|
|
|
|
http.NotFoundHandler().ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path{
|
|
|
|
http.MethodGet: func() { getWhoAmI(router.ts, w, r) },
|
|
|
|
}.ServeHTTP(w, r)
|
2024-01-01 22:31:18 +00:00
|
|
|
case "/api/records":
|
2024-01-02 01:38:42 +00:00
|
|
|
path{
|
2024-01-02 01:10:58 +00:00
|
|
|
http.MethodGet: func() { getRecords(router.rcol, w, r) },
|
2024-01-02 01:38:42 +00:00
|
|
|
}.ServeHTTP(w, r)
|
2024-01-01 22:31:18 +00:00
|
|
|
case "/api/books":
|
2024-01-02 01:38:42 +00:00
|
|
|
p := path{
|
2024-01-02 01:10:58 +00:00
|
|
|
http.MethodGet: func() { getBooks(router.lib, w, r) },
|
2024-01-02 01:38:42 +00:00
|
|
|
}
|
|
|
|
if router.isAdmin {
|
|
|
|
p[http.MethodPost] = func() { addBook(router.lib, w, r) }
|
|
|
|
p[http.MethodDelete] = func() { deleteBook(router.lib, w, r) }
|
|
|
|
}
|
|
|
|
p.ServeHTTP(w, r)
|
2024-01-03 01:52:21 +00:00
|
|
|
case "/api/query":
|
|
|
|
if !router.isAdmin {
|
|
|
|
http.NotFoundHandler().ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path{
|
|
|
|
http.MethodPost: func() { lookupBook(router.query, w, r) },
|
|
|
|
}.ServeHTTP(w, r)
|
2024-01-01 22:31:18 +00:00
|
|
|
default:
|
|
|
|
static(router.static).ServeHTTP(w, r)
|
2024-01-01 21:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func getBooks(l Library, w http.ResponseWriter, r *http.Request) {
|
|
|
|
books, err := l.GetAllBooks(r.Context())
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSON(w, books, http.StatusOK)
|
|
|
|
}
|
2024-01-01 21:26:36 +00:00
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func addBook(l Library, w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Body == nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "no body provided", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error reading body", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
book := &media.Book{}
|
|
|
|
err = json.Unmarshal(b, book)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = l.AddBook(r.Context(), book)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-01 22:52:11 +00:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2024-01-01 21:26:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func deleteBook(l Library, w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Body == nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "no body provided", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error reading body", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
book := &media.Book{}
|
|
|
|
err = json.Unmarshal(b, book)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error parsing body", http.StatusBadRequest)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = l.DeleteBook(r.Context(), book)
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, "error deleting book", http.StatusInternalServerError)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-01 22:52:11 +00:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2024-01-01 22:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRecords(l RecordCollection, w http.ResponseWriter, r *http.Request) {
|
|
|
|
records, err := l.GetAllRecords(r.Context())
|
|
|
|
if err != nil {
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSON(w, records, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWhoAmI(ts *tailscale.LocalClient, w http.ResponseWriter, r *http.Request) {
|
2024-01-01 22:52:11 +00:00
|
|
|
whois, err := ts.WhoIs(r.Context(), r.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
2024-01-01 22:31:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-01 22:52:11 +00:00
|
|
|
writeJSON(w, whois.UserProfile, http.StatusOK)
|
2024-01-01 21:26:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 01:52:21 +00:00
|
|
|
func lookupBook(query Query, w http.ResponseWriter, r *http.Request) {
|
|
|
|
isbn := r.FormValue("isbn")
|
|
|
|
if len(isbn) != 10 && len(isbn) != 13 {
|
|
|
|
writeJSONerror(w, "invalid isbn", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
book, err := query.GetByISBN(isbn)
|
|
|
|
if err != nil {
|
|
|
|
writeJSONerror(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSON(w, book, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:31:18 +00:00
|
|
|
func static(f fs.FS) http.Handler {
|
2024-01-01 21:26:36 +00:00
|
|
|
return http.FileServer(http.FS(f))
|
|
|
|
}
|