Merge branch 'dev-sql-improve'

This commit is contained in:
Sergey Chubaryan 2024-12-03 17:23:07 +03:00
commit f8968ddd82
6 changed files with 55 additions and 16 deletions

View File

@ -16,10 +16,16 @@ services:
-c checkpoint_completion_target=0.9
-c wal_buffers=16MB
-c default_statistics_target=100
healthcheck:
test: ["CMD-SHELL", "psql -U postgres -d postgres -c 'SELECT 1' || exit 1"]
interval: 2s
timeout: 10s
retries: 5
ports:
- 5432:5432
volumes:
- postgres-volume:/var/lib/postgresql/data
- ./sql:/docker-entrypoint-initdb.d
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
@ -72,7 +78,7 @@ services:
- 4318:4318 # OTLP http receiver
tempo-init:
image: &tempoImage grafana/tempo:2.3.1
image: &tempoImage grafana/tempo:r177-60780f7
user: root
entrypoint:
- "chown"

View File

@ -2,10 +2,12 @@ package main
import (
"context"
"encoding/json"
"io"
"log"
"github.com/segmentio/kafka-go"
"gopkg.in/gomail.v2"
)
// type emailHelper struct {
@ -40,31 +42,23 @@ import (
func main() {
const (
SMTP_SERVER = ""
SMTP_PORT = 0
SMTP_LOGIN = ""
SMTP_PASSWORD = ""
SMTP_SERVER = "smtp.yandex.ru"
SMTP_PORT = 587
SMTP_LOGIN = "serghio2@yandex.ru"
SMTP_PASSWORD = "ercutguhcfuzbvyl"
)
ctx := context.Background()
// d := gomail.NewDialer(SMTP_SERVER, SMTP_PORT, SMTP_LOGIN, SMTP_PASSWORD)
// conn, err := kafka.DialLeader(ctx, "", "")
// if err != nil {
// panic(err)
// }
// defer conn.Close()
d := gomail.NewDialer(SMTP_SERVER, SMTP_PORT, SMTP_LOGIN, SMTP_PASSWORD)
log.Println("starting reader...")
r := kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{"localhost:9092"},
Topic: "topic-A",
// Partition: 0,
Topic: "backend_events",
GroupID: "consumer-group-id",
})
// r.SetOffset(kafka.FirstOffset)
log.Println("reader started")
@ -83,6 +77,28 @@ func main() {
if err := r.CommitMessages(ctx, msg); err != nil {
log.Fatalf("failed to commit: %s\n", err.Error())
continue
}
value := struct {
Email string `json:"email"`
Token string `json:"token"`
}{}
if err := json.Unmarshal(msg.Value, &value); err != nil {
log.Fatalf("failed to unmarshal: %s\n", err.Error())
continue
}
m := gomail.NewMessage()
m.SetHeader("From", m.FormatAddress("serghio2@yandex.ru", "Pet Backend"))
m.SetHeader("To", value.Email)
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Test backend")
if err := d.DialAndSend(m); err != nil {
log.Fatalf("failed to send email: %s\n", err.Error())
continue
}
}
}

View File

@ -1,7 +1,7 @@
create table if not exists action_tokens (
id int generated always as identity,
user_id int,
value text
value text,
target int,
expiration timestamp,

9
sql/init.sh Executable file
View File

@ -0,0 +1,9 @@
# FILES=(
# "01_user.sql"
# "02_shortlinks.sql"
# "03_action_tokens.sql"
# )
# for file in ${FILES[@]}; do
# psql -U postgres -d postgres -f $file
# done

View File

@ -29,6 +29,7 @@ type UserService interface {
CreateUser(ctx context.Context, params UserCreateParams) (*models.UserDTO, error)
AuthenticateUser(ctx context.Context, login, password string) (string, error)
ValidateToken(ctx context.Context, tokenStr string) (*models.UserDTO, error)
HelpPasswordForgot(ctx context.Context, userId string) error
}
func NewUserService(deps UserServiceDeps) UserService {
@ -84,6 +85,8 @@ func (u *userService) CreateUser(ctx context.Context, params UserCreateParams) (
return nil, err
}
u.deps.EventRepo.SendEmailForgotPassword(ctx, user.Email, "123")
u.deps.UserCache.Set(result.Id, *result, cache.Expiration{Ttl: userCacheTtl})
return result, nil

View File

@ -7,6 +7,7 @@ import (
"backend/src/logger"
"backend/src/server/handlers"
"backend/src/server/middleware"
"backend/src/server/utils"
"context"
"fmt"
"net"
@ -61,6 +62,10 @@ func New(opts NewServerOpts) *Server {
{
dummyGroup.Use(middleware.NewAuthMiddleware(opts.UserService))
dummyGroup.GET("", handlers.NewDummyHandler())
dummyGroup.POST("/forgot-password", func(c *gin.Context) {
user := utils.GetUserFromRequest(c)
opts.UserService.HelpPasswordForgot(c, user.Id)
})
}
return &Server{