recipes2/cmd/parser/main.go

81 lines
1.7 KiB
Go
Raw Normal View History

2024-01-19 21:00:03 +02:00
package main
2024-01-21 12:52:21 +02:00
import (
"context"
"log/slog"
"os"
"recipes/internal/config"
2024-01-23 12:22:16 +02:00
"recipes/internal/lib/logger/sl"
2024-01-21 12:52:21 +02:00
"recipes/internal/media_storage/minio"
"recipes/internal/parser"
"recipes/internal/storage/postgresql"
prettyLogger "github.com/charmbracelet/log"
)
const (
envLocal = "local"
envDev = "dev"
envProd = "prod"
)
func main() {
// read config
cfg := config.MustLoad()
// init logger
log := setupLogger(cfg.Env)
// init storage
storage, err := postgresql.New(
context.Background(),
cfg.Postgresql.User,
cfg.Postgresql.Password,
cfg.Postgresql.Address,
cfg.Postgresql.DBName,
)
if err != nil {
2024-01-23 12:22:16 +02:00
log.Error("failed to init storage", sl.Err(err))
2024-01-21 12:52:21 +02:00
os.Exit(1)
}
// init media storage
mstorage, err := minio.New(
context.Background(),
cfg.Minio.Address,
cfg.Minio.User,
cfg.Minio.Password,
)
if err != nil {
2024-01-23 12:22:16 +02:00
log.Error("failed to init media storage", sl.Err(err))
2024-01-21 12:52:21 +02:00
os.Exit(1)
}
// run parser
_, err = parser.SavePage(log, 1, mstorage, storage, storage)
if err != nil {
2024-01-23 12:22:16 +02:00
log.Error("Parse failed", sl.Err(err))
2024-01-21 12:52:21 +02:00
os.Exit(1)
}
log.Info("parsing was completed successfully")
}
func setupLogger(env string) *slog.Logger {
var log *slog.Logger
switch env {
case envLocal:
// log = slog.New(
// slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
// )
handler := prettyLogger.NewWithOptions(os.Stdout, prettyLogger.Options{Level: prettyLogger.DebugLevel})
log = slog.New(handler)
case envDev:
log = slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}),
)
case envProd:
log = slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
)
}
return log
}