library/cmd/serve/main.go

162 lines
3.9 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"
2023-12-31 01:27:24 +00:00
"fmt"
2021-07-02 22:13:58 +00:00
"log"
2024-01-01 21:26:36 +00:00
"net"
2021-06-26 18:08:35 +00:00
"net/http"
2024-01-01 21:26:36 +00:00
"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"
2021-07-02 22:47:14 +00:00
"github.com/kelseyhightower/envconfig"
2023-12-31 01:27:24 +00:00
"golang.org/x/sync/errgroup"
"tailscale.com/tsnet"
2024-01-01 21:26:36 +00:00
"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)
2024-01-01 22:31:18 +00:00
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)
}
2024-01-01 21:26:36 +00:00
func main() {
var c config.Config
must.Do(envconfig.Process("library", &c))
2021-07-02 22:13:58 +00:00
2024-01-01 21:26:36 +00:00
var lib Library
var err error
if c.DBType == "memory" {
lib = &database.Memory{}
} else if c.DBType == "sql" {
var latest, run int
lib, latest, run, err = setupSQL(c)
if err != nil {
log.Fatalf("err starting sql connection: %v", err)
}
log.Printf("latest migration: %d; migrations run: %d", latest, run)
2021-07-02 22:13:58 +00:00
}
2024-01-01 21:26:36 +00:00
discogsCache := must.Get(database.NewDiscogsCache(
c.DiscogsToken, time.Hour*24, c.DiscogsUser, c.DiscogsPersist, c.DiscogsCacheFile,
))
2021-07-02 22:13:58 +00:00
2024-01-01 21:26:36 +00:00
frontendRoot := must.Get(frontend.Root())
adminRoot := must.Get(frontend.AdminRoot())
2023-12-31 01:27:24 +00:00
2024-01-01 22:31:18 +00:00
servers := make(chan (*http.Server), 3)
2024-01-01 21:26:36 +00:00
errGroup := errgroup.Group{}
2024-01-01 22:31:18 +00:00
errGroup.Go(func() error {
return start(servers)(
publicServer(8080, &Router{
static: frontendRoot,
lib: lib,
rcol: discogsCache,
}))
})
errGroup.Go(func() error {
return start(servers)(
tailscaleListener("library-admin", &AdminRouter{
static: adminRoot,
lib: lib,
}))
})
errGroup.Go(func() error {
return shutdown(servers)
})
2022-03-13 22:04:09 +00:00
2024-01-01 21:26:36 +00:00
log.Println(errGroup.Wait())
2022-03-13 22:04:09 +00:00
}
2024-01-01 21:26:36 +00:00
func setupSQL(c config.Config) (Library, int, int, error) {
if c.DBUser == "" || c.DBPass == "" || c.DBHost == "" || c.DBPort == "" || c.DBName == "" {
if c.DBPass != "" {
c.DBPass = obscureStr(c.DBPass, 3)
2022-03-13 22:04:09 +00:00
}
2024-01-01 21:26:36 +00:00
if c.DiscogsToken != "" {
c.DiscogsToken = obscureStr(c.DiscogsToken, 3)
2021-07-02 22:13:58 +00:00
}
2024-01-01 21:26:36 +00:00
return nil, 0, 0, fmt.Errorf("invalid config; vars provided: %+v", c)
}
sql, err := database.NewMySQLConnection(c.DBUser, c.DBPass, c.DBHost, c.DBPort, c.DBName)
2021-07-02 22:47:14 +00:00
if err != nil {
2024-01-01 21:26:36 +00:00
return nil, 0, 0, err
2021-07-02 22:47:14 +00:00
}
2024-01-01 21:26:36 +00:00
err = sql.PrepareDatabase(context.Background())
2021-07-02 22:13:58 +00:00
if err != nil {
2024-01-01 21:26:36 +00:00
return nil, 0, 0, err
2021-07-02 22:13:58 +00:00
}
2024-01-01 21:26:36 +00:00
latest, run, err := sql.RunMigrations(context.Background())
if err != nil {
return nil, 0, 0, err
}
return sql, latest, run, nil
}
2024-01-01 22:31:18 +00:00
func start(servers chan (*http.Server)) func(*http.Server, net.Listener, error) error {
return func(s *http.Server, l net.Listener, err error) error {
2023-12-31 01:27:24 +00:00
if err != nil {
2024-01-01 22:31:18 +00:00
return err
2021-08-08 20:56:45 +00:00
}
2024-01-01 21:26:36 +00:00
servers <- s
2024-01-01 22:31:18 +00:00
return s.Serve(l)
2021-07-02 22:47:14 +00:00
}
2024-01-01 21:26:36 +00:00
}
2024-01-01 22:31:18 +00:00
func shutdown(servers chan (*http.Server)) error {
2024-01-01 21:26:36 +00:00
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
close(servers)
2024-01-01 22:31:18 +00:00
var err error
2024-01-01 21:26:36 +00:00
for server := range servers {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
2024-01-01 22:31:18 +00:00
if shutdownerr := server.Shutdown(ctx); shutdownerr != nil {
err = shutdownerr
2024-01-01 21:26:36 +00:00
}
cancel()
2021-07-02 22:13:58 +00:00
}
2024-01-01 22:31:18 +00:00
return err
2023-12-31 01:27:24 +00:00
}
2024-01-01 21:26:36 +00:00
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))
if err != nil {
return nil, nil, err
}
2024-01-01 22:31:18 +00:00
log.Println("starting public server")
2024-01-01 21:26:36 +00:00
return server, ln, nil
2023-12-31 01:27:24 +00:00
}
2024-01-01 21:26:36 +00:00
func tailscaleListener(hostname string, handler *AdminRouter) (*http.Server, net.Listener, error) {
2023-12-31 01:27:24 +00:00
s := &tsnet.Server{
Dir: ".config/" + hostname,
Hostname: hostname,
2021-07-02 22:13:58 +00:00
}
2023-12-31 01:27:24 +00:00
ln, err := s.Listen("tcp", ":80")
2021-07-02 22:13:58 +00:00
if err != nil {
2024-01-01 21:26:36 +00:00
return nil, nil, err
2021-07-02 22:13:58 +00:00
}
2023-12-31 01:27:24 +00:00
handler.ts, err = s.LocalClient()
2022-03-13 22:04:09 +00:00
if err != nil {
2024-01-01 21:26:36 +00:00
return nil, nil, err
2021-07-02 22:13:58 +00:00
}
2024-01-01 21:26:36 +00:00
server := &http.Server{Handler: handler}
return server, ln, nil
2021-06-26 18:08:35 +00:00
}