diff --git a/.gitignore b/.gitignore index 0f649f2..7edbd02 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /manager *.properties .DS_Store -*.csv \ No newline at end of file +*.csv +/vendor \ No newline at end of file diff --git a/cmd/serve/main.go b/cmd/serve/main.go index 119b990..2a15152 100644 --- a/cmd/serve/main.go +++ b/cmd/serve/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "strings" + "time" "git.yetaga.in/alazyreader/library/config" "git.yetaga.in/alazyreader/library/database" @@ -22,24 +23,44 @@ func max(a, b int) int { return b } +func obscureStr(in string, l int) string { + return in[0:max(l, len(in))] + strings.Repeat("*", max(0, len(in)-l)) +} + type Library interface { GetAllBooks(context.Context) ([]media.Book, error) } +type RecordCollection interface { + GetAllRecords(context.Context) ([]media.Record, error) +} + type Router struct { static fs.FS lib Library + rcol RecordCollection +} + +func writeJSON(w http.ResponseWriter, b []byte, status int) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(status) + w.Write(b) + w.Write([]byte("\n")) } func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { - if req.URL.Path == "/api" { - APIHandler(r.lib).ServeHTTP(w, req) + if req.URL.Path == "/api/records" { + RecordsAPIHandler(r.rcol).ServeHTTP(w, req) + return + } + if req.URL.Path == "/api/books" { + BooksAPIHandler(r.lib).ServeHTTP(w, req) return } StaticHandler(r.static).ServeHTTP(w, req) } -func APIHandler(l Library) http.Handler { +func BooksAPIHandler(l Library) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { books, err := l.GetAllBooks(r.Context()) if err != nil { @@ -50,11 +71,25 @@ func APIHandler(l Library) http.Handler { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return + } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - w.Write(b) - w.Write([]byte("\n")) + writeJSON(w, b, http.StatusOK) + }) +} + +func RecordsAPIHandler(l RecordCollection) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + books, err := l.GetAllRecords(r.Context()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + b, err := json.Marshal(books) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + writeJSON(w, b, http.StatusOK) }) } @@ -73,8 +108,11 @@ func main() { log.Fatalln(err) } if c.DBUser == "" || c.DBPass == "" || c.DBHost == "" || c.DBPort == "" || c.DBName == "" { - if c.DBPass != "" { // obscure password - c.DBPass = c.DBPass[0:max(3, len(c.DBPass))] + strings.Repeat("*", max(0, len(c.DBPass)-3)) + if c.DBPass != "" { + c.DBPass = obscureStr(c.DBPass, 3) + } + if c.DiscogsToken != "" { + c.DiscogsToken = obscureStr(c.DiscogsToken, 3) } log.Fatalf("vars: %+v", c) } @@ -91,10 +129,16 @@ func main() { log.Fatalln(err) } log.Printf("latest migration: %d; migrations run: %d", latest, run) + discogsCache, err := database.NewDiscogsCache(c.DiscogsToken, time.Hour*24, "delta.mu.alpha") + if err != nil { + log.Fatalln(err) + } + go discogsCache.FlushCache(context.Background()) r := &Router{ static: f, lib: lib, + rcol: discogsCache, } - log.Println("Listening on http://localhost:8080/") + log.Println("Listening on http://0.0.0.0:8080/") log.Fatalln(http.ListenAndServe(":8080", r)) } diff --git a/config/config.go b/config/config.go index 0813444..3da0fee 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,11 @@ package config type Config struct { - DBUser string - DBPass string - DBHost string - DBPort string - DBName string - Debug bool + DBUser string + DBPass string + DBHost string + DBPort string + DBName string + DiscogsToken string + Debug bool } diff --git a/database/mysql.go b/database/mysql.go index 8b769e4..0bd2a40 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -49,15 +49,15 @@ func (m *MySQL) PrepareDatabase(ctx context.Context) error { return fmt.Errorf("uninitialized mysql client") } - tablecheck := `SELECT count(*) AS count + tablecheck := fmt.Sprintf(`SELECT count(*) AS count FROM information_schema.TABLES - WHERE TABLE_NAME = '` + m.versionTable + `' - AND TABLE_SCHEMA in (SELECT DATABASE());` - tableschema := `CREATE TABLE ` + m.versionTable + `( + WHERE TABLE_NAME = '%s' + AND TABLE_SCHEMA in (SELECT DATABASE());`, m.versionTable) + tableschema := fmt.Sprintf(`CREATE TABLE %s ( id INT NOT NULL, name VARCHAR(100) NOT NULL, datetime DATE, - PRIMARY KEY (id))` + PRIMARY KEY (id))`, m.versionTable) var versionTableExists int m.connection.QueryRowContext(ctx, tablecheck).Scan(&versionTableExists) @@ -73,8 +73,9 @@ func (m *MySQL) GetLatestMigration(ctx context.Context) (int, error) { return 0, fmt.Errorf("uninitialized mysql client") } + migrationCheck := fmt.Sprintf("SELECT COALESCE(MAX(id), 0) FROM %s", m.versionTable) var latestMigration int - err := m.connection.QueryRowContext(ctx, "SELECT COALESCE(MAX(id), 0) FROM "+m.versionTable).Scan(&latestMigration) + err := m.connection.QueryRowContext(ctx, migrationCheck).Scan(&latestMigration) return latestMigration, err } @@ -97,6 +98,9 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) { } mig.id, mig.name = id, name mig.content, err = fs.ReadFile(migrationsFS, m.migrationsDirectory+"/"+dir[f].Name()) + if err != nil { + return 0, 0, fmt.Errorf("failure loading migration: %w", err) + } migrations[mig.id] = mig } } @@ -116,6 +120,7 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) { if err != nil { return latestMigrationRan, 0, err } + migrationLogSql := fmt.Sprintf("INSERT INTO %s (id, name, datetime) VALUES (?, ?, ?)", m.versionTable) migrationsRun := 0 for migrationsToRun := true; migrationsToRun; _, migrationsToRun = migrations[latestMigrationRan+1] { mig := migrations[latestMigrationRan+1] @@ -127,7 +132,7 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) { } return latestMigrationRan, migrationsRun, err } - _, err = tx.ExecContext(ctx, "INSERT INTO "+m.versionTable+" (id, name, datetime) VALUES (?, ?, ?)", mig.id, mig.name, time.Now()) + _, err = tx.ExecContext(ctx, migrationLogSql, mig.id, mig.name, time.Now()) if err != nil { nestederr := tx.Rollback() if nestederr != nil { @@ -147,26 +152,14 @@ func (m *MySQL) GetAllBooks(ctx context.Context) ([]media.Book, error) { return nil, fmt.Errorf("uninitialized mysql client") } + allBooksQuery := fmt.Sprintf(`SELECT + id, title, authors, sortauthor, isbn10, isbn13, format, + genre, publisher, series, volume, year, signed, + description, notes, onloan, coverurl + FROM %s`, m.tableName) + books := []media.Book{} - rows, err := m.connection.QueryContext(ctx, ` - SELECT id, - title, - authors, - sortauthor, - isbn10, - isbn13, - format, - genre, - publisher, - series, - volume, - year, - signed, - description, - notes, - onloan, - coverurl - FROM `+m.tableName) + rows, err := m.connection.QueryContext(ctx, allBooksQuery) if err != nil { return nil, err } diff --git a/database/records.go b/database/records.go new file mode 100644 index 0000000..46aac98 --- /dev/null +++ b/database/records.go @@ -0,0 +1,112 @@ +package database + +import ( + "context" + "fmt" + "log" + "strconv" + "sync" + "time" + + "git.yetaga.in/alazyreader/library/media" + "github.com/irlndts/go-discogs" +) + +type DiscogsCache struct { + authToken string + m sync.Mutex + cache []media.Record + maxCacheAge time.Duration + lastRefresh time.Time + client discogs.Discogs + username string +} + +func NewDiscogsCache(token string, maxCacheAge time.Duration, username string) (*DiscogsCache, error) { + client, err := discogs.New(&discogs.Options{ + UserAgent: "library.yetaga.in personal collection cache", + Token: token, + }) + if err != nil { + return nil, err + } + return &DiscogsCache{ + authToken: token, + client: client, + maxCacheAge: maxCacheAge, + username: username, + }, nil +} + +func (c *DiscogsCache) FlushCache(ctx context.Context) error { + c.m.Lock() + defer c.m.Unlock() + records, err := c.fetchRecords(ctx, nil) + c.cache = records + c.lastRefresh = time.Now() + return err +} + +func (c *DiscogsCache) GetAllRecords(ctx context.Context) ([]media.Record, error) { + c.m.Lock() + defer c.m.Unlock() + if time.Now().After(c.lastRefresh.Add(c.maxCacheAge)) { + records, err := c.fetchRecords(ctx, nil) + c.cache = records + c.lastRefresh = time.Now() + return c.cache, err + } + return c.cache, nil +} + +func (c *DiscogsCache) fetchRecords(ctx context.Context, pagination *discogs.Pagination) ([]media.Record, error) { + records := []media.Record{} + if pagination == nil { + pagination = getPagination(1) + } + log.Printf("calling discogs API, page %v", pagination.Page) + coll, err := c.client.CollectionItemsByFolder(c.username, 0, pagination) + if err != nil { + return records, fmt.Errorf("error loading collection: %w", err) + } + log.Printf("length: %v, first item in list: %s", len(coll.Items), coll.Items[0].BasicInformation.Title) + for i := range coll.Items { + records = append(records, collectionItemToRecord(&coll.Items[i])) + } + // recurse down the list + if coll.Pagination.URLs.Next != "" { + coll, err := c.fetchRecords(ctx, getPagination(pagination.Page+1)) + if err != nil { + return records, err + } + records = append(records, coll...) + } + return records, nil +} + +func getPagination(page int) *discogs.Pagination { + return &discogs.Pagination{Page: page, Sort: "added", SortOrder: "asc", PerPage: 100} +} + +func collectionItemToRecord(item *discogs.CollectionItemSource) media.Record { + artists := []string{} + for _, artist := range item.BasicInformation.Artists { + artists = append(artists, artist.Name) + } + year := strconv.Itoa(item.BasicInformation.Year) + if year == "0" { + year = "" + } + return media.Record{ + ID: item.ID, + AlbumName: item.BasicInformation.Title, + Artists: artists, + Identifier: item.BasicInformation.Labels[0].Catno, + Format: item.BasicInformation.Formats[0].Name, + Genre: item.BasicInformation.Genres[0], + Label: item.BasicInformation.Labels[0].Name, + Year: year, + CoverURL: item.BasicInformation.CoverImage, + DiscogsURL: fmt.Sprintf("https://www.discogs.com/release/%v", item.ID), + } +} diff --git a/frontend/files/app.js b/frontend/files/app.js index c43f19a..f218913 100644 --- a/frontend/files/app.js +++ b/frontend/files/app.js @@ -4,7 +4,7 @@ var sortState = { }; function init() { - fetch("/api") + fetch("/api/books") .then((response) => response.json()) .then((books) => { // prepare response diff --git a/frontend/files/index.html b/frontend/files/index.html index 5e0b3b1..d674e39 100644 --- a/frontend/files/index.html +++ b/frontend/files/index.html @@ -22,6 +22,7 @@
Name | +Artist(s) | +Label | +Identifier | +Year | +
---|