more backend
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2024-01-02 20:52:21 -05:00
parent 382d3bab61
commit 86f643ad0b
13 changed files with 349 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ type Router struct {
static fs.FS
lib Library
rcol RecordCollection
query Query
ts *tailscale.LocalClient
isAdmin bool
}
@@ -73,6 +74,14 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p[http.MethodDelete] = func() { deleteBook(router.lib, w, r) }
}
p.ServeHTTP(w, r)
case "/api/query":
if !router.isAdmin {
http.NotFoundHandler().ServeHTTP(w, r)
return
}
path{
http.MethodPost: func() { lookupBook(router.query, w, r) },
}.ServeHTTP(w, r)
default:
static(router.static).ServeHTTP(w, r)
}
@@ -159,6 +168,20 @@ func getWhoAmI(ts *tailscale.LocalClient, w http.ResponseWriter, r *http.Request
writeJSON(w, whois.UserProfile, http.StatusOK)
}
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)
}
func static(f fs.FS) http.Handler {
return http.FileServer(http.FS(f))
}

View File

@@ -15,6 +15,7 @@ import (
"git.yetaga.in/alazyreader/library/database"
"git.yetaga.in/alazyreader/library/frontend"
"git.yetaga.in/alazyreader/library/media"
"git.yetaga.in/alazyreader/library/query"
"github.com/kelseyhightower/envconfig"
"golang.org/x/sync/errgroup"
"tailscale.com/tsnet"
@@ -35,6 +36,10 @@ type RecordCollection interface {
GetAllRecords(context.Context) ([]media.Record, error)
}
type Query interface {
GetByISBN(string) (*media.Book, error)
}
func main() {
var c config.Config
must.Do(envconfig.Process("library", &c))
@@ -55,6 +60,8 @@ func main() {
c.DiscogsToken, time.Hour*24, c.DiscogsUser, c.DiscogsPersist, c.DiscogsCacheFile,
))
queryProvider := &query.GoogleBooks{}
staticRoot := must.Get(frontend.Root())
servers := make(chan (*http.Server), 3)
@@ -74,6 +81,7 @@ func main() {
static: staticRoot,
lib: lib,
rcol: discogsCache,
query: queryProvider,
isAdmin: true,
}))
})
@@ -137,11 +145,11 @@ func shutdown(servers chan (*http.Server)) error {
func publicServer(port int, handler http.Handler) (*http.Server, net.Listener, error) {
server := &http.Server{Handler: handler}
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", 8080))
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, nil, err
}
log.Println("starting public server")
log.Printf("public server: http://0.0.0.0:%d/", port)
return server, ln, nil
}
@@ -163,6 +171,7 @@ func tailscaleListener(hostname string, handler *Router) (*http.Server, net.List
if err != nil {
return nil, nil, err
}
log.Printf("management server: http://%s/", hostname)
server := &http.Server{Handler: handler}
return server, ln, nil
}