generated from PenaSide/GolangTemplate
added some tests and update loadcfg func
This commit is contained in:
parent
76595cb3d2
commit
b1bcf42af6
@ -6,13 +6,15 @@ import (
|
||||
"gitea.pena/PenaSide/common/encrypt"
|
||||
"gitea.pena/PenaSide/common/mongo"
|
||||
"gitea.pena/PenaSide/customer/internal/models"
|
||||
"github.com/caarlos0/env/v8"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -62,27 +64,80 @@ func main() {
|
||||
log.Fatalf("error validating tg enviroments: %v", err)
|
||||
}
|
||||
|
||||
//todo mongo
|
||||
if err = validateMongo(config.ExternalCfg.Database); err != nil {
|
||||
log.Fatalf("error validating mongodb: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 38 fields
|
||||
func loadConfig() (*models.Config, error) {
|
||||
var config models.Config
|
||||
if err := env.Parse(&config); err != nil {
|
||||
return nil, err
|
||||
config := models.Config{
|
||||
ExternalCfg: models.ExternalCfg{
|
||||
JwtCfg: models.JWTConfiguration{
|
||||
PublicKey: os.Getenv("JWT_PUBLIC_KEY"),
|
||||
Audience: os.Getenv("JWT_AUDIENCE"),
|
||||
Issuer: os.Getenv("JWT_ISSUER"),
|
||||
},
|
||||
Database: mongo.Configuration{
|
||||
Host: os.Getenv("MONGO_HOST"),
|
||||
Port: os.Getenv("MONGO_PORT"),
|
||||
User: os.Getenv("MONGO_USER"),
|
||||
Password: os.Getenv("MONGO_PASSWORD"),
|
||||
DatabaseName: os.Getenv("MONGO_DB_NAME"),
|
||||
Auth: os.Getenv("MONGO_AUTH"),
|
||||
},
|
||||
MailClientCfg: models.MailClientCfg{
|
||||
ApiUrl: os.Getenv("API_URL"),
|
||||
Sender: os.Getenv("MAIL_SENDER"),
|
||||
ApiKey: os.Getenv("MAIL_API_KEY"),
|
||||
MailAddress: os.Getenv("MAIL_ADDRESS"),
|
||||
},
|
||||
EncryptCommon: encrypt.Encrypt{
|
||||
PrivKey: os.Getenv("ENCRYPT_PRIVATE_KEY"),
|
||||
PubKey: os.Getenv("ENCRYPT_PUBLIC_KEY"),
|
||||
},
|
||||
},
|
||||
ClientHttpPort: os.Getenv("CLIENT_HTTP_PORT"),
|
||||
ClientHttpHost: os.Getenv("CLIENT_HTTP_HOST"),
|
||||
AdminHttpPort: os.Getenv("ADMIN_HTTP_PORT"),
|
||||
AdminHttpHost: os.Getenv("ADMIN_HTTP_HOST"),
|
||||
GrpcHost: os.Getenv("GRPC_HOST"),
|
||||
GrpcPort: os.Getenv("GRPC_PORT"),
|
||||
GrpcDomen: os.Getenv("GRPC_DOMEN"),
|
||||
KafkaBrokers: strings.Split(os.Getenv("KAFKA_BROKERS"), ","),
|
||||
KafkaTopic: os.Getenv("KAFKA_TOPIC_TARIFF"),
|
||||
AuthMicroservice: os.Getenv("AUTH_MICROSERVICE_URL"),
|
||||
HubadminMicroservice: os.Getenv("HUBADMIN_MICROSERVICE_URL"),
|
||||
CurrencyMicroservice: os.Getenv("CURRENCY_MICROSERVICE_URL"),
|
||||
DiscountMicroservice: os.Getenv("DISCOUNT_MICROSERVICE_GRPC_HOST"),
|
||||
PaymentMicroservice: os.Getenv("PAYMENT_MICROSERVICE_GRPC_HOST"),
|
||||
VerificationMicroservice: os.Getenv("VERIFICATION_MICROSERVICE_URL"),
|
||||
TemplategenMicroserviceURL: os.Getenv("TEMPLATEGEN_MICROSERVICE_URL"),
|
||||
CodewordMicroservice: os.Getenv("CODEWORD_MICROSERVICE_GRPC_HOST"),
|
||||
TrashLogHost: os.Getenv("TRASH_LOG_HOST"),
|
||||
ModuleLogger: os.Getenv("MODULE_LOGGER"),
|
||||
NotificationBotToken: os.Getenv("NOTIFICATION_BOT_TOKEN"),
|
||||
NotificationRsPayChannel: envTiInt64(os.Getenv("NOTIFICATION_RS_PAY_CHANNEL")),
|
||||
NotificationChannel: envTiInt64(os.Getenv("NOTIFICATION_CHANNEL")),
|
||||
AdminURL: os.Getenv("ADMIN_FRONT_URL"),
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func envTiInt64(str string) int64 {
|
||||
n, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func validateURLs(urls []string) error {
|
||||
for index, u := range urls {
|
||||
if u == "" {
|
||||
return fmt.Errorf("empty url, index: %d", index)
|
||||
}
|
||||
|
||||
// todo проверять урлы
|
||||
//if ip := net.Pa(u); ip == nil {
|
||||
// return fmt.Errorf("invalid url: %s", u)
|
||||
//}
|
||||
// todo check the liveness of these URLs, many services do not support
|
||||
}
|
||||
return nil
|
||||
|
@ -1,9 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"gitea.pena/PenaSide/common/encrypt"
|
||||
"gitea.pena/PenaSide/common/mongo"
|
||||
"gitea.pena/PenaSide/customer/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -51,3 +58,93 @@ func TestValidateMongo(t *testing.T) {
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateKafka(t *testing.T) {
|
||||
err := validateKafka([]string{"localhost:9092"}, "test-topic")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateEncryptKeys(t *testing.T) {
|
||||
priveKey, pubKey, err := generateRSAKeys(2048)
|
||||
assert.NoError(t, err)
|
||||
err = validateEncryptKeys(&encrypt.Encrypt{
|
||||
PubKey: pubKey,
|
||||
PrivKey: priveKey,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func generateRSAKeys(bitSize int) (privateKey string, publicKey string, err error) {
|
||||
privKey, err := rsa.GenerateKey(rand.Reader, bitSize)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
privKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
|
||||
})
|
||||
|
||||
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&privKey.PublicKey)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
pubKeyPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "RSA PUBLIC KEY",
|
||||
Bytes: pubKeyBytes,
|
||||
})
|
||||
|
||||
return string(privKeyPEM), string(pubKeyPEM), nil
|
||||
}
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
envVars := map[string]string{
|
||||
"JWT_ISSUER": "pena-auth-service",
|
||||
"JWT_AUDIENCE": "pena",
|
||||
"JWT_PUBLIC_KEY": "-----BEGIN PUBLIC KEY-----\nMIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHgnvr7O2tiApjJfid1orFnIGm69\n80fZp+Lpbjo+NC/0whMFga2Biw5b1G2Q/B2u0tpO1Fs/E8z7Lv1nYfr5jx2S8x6B\ndA4TS2kB9Kf0wn0+7wSlyikHoKhbtzwXHZl17GsyEi6wHnsqNBSauyIWhpha8i+Y\n+3GyaOY536H47qyXAgMBAAE=\n-----END PUBLIC KEY-----",
|
||||
"CLIENT_HTTP_HOST": "0.0.0.0",
|
||||
"CLIENT_HTTP_PORT": "8080",
|
||||
"ADMIN_HTTP_HOST": "0.0.0.0",
|
||||
"ADMIN_HTTP_PORT": "8081",
|
||||
"GRPC_HOST": "0.0.0.0",
|
||||
"GRPC_PORT": "9001",
|
||||
"GRPC_DOMEN": "customer-service:9000",
|
||||
"MONGO_HOST": "localhost",
|
||||
"MONGO_PORT": "27020",
|
||||
"MONGO_USER": "test",
|
||||
"MONGO_PASSWORD": "test",
|
||||
"MONGO_DB_NAME": "admin",
|
||||
"MONGO_AUTH": "admin",
|
||||
"KAFKA_BROKERS": "localhost:9092",
|
||||
"KAFKA_TOPIC_TARIFF": "test-topic",
|
||||
"AUTH_MICROSERVICE_URL": "http://localhost:8000/user",
|
||||
"HUBADMIN_MICROSERVICE_URL": "http://localhost:8001/tariff",
|
||||
"CURRENCY_MICROSERVICE_URL": "http://cbrfworker-service:8000/change",
|
||||
"DISCOUNT_MICROSERVICE_GRPC_HOST": "localhost:9040",
|
||||
"PAYMENT_MICROSERVICE_GRPC_HOST": "treasurer-service:9085",
|
||||
"VERIFICATION_MICROSERVICE_URL": "http://10.8.0.8:7035/verification",
|
||||
"TEMPLATEGEN_MICROSERVICE_URL": "10.6.0.17",
|
||||
"CODEWORD_MICROSERVICE_GRPC_HOST": "http://localhost:8000/user",
|
||||
"TRASH_LOG_HOST": "localhost:7113",
|
||||
"API_URL": "https://api.smtp.bz/v1/smtp/send",
|
||||
"MAIL_SENDER": "noreply@mailing.pena.digital",
|
||||
"MAIL_API_KEY": "P0YsjUB137upXrr1NiJefHmXVKW1hmBWlpev",
|
||||
"MAIL_ADDRESS": "mail@mail.com",
|
||||
"MODULE_LOGGER": "local-customer",
|
||||
"NOTIFICATION_BOT_TOKEN": "7127966184:AAG1steOCH4wDvHRe9QcsXJPS4dWRyRYsqg",
|
||||
"NOTIFICATION_CHANNEL": "-1002177203276",
|
||||
"NOTIFICATION_RS_PAY_CHANNEL": "-1002177203276",
|
||||
"ADMIN_FRONT_URL": "https://admin-front.ru",
|
||||
"ENCRYPT_PUBLIC_KEY": "1",
|
||||
"ENCRYPT_PRIVATE_KEY": "2",
|
||||
}
|
||||
|
||||
for key, value := range envVars {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
|
||||
cfg, err := loadConfig()
|
||||
assert.Nil(t, err)
|
||||
|
||||
fmt.Println(cfg)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user