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{
|
adminSrv := server.NewHTTP(server.ServerConfig{
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Controllers: []server.Controller{cons.VerificationAdmin},
|
Controllers: []server.Controller{cons.VerificationAdmin, cons.HealthCheck},
|
||||||
HLogger: loggerHlog,
|
HLogger: loggerHlog,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gitea.pena/PenaSide/verification/internal/models"
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gitea.pena/PenaSide/verification/internal/models"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -84,3 +84,11 @@ func (t *Telegram) SendVerification(data *models.Verification, url string, isUpd
|
|||||||
|
|
||||||
return nil
|
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
|
package initialize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gitea.pena/PenaSide/customer/pkg/customer_clients"
|
||||||
"gitea.pena/PenaSide/verification/internal/client"
|
"gitea.pena/PenaSide/verification/internal/client"
|
||||||
"gitea.pena/PenaSide/verification/internal/controllers/admin"
|
"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/verification/internal/controllers/user"
|
||||||
"gitea.pena/PenaSide/customer/pkg/customer_clients"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controllers struct {
|
type Controllers struct {
|
||||||
VerificationAdmin *admin.VerifyAdminController
|
VerificationAdmin *admin.VerifyAdminController
|
||||||
VerificationUser *user.VerifyUserController
|
VerificationUser *user.VerifyUserController
|
||||||
|
HealthCheck *health_check.HealthCheck
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewControllers(reps *Repositories, telegram *client.Telegram, customer *customer_clients.CustomersClient) *Controllers {
|
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,
|
Repository: reps.Verification,
|
||||||
Telegram: telegram,
|
Telegram: telegram,
|
||||||
}),
|
}),
|
||||||
|
HealthCheck: health_check.NewHealthCheck(health_check.Deps{
|
||||||
|
TgClient: telegram,
|
||||||
|
Repository: reps.Verification,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,16 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"time"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.pena/PenaSide/verification/internal/models"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gitea.pena/PenaSide/verification/internal/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type VerificationRepository struct {
|
type VerificationRepository struct {
|
||||||
@ -228,7 +228,6 @@ func (r *VerificationRepository) UpdateFile(ctx context.Context, userID, fileNam
|
|||||||
|
|
||||||
// update in mongodb
|
// update in mongodb
|
||||||
|
|
||||||
|
|
||||||
result, err := r.Update(ctx, &models.Verification{ID: verification.ID, Files: verification.Files})
|
result, err := r.Update(ctx, &models.Verification{ID: verification.ID, Files: verification.Files})
|
||||||
if r.err(err) {
|
if r.err(err) {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -244,3 +243,31 @@ func (r *VerificationRepository) err(err error) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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