eBay marketplaces are now polled through eBay's official Buy > Browse API (client-credentials OAuth2) instead of an Apify scraper actor; Apify still handles Yahoo JP and Mercari. Browse API calls are tracked per day in a new ebay_api_usage table and capped (default 5000, configurable) on eBay's Pacific-time reset clock, so polling halts before the limit is hit. Credentials live in config.toml [ebay] and are overridable via /settings, which also surfaces the day's running call count. Also carries the server.secure_cookies config plumbing (field, accessor, example) consumed by the following commit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
// Embed the timezone database so eBay's Pacific-time quota reset resolves
|
|
// correctly even on minimal hosts without system zoneinfo.
|
|
_ "time/tzdata"
|
|
|
|
"veola/internal/apify"
|
|
"veola/internal/auth"
|
|
"veola/internal/config"
|
|
"veola/internal/crypto"
|
|
"veola/internal/db"
|
|
"veola/internal/handlers"
|
|
"veola/internal/ntfy"
|
|
"veola/internal/scheduler"
|
|
)
|
|
|
|
func main() {
|
|
configPath := flag.String("config", "config.toml", "path to config TOML file")
|
|
flag.Parse()
|
|
|
|
if err := run(*configPath); err != nil {
|
|
slog.Error("fatal", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(configPath string) error {
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("config: %w", err)
|
|
}
|
|
|
|
key, err := crypto.DeriveKey([]byte(cfg.Security.EncryptionKey))
|
|
if err != nil {
|
|
return fmt.Errorf("derive key: %w", err)
|
|
}
|
|
|
|
sqlDB, err := db.Open(cfg.Server.DBPath)
|
|
if err != nil {
|
|
return fmt.Errorf("db open: %w", err)
|
|
}
|
|
defer sqlDB.Close()
|
|
|
|
store := db.NewStore(sqlDB, key)
|
|
authMgr, err := auth.NewManager(sqlDB, store, cfg.Security.SessionSecret, cfg.Server.UseSecureCookies())
|
|
if err != nil {
|
|
return fmt.Errorf("auth manager: %w", err)
|
|
}
|
|
|
|
apifyClient := apify.New(cfg.Apify.APIKey)
|
|
ntfyClient := ntfy.New(cfg.Ntfy.BaseURL)
|
|
sched := scheduler.New(cfg, store, apifyClient, ntfyClient)
|
|
|
|
startCtx, cancelStart := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancelStart()
|
|
if err := sched.Start(startCtx); err != nil {
|
|
return fmt.Errorf("scheduler start: %w", err)
|
|
}
|
|
|
|
app := handlers.New(cfg, store, authMgr, apifyClient, ntfyClient, sched)
|
|
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
|
srv := &http.Server{
|
|
Addr: addr,
|
|
Handler: app.Routes(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
}
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
slog.Info("listening", "addr", addr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
errCh <- err
|
|
}
|
|
close(errCh)
|
|
}()
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select {
|
|
case sig := <-sigCh:
|
|
slog.Info("shutting down", "signal", sig.String())
|
|
case err := <-errCh:
|
|
if err != nil {
|
|
return fmt.Errorf("http: %w", err)
|
|
}
|
|
}
|
|
|
|
shutdownCtx, cancelShutdown := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancelShutdown()
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
slog.Error("http shutdown", "err", err)
|
|
}
|
|
sched.Stop()
|
|
slog.Info("shutdown complete")
|
|
return nil
|
|
}
|