apps/easel/main.go 2.1 K raw
1
package main
2
3
import (
4
	"context"
5
	"log"
6
	"log/slog"
7
	"net/http"
8
	"os"
9
	"strings"
10
	"time"
11
12
	"github.com/stevedylandev/andromeda/pkg/config"
13
	"github.com/stevedylandev/andromeda/pkg/sqlite"
14
)
15
16
func splitCommaTrim(s string) []string {
17
	out := []string{}
18
	for _, part := range strings.Split(s, ",") {
19
		if v := strings.TrimSpace(part); v != "" {
20
			out = append(out, v)
21
		}
22
	}
23
	return out
24
}
25
26
func main() {
27
	config.LoadDotEnv(".env")
28
	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
29
30
	dbPath := config.Getenv("EASEL_DB_PATH", "easel.sqlite")
31
	db, err := sqlite.Open(dbPath, easelSchema)
32
	if err != nil {
33
		log.Fatal(err)
34
	}
35
	defer db.Close()
36
37
	tzName := config.Getenv("EASEL_TIMEZONE", "UTC")
38
	loc, err := time.LoadLocation(tzName)
39
	if err != nil {
40
		logger.Warn("invalid EASEL_TIMEZONE, falling back to UTC", "value", tzName)
41
		loc = time.UTC
42
		tzName = "UTC"
43
	}
44
45
	classifications := splitCommaTrim(config.Getenv("EASEL_CLASSIFICATIONS", "painting"))
46
	if len(classifications) == 0 {
47
		log.Fatal("EASEL_CLASSIFICATIONS resolved to empty list")
48
	}
49
	excludeTerms := splitCommaTrim(config.Getenv("EASEL_EXCLUDE_TERMS", "erotic,erotica,shunga"))
50
51
	tmpl, err := buildTemplates()
52
	if err != nil {
53
		log.Fatal(err)
54
	}
55
	app := &App{
56
		DB:              db,
57
		Log:             logger,
58
		Templates:       tmpl,
59
		HTTP:            buildHTTPClient(),
60
		TZ:              loc,
61
		TZName:          tzName,
62
		Classifications: classifications,
63
		ExcludeTerms:    excludeTerms,
64
		BackfillDays:    config.GetenvInt("EASEL_BACKFILL_DAYS", 0),
65
		MaxDedupRetries: config.GetenvInt("EASEL_MAX_DEDUP_RETRIES", 10),
66
		BaseURL:         strings.TrimRight(config.Getenv("EASEL_BASE_URL", "http://localhost:4242"), "/"),
67
	}
68
	logger.Info("easel starting", "tz", tzName, "classifications", classifications, "exclude_terms", excludeTerms, "backfill_days", app.BackfillDays, "retries", app.MaxDedupRetries)
69
70
	go app.runScheduler(context.Background())
71
72
	addr := config.Getenv("HOST", "127.0.0.1") + ":" + config.Getenv("PORT", "4242")
73
	logger.Info("easel server running", "addr", addr)
74
	if err := http.ListenAndServe(addr, app.routes()); err != nil {
75
		log.Fatal(err)
76
	}
77
}