generated from PenaSide/GolangTemplate
update after moved some validate func to common
This commit is contained in:
parent
17ccca3154
commit
6c26eb8295
@ -5,14 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"gitea.pena/PenaSide/common/encrypt"
|
"gitea.pena/PenaSide/common/encrypt"
|
||||||
"gitea.pena/PenaSide/common/mongo"
|
"gitea.pena/PenaSide/common/mongo"
|
||||||
|
"gitea.pena/PenaSide/common/validate"
|
||||||
"gitea.pena/PenaSide/customer/internal/models"
|
"gitea.pena/PenaSide/customer/internal/models"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/twmb/franz-go/pkg/kgo"
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -64,7 +62,7 @@ func main() {
|
|||||||
log.Fatalf("error validating tg enviroments: %v", err)
|
log.Fatalf("error validating tg enviroments: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = validateMongo(config.ExternalCfg.Database); err != nil {
|
if err = validate.ValidateMongo(config.ExternalCfg.Database); err != nil {
|
||||||
log.Fatalf("error validating mongodb: %v", err)
|
log.Fatalf("error validating mongodb: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -233,18 +231,9 @@ func validateNotEmpty(config *models.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateTG(notificationBotToken string, notificationRsPayChannel int64, notificationChannel int64) error {
|
func validateTG(notificationBotToken string, notificationRsPayChannel int64, notificationChannel int64) error {
|
||||||
if notificationBotToken == "" {
|
err := validate.ValidateTgToken(notificationBotToken)
|
||||||
return fmt.Errorf("notificationBotToken is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo обдумать еще регулярку
|
|
||||||
pattern := `^\d+:.+$`
|
|
||||||
ok, err := regexp.MatchString(pattern, notificationBotToken)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error validating notificationBotToken: %w", err)
|
return err
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("invalid notificationBotToken format")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if notificationChannel == 0 {
|
if notificationChannel == 0 {
|
||||||
@ -256,78 +245,3 @@ func validateTG(notificationBotToken string, notificationRsPayChannel int64, not
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type t struct {
|
|
||||||
ID string `bson:"_id,omitempty"`
|
|
||||||
I int `bson:"i"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo в будущем в монге будут запрещены некоторые операции, надо будет обновлять
|
|
||||||
func validateMongo(cfg mongo.Configuration) error {
|
|
||||||
if cfg.URL == "" {
|
|
||||||
return fmt.Errorf("mongo URL is empty")
|
|
||||||
}
|
|
||||||
if cfg.DatabaseName == "" {
|
|
||||||
return fmt.Errorf("mongo database name is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.DatabaseName = "testDBName"
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
database, err := mongo.Connect(ctx, &mongo.ConnectDeps{
|
|
||||||
Configuration: &cfg,
|
|
||||||
Timeout: 10 * time.Second,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer database.Drop(ctx)
|
|
||||||
|
|
||||||
testCollection := database.Collection(cfg.DatabaseName)
|
|
||||||
|
|
||||||
receivedChannel := make(chan string, 10)
|
|
||||||
errorChannel := make(chan error, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(receivedChannel)
|
|
||||||
defer close(errorChannel)
|
|
||||||
for i := 0; i <= 100; i++ {
|
|
||||||
d := t{
|
|
||||||
ID: primitive.NewObjectID().Hex(),
|
|
||||||
I: i,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = testCollection.InsertOne(ctx, d)
|
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
}
|
|
||||||
|
|
||||||
receivedChannel <- d.ID
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
timeout := time.After(30 * time.Second)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case err = <-errorChannel:
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error document insert: %w", err)
|
|
||||||
}
|
|
||||||
case id := <-receivedChannel:
|
|
||||||
result := t{}
|
|
||||||
err := testCollection.FindOne(ctx, bson.M{"_id": id}).Decode(&result)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("mongo error finding document: %v", err)
|
|
||||||
}
|
|
||||||
if id != result.ID {
|
|
||||||
return fmt.Errorf("invalid id received")
|
|
||||||
}
|
|
||||||
if result.I == 100 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
case <-timeout:
|
|
||||||
return fmt.Errorf("timeout")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.pena/PenaSide/common/encrypt"
|
"gitea.pena/PenaSide/common/encrypt"
|
||||||
"gitea.pena/PenaSide/common/mongo"
|
|
||||||
"gitea.pena/PenaSide/customer/internal/models"
|
"gitea.pena/PenaSide/customer/internal/models"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"os"
|
"os"
|
||||||
@ -47,14 +46,6 @@ func TestValidateURLs(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateMongo(t *testing.T) {
|
|
||||||
err := validateMongo(mongo.Configuration{
|
|
||||||
URL: "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0&readPreference=primary&ssl=false",
|
|
||||||
DatabaseName: "admin",
|
|
||||||
})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateKafka(t *testing.T) {
|
func TestValidateKafka(t *testing.T) {
|
||||||
err := validateKafka([]string{"localhost:9092"}, "test-topic")
|
err := validateKafka([]string{"localhost:9092"}, "test-topic")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.23.2
|
|||||||
toolchain go1.23.3
|
toolchain go1.23.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitea.pena/PenaSide/common v0.0.0-20241128160655-fe730a08b5f1
|
gitea.pena/PenaSide/common v0.0.0-20241202085238-def6267b6da0
|
||||||
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a
|
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a
|
||||||
gitea.pena/PenaSide/trashlog v0.0.0-20241119225515-2fd267647ca4
|
gitea.pena/PenaSide/trashlog v0.0.0-20241119225515-2fd267647ca4
|
||||||
github.com/caarlos0/env/v8 v8.0.0
|
github.com/caarlos0/env/v8 v8.0.0
|
||||||
|
2
go.sum
2
go.sum
@ -5,6 +5,8 @@ gitea.pena/PenaSide/common v0.0.0-20241128144932-b14c114d2569 h1:4PJsYkPNmbpcLAh
|
|||||||
gitea.pena/PenaSide/common v0.0.0-20241128144932-b14c114d2569/go.mod h1:l71j3W1yROhOSfjWZ6wcMuzjBR37gu2ZTcXsorEJoiw=
|
gitea.pena/PenaSide/common v0.0.0-20241128144932-b14c114d2569/go.mod h1:l71j3W1yROhOSfjWZ6wcMuzjBR37gu2ZTcXsorEJoiw=
|
||||||
gitea.pena/PenaSide/common v0.0.0-20241128160655-fe730a08b5f1 h1:w1AAxrlZGmxUI9NAa/0TYsc6sqWWF9yAzQaLL/KxwrM=
|
gitea.pena/PenaSide/common v0.0.0-20241128160655-fe730a08b5f1 h1:w1AAxrlZGmxUI9NAa/0TYsc6sqWWF9yAzQaLL/KxwrM=
|
||||||
gitea.pena/PenaSide/common v0.0.0-20241128160655-fe730a08b5f1/go.mod h1:l71j3W1yROhOSfjWZ6wcMuzjBR37gu2ZTcXsorEJoiw=
|
gitea.pena/PenaSide/common v0.0.0-20241128160655-fe730a08b5f1/go.mod h1:l71j3W1yROhOSfjWZ6wcMuzjBR37gu2ZTcXsorEJoiw=
|
||||||
|
gitea.pena/PenaSide/common v0.0.0-20241202085238-def6267b6da0 h1:YkaR1shmenCRumIpllxanZJEUI38Snea7kZ94xjHUYM=
|
||||||
|
gitea.pena/PenaSide/common v0.0.0-20241202085238-def6267b6da0/go.mod h1:l71j3W1yROhOSfjWZ6wcMuzjBR37gu2ZTcXsorEJoiw=
|
||||||
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a h1:UySqMgaOKNsR42Y6GQXoM2wn/waYNc9cakMUSvbEEAg=
|
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a h1:UySqMgaOKNsR42Y6GQXoM2wn/waYNc9cakMUSvbEEAg=
|
||||||
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a/go.mod h1:gdd+vOT6up9STkEbxa2qESLIMZFjCmRbkcheFQCVgZU=
|
gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a/go.mod h1:gdd+vOT6up9STkEbxa2qESLIMZFjCmRbkcheFQCVgZU=
|
||||||
gitea.pena/PenaSide/trashlog v0.0.0-20241119225515-2fd267647ca4 h1:y9B4CSPIgiUoaXKyXLZxs1A9hxzDj26F9MH2R6uTkHQ=
|
gitea.pena/PenaSide/trashlog v0.0.0-20241119225515-2fd267647ca4 h1:y9B4CSPIgiUoaXKyXLZxs1A9hxzDj26F9MH2R6uTkHQ=
|
||||||
|
Loading…
Reference in New Issue
Block a user