2024-11-27 15:01:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"gitea.pena/PenaSide/common/encrypt"
|
2024-11-28 09:47:29 +00:00
|
|
|
"gitea.pena/PenaSide/common/mongo"
|
2024-12-02 08:59:38 +00:00
|
|
|
"gitea.pena/PenaSide/common/validate"
|
2024-11-27 15:01:51 +00:00
|
|
|
"gitea.pena/PenaSide/customer/internal/models"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
|
|
|
"log"
|
2024-11-28 12:31:54 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-11-27 15:01:51 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
config, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateNotEmpty(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error validating config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
urls := []string{
|
2024-12-06 12:23:44 +00:00
|
|
|
config.AuthServiceURL,
|
|
|
|
config.HubadminServiceURL,
|
|
|
|
config.CurrencyServiceURL,
|
|
|
|
config.DiscountServiceGRPCURL,
|
|
|
|
config.CodewordServiceGRPCURL,
|
|
|
|
config.PaymentServiceGRPCURL,
|
|
|
|
config.VerificationServiceURL,
|
|
|
|
config.TemplategenServiceURL,
|
2024-11-27 15:01:51 +00:00
|
|
|
config.TrashLogHost,
|
|
|
|
config.AdminURL,
|
|
|
|
config.ExternalCfg.MailClientCfg.ApiUrl,
|
|
|
|
}
|
|
|
|
if err = validateURLs(urls); err != nil {
|
|
|
|
log.Fatalf("error validating urls: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo validate jwt
|
|
|
|
|
2024-12-06 12:23:44 +00:00
|
|
|
if err = validateKafka(config.KafkaBrokers, config.KafkaTopicTariff); err != nil {
|
2024-11-27 15:01:51 +00:00
|
|
|
log.Fatalf("error validating kafka: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateMail(config.ExternalCfg.MailClientCfg); err != nil {
|
|
|
|
log.Fatalf("error validating smtp: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-12-06 09:55:16 +00:00
|
|
|
if err = validate.ValidateEncryptKeys(&config.ExternalCfg.EncryptCommon); err != nil {
|
2024-11-27 15:01:51 +00:00
|
|
|
log.Fatalf("error validating encrypted: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateTG(config.NotificationBotToken, config.NotificationRsPayChannel, config.NotificationChannel); err != nil {
|
|
|
|
log.Fatalf("error validating tg enviroments: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-12-02 08:59:38 +00:00
|
|
|
if err = validate.ValidateMongo(config.ExternalCfg.Database); err != nil {
|
2024-11-28 12:31:54 +00:00
|
|
|
log.Fatalf("error validating mongodb: %v", err)
|
|
|
|
}
|
2024-11-27 15:01:51 +00:00
|
|
|
}
|
|
|
|
|
2024-11-28 12:31:54 +00:00
|
|
|
// 38 fields
|
2024-11-27 15:01:51 +00:00
|
|
|
func loadConfig() (*models.Config, error) {
|
2024-11-28 12:31:54 +00:00
|
|
|
config := models.Config{
|
|
|
|
ExternalCfg: models.ExternalCfg{
|
|
|
|
JwtCfg: models.JWTConfiguration{
|
2024-12-06 12:23:44 +00:00
|
|
|
JwtPublicKey: os.Getenv("JWT_PUBLIC_KEY"),
|
|
|
|
JwtAudience: os.Getenv("JWT_AUDIENCE"),
|
|
|
|
JwtIssuer: os.Getenv("JWT_ISSUER"),
|
2024-11-28 12:31:54 +00:00
|
|
|
},
|
|
|
|
Database: mongo.Configuration{
|
2024-11-28 16:14:53 +00:00
|
|
|
URL: os.Getenv("MONGO_URL"),
|
2024-11-28 12:31:54 +00:00
|
|
|
DatabaseName: os.Getenv("MONGO_DB_NAME"),
|
|
|
|
},
|
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2024-12-06 12:23:44 +00:00
|
|
|
ClientHttpURL: os.Getenv("CLIENT_HTTP_URL"),
|
|
|
|
AdminHttpURL: os.Getenv("ADMIN_HTTP_URL"),
|
|
|
|
GrpcURL: os.Getenv("GRPC_URL"),
|
|
|
|
GrpcDomain: os.Getenv("GRPC_DOMAIN"),
|
|
|
|
KafkaBrokers: strings.Split(os.Getenv("KAFKA_BROKERS"), ","),
|
|
|
|
KafkaTopicTariff: os.Getenv("KAFKA_TOPIC_TARIFF"),
|
|
|
|
AuthServiceURL: os.Getenv("AUTH_SERVICE_URL"),
|
|
|
|
HubadminServiceURL: os.Getenv("HUBADMIN_SERVICE_URL"),
|
|
|
|
CurrencyServiceURL: os.Getenv("CURRENCY_SERVICE_URL"),
|
|
|
|
DiscountServiceGRPCURL: os.Getenv("DISCOUNT_SERVICE_GRPC_URL"),
|
|
|
|
PaymentServiceGRPCURL: os.Getenv("PAYMENT_SERVICE_GRPC_URL"),
|
|
|
|
VerificationServiceURL: os.Getenv("VERIFICATION_SERVICE_URL"),
|
|
|
|
TemplategenServiceURL: os.Getenv("TEMPLATEGEN_SERVICE_URL"),
|
|
|
|
CodewordServiceGRPCURL: os.Getenv("CODEWORD_SERVICE_GRPC_URL"),
|
|
|
|
TrashLogHost: os.Getenv("TRASH_LOG_HOST"),
|
|
|
|
NotificationBotToken: os.Getenv("NOTIFICATION_BOT_TOKEN"),
|
|
|
|
NotificationRsPayChannel: envToInt64(os.Getenv("NOTIFICATION_RS_PAY_CHANNEL")),
|
|
|
|
NotificationChannel: envToInt64(os.Getenv("NOTIFICATION_CHANNEL")),
|
|
|
|
AdminURL: os.Getenv("ADMIN_FRONT_URL"),
|
2024-11-27 15:01:51 +00:00
|
|
|
}
|
2024-11-28 12:31:54 +00:00
|
|
|
|
2024-11-27 15:01:51 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
2024-11-28 12:33:30 +00:00
|
|
|
func envToInt64(str string) int64 {
|
2024-11-28 12:31:54 +00:00
|
|
|
n, err := strconv.ParseInt(str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-11-27 15:01:51 +00:00
|
|
|
func validateURLs(urls []string) error {
|
|
|
|
for index, u := range urls {
|
|
|
|
if u == "" {
|
|
|
|
return fmt.Errorf("empty url, index: %d", index)
|
|
|
|
}
|
|
|
|
// todo check the liveness of these URLs, many services do not support
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateKafka(brokers []string, topic string) error {
|
|
|
|
if len(brokers) == 0 {
|
|
|
|
return fmt.Errorf("kafka brokers is empty")
|
|
|
|
}
|
|
|
|
if topic == "" {
|
|
|
|
return fmt.Errorf("kafka topic is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range brokers {
|
|
|
|
if addr == "" {
|
|
|
|
return fmt.Errorf("empty kafka broker")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
kafkaTariffClient, err := kgo.NewClient(
|
|
|
|
kgo.SeedBrokers(brokers...),
|
|
|
|
kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()),
|
|
|
|
kgo.DefaultProduceTopic(topic),
|
|
|
|
kgo.ConsumeTopics(topic),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer kafkaTariffClient.Close()
|
|
|
|
|
|
|
|
err = kafkaTariffClient.Ping(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMail(cfg models.MailClientCfg) error {
|
|
|
|
if cfg.MailAddress == "" {
|
|
|
|
return fmt.Errorf("mail address is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ApiUrl == "" {
|
|
|
|
return fmt.Errorf("mail api url is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ApiKey == "" {
|
|
|
|
return fmt.Errorf("mail api key is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Sender == "" {
|
|
|
|
return fmt.Errorf("mail sender is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := fiber.AcquireClient()
|
|
|
|
req := client.Get("https://api.smtp.bz/v1/user")
|
|
|
|
req.Set("Authorization", cfg.ApiKey)
|
|
|
|
|
|
|
|
code, _, _ := req.Bytes()
|
|
|
|
if code != fiber.StatusOK {
|
|
|
|
return fmt.Errorf("invalid smtp code, no auth: %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNotEmpty(config *models.Config) error {
|
|
|
|
if config.ExternalCfg.EncryptCommon.PrivKey == "" {
|
|
|
|
return fmt.Errorf("invalid private encrypt key")
|
|
|
|
}
|
|
|
|
if config.ExternalCfg.EncryptCommon.PubKey == "" {
|
|
|
|
return fmt.Errorf("invalid publice encrypt key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTG(notificationBotToken string, notificationRsPayChannel int64, notificationChannel int64) error {
|
2024-12-02 08:59:38 +00:00
|
|
|
err := validate.ValidateTgToken(notificationBotToken)
|
2024-11-27 15:01:51 +00:00
|
|
|
if err != nil {
|
2024-12-02 08:59:38 +00:00
|
|
|
return err
|
2024-11-27 15:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if notificationChannel == 0 {
|
|
|
|
return fmt.Errorf("notificationChannel is not set")
|
|
|
|
}
|
|
|
|
if notificationRsPayChannel == 0 {
|
|
|
|
return fmt.Errorf("notificationRsPayChannel is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|