library/cmd/serve/main.go

171 lines
4.2 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"
"fmt"
2021-07-02 22:13:58 +00:00
"log"
"net"
2021-06-26 18:08:35 +00:00
"net/http"
"os"
"os/signal"
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"
"git.yetaga.in/alazyreader/library/query"
2021-07-02 22:47:14 +00:00
"github.com/kelseyhightower/envconfig"
"golang.org/x/sync/errgroup"
"tailscale.com/tsnet"
"tailscale.com/util/must"
2021-06-26 18:08:35 +00:00
)
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)
AddBook(context.Context, *media.Book) error
DeleteBook(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)
}
type Query interface {
GetByISBN(string) (*media.Book, error)
2022-03-13 22:04:09 +00:00
}
func main() {
var c config.Config
must.Do(envconfig.Process("library", &c))
2021-07-02 22:13:58 +00:00
var lib Library
if c.DBType == "memory" {
lib = &database.Memory{}
} else if c.DBType == "sql" {
sqllib, latest, run, err := setupSQL(c)
2021-07-02 22:13:58 +00:00
if err != nil {
log.Fatalf("sql connection err: %v", err)
2021-07-02 22:13:58 +00:00
}
log.Printf("latest migration: %d; migrations run: %d", latest, run)
lib = sqllib
}
discogsCache := must.Get(database.NewDiscogsCache(
c.DiscogsToken, time.Hour*24, c.DiscogsUser, c.DiscogsPersist, c.DiscogsCacheFile,
))
queryProvider := &query.GoogleBooks{}
staticRoot := must.Get(frontend.Root())
2022-03-13 22:04:09 +00:00
servers := make(chan (*http.Server), 3)
errGroup := errgroup.Group{}
errGroup.Go(func() error {
return start(servers)(publicServer(8080, &Router{
static: staticRoot,
lib: lib,
rcol: discogsCache,
isAdmin: false,
}))
2022-03-13 22:04:09 +00:00
})
errGroup.Go(func() error {
return start(servers)(tailscaleListener("library-admin", &Router{
static: staticRoot,
lib: lib,
rcol: discogsCache,
query: queryProvider,
isAdmin: true,
}))
2021-07-02 22:13:58 +00:00
})
errGroup.Go(func() error {
return shutdown(servers)
})
log.Println(errGroup.Wait())
2021-07-02 22:13:58 +00:00
}
func setupSQL(c config.Config) (Library, int, int, error) {
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)
}
2022-04-02 17:10:16 +00:00
if c.DiscogsToken != "" {
c.DiscogsToken = obscureStr(c.DiscogsToken, 3)
2021-08-08 20:56:45 +00:00
}
return nil, 0, 0, fmt.Errorf("invalid config; vars provided: %+v", c)
2021-07-02 22:47:14 +00:00
}
sql, err := database.NewMySQLConnection(c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName)
2021-07-02 22:13:58 +00:00
if err != nil {
return nil, 0, 0, err
2021-07-02 22:13:58 +00:00
}
err = sql.PrepareDatabase(context.Background())
2021-07-02 22:13:58 +00:00
if err != nil {
return nil, 0, 0, err
}
latest, run, err := sql.RunMigrations(context.Background())
if err != nil {
return nil, 0, 0, err
}
return sql, latest, run, nil
}
func start(servers chan (*http.Server)) func(*http.Server, net.Listener, error) error {
return func(s *http.Server, l net.Listener, err error) error {
if err != nil {
return err
}
servers <- s
return s.Serve(l)
}
}
func shutdown(servers chan (*http.Server)) error {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
close(servers)
var err error
for server := range servers {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
if shutdownerr := server.Shutdown(ctx); shutdownerr != nil {
err = shutdownerr
}
cancel()
2021-07-02 22:13:58 +00:00
}
return err
}
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", port))
2021-07-02 22:13:58 +00:00
if err != nil {
return nil, nil, err
2021-07-02 22:13:58 +00:00
}
log.Printf("public server: http://0.0.0.0:%d/", port)
return server, ln, nil
}
func tailscaleListener(hostname string, handler *Router) (*http.Server, net.Listener, error) {
s := &tsnet.Server{
Dir: ".config/" + hostname,
Hostname: hostname,
Logf: func(s string, a ...any) { // silence most tsnet logs
if strings.HasPrefix(s, "To start this tsnet server") {
log.Printf(s, a...)
}
},
}
ln, err := s.Listen("tcp", ":80")
2022-03-13 22:04:09 +00:00
if err != nil {
return nil, nil, err
2022-03-13 22:04:09 +00:00
}
handler.ts, err = s.LocalClient()
if err != nil {
return nil, nil, err
2021-07-02 22:13:58 +00:00
}
log.Printf("management server: http://%s/", hostname)
return &http.Server{Handler: handler}, ln, nil
2021-06-26 18:08:35 +00:00
}