Compare commits
3 Commits
dev
...
healthchec
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7bdd6a000e | ||
![]() |
d00e76d872 | ||
![]() |
fb39b8d395 |
@ -99,7 +99,7 @@ func Run(cfg *config.Config, build Build) {
|
||||
|
||||
adminSrv := server.NewHTTP(server.ServerConfig{
|
||||
Logger: logger,
|
||||
Controllers: []server.Controller{cons.VerificationAdmin},
|
||||
Controllers: []server.Controller{cons.VerificationAdmin, cons.HealthCheck},
|
||||
HLogger: loggerHlog,
|
||||
})
|
||||
|
||||
|
@ -4,9 +4,9 @@ import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"gitea.pena/PenaSide/verification/internal/models"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"go.uber.org/zap"
|
||||
"gitea.pena/PenaSide/verification/internal/models"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
@ -35,7 +35,7 @@ func NewTelegram(deps Deps) *Telegram {
|
||||
}
|
||||
|
||||
func (t *Telegram) SendVerification(data *models.Verification, url string, isUpdate bool) error {
|
||||
fmt.Println("VERT", data, url,isUpdate)
|
||||
fmt.Println("VERT", data, url, isUpdate)
|
||||
var tplPath string
|
||||
if isUpdate {
|
||||
tplPath = UpdatedVerification
|
||||
@ -43,13 +43,13 @@ func (t *Telegram) SendVerification(data *models.Verification, url string, isUpd
|
||||
tplPath = NewVerification
|
||||
}
|
||||
|
||||
fmt.Println("VERT1", tplPath)
|
||||
fmt.Println("VERT1", tplPath)
|
||||
var userURL string
|
||||
userURL = fmt.Sprintf("%s/users/%s", t.stagingURL, data.UserID)
|
||||
fmt.Println("VERT2", userURL)
|
||||
fmt.Println("VERT2", userURL)
|
||||
|
||||
tpl, err := template.New("verification_template").Parse(tplPath)
|
||||
fmt.Println("VERT333", tpl,err)
|
||||
fmt.Println("VERT333", tpl, err)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing template: %w", err)
|
||||
}
|
||||
@ -69,14 +69,14 @@ func (t *Telegram) SendVerification(data *models.Verification, url string, isUpd
|
||||
}
|
||||
|
||||
err = tpl.Execute(&text, toTG)
|
||||
fmt.Println("VERT433", err)
|
||||
fmt.Println("VERT433", err)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error executing template: %w", err)
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(t.chatID, text.String())
|
||||
|
||||
fmt.Println("VERT433", err, t.chatID, text.String())
|
||||
fmt.Println("VERT433", err, t.chatID, text.String())
|
||||
_, err = t.bot.Send(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending message: %w", err)
|
||||
@ -84,3 +84,11 @@ func (t *Telegram) SendVerification(data *models.Verification, url string, isUpd
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Telegram) Ping() error {
|
||||
_, err := t.bot.GetMe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error pinging tg bot: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
44
internal/controllers/admin/health_check/health_check.go
Normal file
44
internal/controllers/admin/health_check/health_check.go
Normal file
@ -0,0 +1,44 @@
|
||||
package health_check
|
||||
|
||||
import (
|
||||
"gitea.pena/PenaSide/verification/internal/client"
|
||||
"gitea.pena/PenaSide/verification/internal/repository"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Deps struct {
|
||||
TgClient *client.Telegram
|
||||
Repository *repository.VerificationRepository
|
||||
}
|
||||
|
||||
type HealthCheck struct {
|
||||
tgClient *client.Telegram
|
||||
repository *repository.VerificationRepository
|
||||
}
|
||||
|
||||
func NewHealthCheck(deps Deps) *HealthCheck {
|
||||
return &HealthCheck{
|
||||
tgClient: deps.TgClient,
|
||||
repository: deps.Repository,
|
||||
}
|
||||
}
|
||||
|
||||
func (receiver *HealthCheck) Liveness(ctx *fiber.Ctx) error {
|
||||
return ctx.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func (receiver *HealthCheck) Readiness(ctx *fiber.Ctx) error {
|
||||
if err := receiver.repository.CheckMongoDB(); err != nil {
|
||||
return ctx.Status(fiber.StatusServiceUnavailable).SendString("mongoDB is unavailable: " + err.Error())
|
||||
}
|
||||
|
||||
if err := receiver.tgClient.Ping(); err != nil {
|
||||
return ctx.Status(fiber.StatusServiceUnavailable).SendString("could not connect to Telegram: " + err.Error())
|
||||
}
|
||||
|
||||
if err := receiver.repository.CheckS3(); err != nil {
|
||||
return ctx.Status(fiber.StatusServiceUnavailable).SendString("could not connect to S3: " + err.Error())
|
||||
}
|
||||
|
||||
return ctx.SendStatus(fiber.StatusOK)
|
||||
}
|
12
internal/controllers/admin/health_check/route.go
Normal file
12
internal/controllers/admin/health_check/route.go
Normal file
@ -0,0 +1,12 @@
|
||||
package health_check
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
func (receiver *HealthCheck) Register(router fiber.Router) {
|
||||
router.Get("/liveness", receiver.Liveness)
|
||||
router.Get("/readiness", receiver.Readiness)
|
||||
}
|
||||
|
||||
func (receiver *HealthCheck) Name() string {
|
||||
return ""
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"gitea.pena/PenaSide/customer/pkg/customer_clients"
|
||||
"gitea.pena/PenaSide/verification/internal/client"
|
||||
"gitea.pena/PenaSide/verification/internal/controllers/admin"
|
||||
"gitea.pena/PenaSide/verification/internal/controllers/admin/health_check"
|
||||
"gitea.pena/PenaSide/verification/internal/controllers/user"
|
||||
"gitea.pena/PenaSide/customer/pkg/customer_clients"
|
||||
)
|
||||
|
||||
type Controllers struct {
|
||||
VerificationAdmin *admin.VerifyAdminController
|
||||
VerificationUser *user.VerifyUserController
|
||||
HealthCheck *health_check.HealthCheck
|
||||
}
|
||||
|
||||
func NewControllers(reps *Repositories, telegram *client.Telegram, customer *customer_clients.CustomersClient) *Controllers {
|
||||
@ -22,5 +24,9 @@ func NewControllers(reps *Repositories, telegram *client.Telegram, customer *cus
|
||||
Repository: reps.Verification,
|
||||
Telegram: telegram,
|
||||
}),
|
||||
HealthCheck: health_check.NewHealthCheck(health_check.Deps{
|
||||
TgClient: telegram,
|
||||
Repository: reps.Verification,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -5,16 +5,16 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"time"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.pena/PenaSide/verification/internal/models"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.uber.org/zap"
|
||||
"gitea.pena/PenaSide/verification/internal/models"
|
||||
)
|
||||
|
||||
type VerificationRepository struct {
|
||||
@ -195,17 +195,17 @@ func (r *VerificationRepository) UpdateFile(ctx context.Context, userID, fileNam
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileURL := fmt.Sprintf("%s/%s/%s/%s", r.url, r.folder, userID, fileHeader.Filename)
|
||||
fileURL := fmt.Sprintf("%s/%s/%s/%s", r.url, r.folder, userID, fileHeader.Filename)
|
||||
|
||||
// remove old file
|
||||
verification, err := r.GetByUserID(ctx, userID)
|
||||
if r.err(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if verification == nil {
|
||||
return nil, fmt.Errorf("no verification found")
|
||||
}
|
||||
|
||||
if verification == nil {
|
||||
return nil, fmt.Errorf("no verification found")
|
||||
}
|
||||
|
||||
found := false
|
||||
for iterator, file := range verification.Files {
|
||||
@ -228,7 +228,6 @@ func (r *VerificationRepository) UpdateFile(ctx context.Context, userID, fileNam
|
||||
|
||||
// update in mongodb
|
||||
|
||||
|
||||
result, err := r.Update(ctx, &models.Verification{ID: verification.ID, Files: verification.Files})
|
||||
if r.err(err) {
|
||||
return nil, err
|
||||
@ -244,3 +243,31 @@ func (r *VerificationRepository) err(err error) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *VerificationRepository) CheckMongoDB() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := r.mongo.Database().Client().Ping(ctx, nil); err != nil {
|
||||
return fmt.Errorf("failed to ping MongoDB: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *VerificationRepository) CheckS3() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
buckets, err := r.s3.ListBuckets(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list buckets: %v", err)
|
||||
}
|
||||
|
||||
for _, b := range buckets {
|
||||
if b.Name == VerificationBucket {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to find verification bucket")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user