library/cmd/serve/main.go

145 lines
3.3 KiB
Go
Raw Normal View History

2021-06-26 18:08:35 +00:00
package main
import (
2021-07-02 22:13:58 +00:00
"context"
"encoding/json"
2021-06-26 18:08:35 +00:00
"io/fs"
2021-07-02 22:13:58 +00:00
"log"
2021-06-26 18:08:35 +00:00
"net/http"
2021-08-08 20:56:45 +00:00
"strings"
2022-03-13 22:04:09 +00:00
"time"
2021-06-26 18:08:35 +00:00
2021-07-03 00:46:58 +00:00
"git.yetaga.in/alazyreader/library/config"
2021-07-02 22:13:58 +00:00
"git.yetaga.in/alazyreader/library/database"
2021-06-26 18:08:35 +00:00
"git.yetaga.in/alazyreader/library/frontend"
2022-03-05 15:58:15 +00:00
"git.yetaga.in/alazyreader/library/media"
2021-07-02 22:47:14 +00:00
"github.com/kelseyhightower/envconfig"
2021-06-26 18:08:35 +00:00
)
2021-08-08 20:56:45 +00:00
func max(a, b int) int {
if a > b {
return a
}
return b
}
2022-03-13 22:04:09 +00:00
func obscureStr(in string, l int) string {
return in[0:max(l, len(in))] + strings.Repeat("*", max(0, len(in)-l))
}
2021-07-02 22:13:58 +00:00
type Library interface {
2022-03-05 15:58:15 +00:00
GetAllBooks(context.Context) ([]media.Book, error)
2021-07-02 22:13:58 +00:00
}
2022-03-13 22:04:09 +00:00
type RecordCollection interface {
GetAllRecords(context.Context) ([]media.Record, error)
}
2021-07-02 22:13:58 +00:00
type Router struct {
static fs.FS
lib Library
2022-03-13 22:04:09 +00:00
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"))
2021-07-02 22:13:58 +00:00
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2022-03-13 22:04:09 +00:00
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)
2021-07-02 22:13:58 +00:00
return
}
StaticHandler(r.static).ServeHTTP(w, req)
}
2022-03-13 22:04:09 +00:00
func BooksAPIHandler(l Library) http.Handler {
2021-07-02 22:13:58 +00:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
books, err := l.GetAllBooks(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b, err := json.Marshal(books)
2022-03-13 22:04:09 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
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)
2021-07-02 22:13:58 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2022-03-13 22:04:09 +00:00
writeJSON(w, b, http.StatusOK)
2021-07-02 22:13:58 +00:00
})
}
func StaticHandler(f fs.FS) http.Handler {
return http.FileServer(http.FS(f))
}
2021-06-26 18:08:35 +00:00
func main() {
2021-07-03 00:46:58 +00:00
var c config.Config
2021-07-02 22:47:14 +00:00
err := envconfig.Process("library", &c)
if err != nil {
log.Fatalln(err)
}
2021-07-02 22:13:58 +00:00
f, err := frontend.Root()
if err != nil {
log.Fatalln(err)
}
2021-07-02 22:47:14 +00:00
if c.DBUser == "" || c.DBPass == "" || c.DBHost == "" || c.DBPort == "" || c.DBName == "" {
2022-03-13 22:04:09 +00:00
if c.DBPass != "" {
c.DBPass = obscureStr(c.DBPass, 3)
}
if c.Discogs_Token != "" {
c.Discogs_Token = obscureStr(c.Discogs_Token, 3)
2021-08-08 20:56:45 +00:00
}
2021-07-02 22:47:14 +00:00
log.Fatalf("vars: %+v", c)
}
lib, err := database.NewMySQLConnection(c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName)
2021-07-02 22:13:58 +00:00
if err != nil {
log.Fatalln(err)
}
err = lib.PrepareDatabase(context.Background())
if err != nil {
log.Fatalln(err)
}
latest, run, err := lib.RunMigrations(context.Background())
if err != nil {
log.Fatalln(err)
}
log.Printf("latest migration: %d; migrations run: %d", latest, run)
2022-03-13 22:04:09 +00:00
discogsCache, err := database.NewDiscogsCache(c.Discogs_Token, time.Hour*24, "delta.mu.alpha")
if err != nil {
log.Fatalln(err)
}
go discogsCache.FlushCache(context.Background())
2021-07-02 22:13:58 +00:00
r := &Router{
static: f,
lib: lib,
2022-03-13 22:04:09 +00:00
rcol: discogsCache,
2021-07-02 22:13:58 +00:00
}
2022-03-13 22:04:09 +00:00
log.Println("Listening on http://0.0.0.0:8080/")
2021-07-02 22:13:58 +00:00
log.Fatalln(http.ListenAndServe(":8080", r))
2021-06-26 18:08:35 +00:00
}