refactor
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
FROM golang:1.25-alpine AS build
|
||||
|
||||
WORKDIR /app
|
||||
WORKDIR /app/tg_parser
|
||||
|
||||
# Modules layer
|
||||
COPY go.mod go.sum ./
|
||||
COPY tg_parser/go.mod tg_parser/go.sum ./
|
||||
COPY pkg /app/pkg
|
||||
RUN go mod download
|
||||
|
||||
# Build layer
|
||||
COPY . .
|
||||
COPY tg_parser /app/tg_parser
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /parser .
|
||||
|
||||
FROM alpine:latest AS run
|
||||
|
||||
@@ -5,13 +5,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/logger"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/postgres"
|
||||
"github.com/TelegramExchange/pkg/logger"
|
||||
"github.com/TelegramExchange/pkg/postgres"
|
||||
"github.com/TelegramExchange/pkg/telegram"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/controller/worker"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/telegram"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/controller/worker"
|
||||
)
|
||||
|
||||
type HTTP struct {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module github.com/TelegramExchange/tgex-parser
|
||||
module github.com/TelegramExchange/tgex-backend/tg_parser
|
||||
|
||||
go 1.25.0
|
||||
|
||||
@@ -8,6 +8,7 @@ require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/rs/zerolog v1.34.0
|
||||
github.com/TelegramExchange/pkg v0.0.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -49,3 +50,5 @@ require (
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
rsc.io/qr v0.2.0 // indirect
|
||||
)
|
||||
|
||||
replace github.com/TelegramExchange/pkg => ../pkg
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) CreatePost(ctx context.Context, post domain.Post) error {
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) CreateViewsSnapshot(ctx context.Context, snapshot domain.ViewsSnapshot) error {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) DeletePost(ctx context.Context, p domain.Post) error {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) GetChannels(ctx context.Context) []domain.Channel {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) GetChannelsWithTrackedPosts(ctx context.Context) ([]domain.Channel, error) {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) GetTrackedPosts(ctx context.Context, channel domain.Channel) ([]domain.Post, error) {
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (d *Database) UpdateChannel(ctx context.Context, channel domain.Channel) error {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/telegram"
|
||||
"github.com/TelegramExchange/pkg/telegram"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/config"
|
||||
"github.com/TelegramExchange/tgex-parser/internal/adapter/database"
|
||||
tgadapter "github.com/TelegramExchange/tgex-parser/internal/adapter/telegram"
|
||||
"github.com/TelegramExchange/tgex-parser/internal/controller/httpserver"
|
||||
"github.com/TelegramExchange/tgex-parser/internal/controller/worker"
|
||||
"github.com/TelegramExchange/tgex-parser/internal/usecase"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/postgres"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/telegram"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/postgres"
|
||||
"github.com/TelegramExchange/pkg/telegram"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/config"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/adapter/database"
|
||||
tgadapter "github.com/TelegramExchange/tgex-backend/tg_parser/internal/adapter/telegram"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/controller/httpserver"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/controller/worker"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/usecase"
|
||||
)
|
||||
|
||||
func Run(ctx context.Context, c config.Config) error {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/usecase"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/usecase"
|
||||
"github.com/gotd/td/tgerr"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/usecase"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/usecase"
|
||||
)
|
||||
|
||||
type ChannelConfig struct {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/usecase"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/usecase"
|
||||
)
|
||||
|
||||
type ViewsConfig struct {
|
||||
|
||||
@@ -3,7 +3,7 @@ package usecase
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (uc *UseCase) FetchChannelMeta(ctx context.Context, username string) (domain.Channel, error) {
|
||||
|
||||
@@ -4,10 +4,10 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/transaction"
|
||||
"github.com/TelegramExchange/pkg/transaction"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (uc *UseCase) FetchChannels(ctx context.Context) error {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
func (uc *UseCase) FetchViews(ctx context.Context) error {
|
||||
|
||||
@@ -3,7 +3,7 @@ package usecase
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/internal/domain"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/domain"
|
||||
)
|
||||
|
||||
type Telegram interface {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/config"
|
||||
"github.com/TelegramExchange/tgex-parser/internal/app"
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/logger"
|
||||
"github.com/TelegramExchange/pkg/logger"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/config"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_parser/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Level string `default:"info" envconfig:"LOGGER__LEVEL"`
|
||||
PrettyConsole bool `default:"false" envconfig:"LOGGER__PRETTY_CONSOLE"`
|
||||
}
|
||||
|
||||
func Init(c Config) {
|
||||
zerolog.TimeFieldFormat = time.RFC3339
|
||||
|
||||
level := zerolog.InfoLevel
|
||||
if parsedLevel, err := zerolog.ParseLevel(c.Level); err == nil {
|
||||
level = parsedLevel
|
||||
}
|
||||
zerolog.SetGlobalLevel(level)
|
||||
|
||||
log.Logger = log.With().
|
||||
// Caller().
|
||||
// Str("app_name", c.AppName).
|
||||
// Str("app_version", c.AppVersion).
|
||||
Logger()
|
||||
|
||||
if c.PrettyConsole {
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"})
|
||||
}
|
||||
|
||||
log.Info().Msg("Logger initialized")
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string `envconfig:"DB__URL" required:"true"`
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
*pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(ctx context.Context, c Config) (*Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(c.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pgxpool.ParseConfig: %w", err)
|
||||
}
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pgxpool.NewWithConfig: %w", err)
|
||||
}
|
||||
|
||||
return &Pool{Pool: pool}, nil
|
||||
}
|
||||
|
||||
func (p *Pool) Close() {
|
||||
p.Pool.Close()
|
||||
|
||||
log.Info().Msg("Postgres closed")
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/gotd/td/session"
|
||||
"github.com/gotd/td/telegram"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ApiID int `envconfig:"TELEGRAM__API_ID" required:"true"`
|
||||
ApiHash string `envconfig:"TELEGRAM__API_HASH" required:"true"`
|
||||
SessionFile string `envconfig:"TELEGRAM__SESSION_FILE" required:"true"`
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
*telegram.Client
|
||||
cancel context.CancelFunc
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func New(cfg Config) (*Client, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
tg := telegram.NewClient(cfg.ApiID, cfg.ApiHash, telegram.Options{
|
||||
SessionStorage: &session.FileStorage{Path: cfg.SessionFile},
|
||||
})
|
||||
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
|
||||
ready := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
|
||||
client := &Client{
|
||||
Client: tg,
|
||||
cancel: cancel,
|
||||
done: done,
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := tg.Run(runCtx, func(ctx context.Context) error {
|
||||
status, err := tg.Auth().Status(ctx)
|
||||
if err != nil {
|
||||
close(ready)
|
||||
return fmt.Errorf("telegram.Auth error: %w", err)
|
||||
}
|
||||
if !status.Authorized {
|
||||
close(ready)
|
||||
return fmt.Errorf("session not authorized")
|
||||
}
|
||||
|
||||
close(ready)
|
||||
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("telegram client stopped")
|
||||
}
|
||||
|
||||
close(done)
|
||||
}()
|
||||
|
||||
<-ready
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) Close() {
|
||||
c.cancel()
|
||||
<-c.done
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package transaction
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/TelegramExchange/tgex-parser/pkg/postgres"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var (
|
||||
errMissingInit = errors.New("missing `transaction.Init' call before `transaction.Begin'")
|
||||
errMissingBegin = errors.New("missing `transaction.Begin' call before 'transaction.Get'")
|
||||
)
|
||||
|
||||
var (
|
||||
pool *pgxpool.Pool
|
||||
IsUnitTest bool
|
||||
)
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
func Init(p *postgres.Pool) {
|
||||
pool = p.Pool
|
||||
}
|
||||
|
||||
type Transaction struct {
|
||||
pgx.Tx
|
||||
}
|
||||
|
||||
func Begin(ctx context.Context) (context.Context, error) {
|
||||
if IsUnitTest {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
if pool == nil {
|
||||
return nil, errMissingInit
|
||||
}
|
||||
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pool.Begin: %w", err)
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, ctxKey{}, &Transaction{tx})
|
||||
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func Rollback(ctx context.Context) {
|
||||
if IsUnitTest {
|
||||
return
|
||||
}
|
||||
|
||||
tx, ok := ctx.Value(ctxKey{}).(*Transaction)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
err := tx.Rollback(ctx)
|
||||
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
|
||||
log.Error().Err(err).Msg("transaction: Rollback")
|
||||
}
|
||||
}
|
||||
|
||||
func Commit(ctx context.Context) error {
|
||||
if IsUnitTest {
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, ok := ctx.Value(ctxKey{}).(*Transaction)
|
||||
if !ok {
|
||||
return errMissingBegin
|
||||
}
|
||||
|
||||
err := tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Commit: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Executor interface {
|
||||
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
|
||||
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
|
||||
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
|
||||
}
|
||||
|
||||
func TryExtractTX(ctx context.Context) Executor {
|
||||
tx, ok := ctx.Value(ctxKey{}).(*Transaction)
|
||||
if !ok {
|
||||
return pool
|
||||
}
|
||||
|
||||
return tx
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package transaction
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func Wrap(ctx context.Context, fn func(context.Context) error) error {
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pool.Begin: %w", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = tx.Rollback(ctx)
|
||||
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
|
||||
log.Error().Err(err).Msg("transaction: Rollback")
|
||||
}
|
||||
}()
|
||||
|
||||
ctx = context.WithValue(ctx, ctxKey{}, &Transaction{tx})
|
||||
|
||||
err = fn(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fn: %w", err)
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("tx.Commit: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user