some refactore
This commit is contained in:
parent
38a20aa977
commit
49533bf235
14
deployments/test/docker-compose.yaml
Normal file
14
deployments/test/docker-compose.yaml
Normal file
@ -0,0 +1,14 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
mongodb:
|
||||
image: mongo:latest
|
||||
container_name: mongodb
|
||||
ports:
|
||||
- "27017:27017"
|
||||
networks:
|
||||
- app_network
|
||||
|
||||
networks:
|
||||
app_network:
|
||||
driver: bridge
|
@ -2,7 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"fmt"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@ -29,7 +29,9 @@ func Run(cfg *config.Config) {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoUri))
|
||||
fmt.Println(cfg.MongoUri)
|
||||
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
logger.Fatal("MongoClient", zap.Error(err))
|
||||
}
|
||||
@ -46,12 +48,12 @@ func Run(cfg *config.Config) {
|
||||
if err != nil {
|
||||
logger.Fatal("Repositories", zap.Error(err))
|
||||
}
|
||||
tgBot, err := tgbotapi.NewBotAPI(cfg.TelegramToken)
|
||||
if err != nil {
|
||||
logger.Fatal("TelegramBotApi", zap.Error(err))
|
||||
}
|
||||
//tgBot, err := tgbotapi.NewBotAPI(cfg.TelegramToken)
|
||||
//if err != nil {
|
||||
// logger.Fatal("TelegramBotApi", zap.Error(err))
|
||||
//}
|
||||
|
||||
telegram := client.NewTelegram(logger, tgBot, cfg.TelegramChannelID)
|
||||
telegram := client.NewTelegram(logger, nil, cfg.TelegramChannelID)
|
||||
cons := initialize.NewControllers(reps, telegram, client.NewCustomer(logger, cfg.CustomerSvcAddress))
|
||||
|
||||
httpSrv := server.NewHTTP(cfg, logger).Register(cons.List()...)
|
||||
|
@ -18,6 +18,7 @@ func NewCustomer(logger *zap.Logger, address string) *Customer {
|
||||
}
|
||||
|
||||
func (c *Customer) UpdateAccountVerification(userId, status string) (*models.RespUpdateVerificationStatus, error) {
|
||||
fmt.Println(c.address)
|
||||
agent := fiber.Patch(fmt.Sprintf("%s/account/%s", c.address, userId))
|
||||
agent.JSON(&models.ReqCreateVerification{Status: status})
|
||||
|
||||
|
@ -6,15 +6,15 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
TelegramToken string `env:"TELEGRAM_TOKEN,required"`
|
||||
TelegramChannelID int64 `env:"TELEGRAM_CHANNEL_ID,required"`
|
||||
HttpAddress string `env:"HTTP_ADDRESS" envDefault:":80"`
|
||||
MongoUri string `env:"MONGO_URI,required"`
|
||||
DatabaseName string `env:"MONGO_DATABASE_NAME,required"`
|
||||
S3Endpoint string `env:"S3_ENDPOINT,required"`
|
||||
S3AccessKeyID string `env:"S3_ACCESS_KEY_ID,required"`
|
||||
S3SecretKey string `env:"S3_SECRET_KEY,required"`
|
||||
CustomerSvcAddress string `env:"CUSTOMER_SVC_ADDRESS,required"`
|
||||
TelegramToken string `env:"TELEGRAM_TOKEN" envDefault:"1"`
|
||||
TelegramChannelID int64 `env:"TELEGRAM_CHANNEL_ID"envDefault:"1"`
|
||||
HttpAddress string `env:"HTTP_ADDRESS" envDefault:":8080"`
|
||||
MongoUri string `env:"MONGO_URI" envDefault:"mongodb://localhost:27017"`
|
||||
DatabaseName string `env:"MONGO_DATABASE_NAME" envDefault:"admin"`
|
||||
S3Endpoint string `env:"S3_ENDPOINT" envDefault:"1"`
|
||||
S3AccessKeyID string `env:"S3_ACCESS_KEY_ID" envDefault:"1"`
|
||||
S3SecretKey string `env:"S3_SECRET_KEY" envDefault:"1"`
|
||||
CustomerSvcAddress string `env:"CUSTOMER_SVC_ADDRESS" envDefault:"http://localhost:8003"`
|
||||
}
|
||||
|
||||
func NewConfig(file ...string) (*Config, error) {
|
||||
|
@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@ -131,30 +132,32 @@ func (r *VerificationController) SetVerificationStatus(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
fmt.Println(req)
|
||||
errValidate := validateStruct(&req)
|
||||
if errValidate != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||||
}
|
||||
|
||||
_, err = r.repository.Update(c.Context(), &models.Verification{
|
||||
ID: req.ID,
|
||||
Accepted: req.Accepted,
|
||||
Status: req.Status,
|
||||
Comment: req.Comment,
|
||||
TaxNumber: req.TaxNumber,
|
||||
ID: req.ID,
|
||||
Accepted: req.Accepted,
|
||||
Status: req.Status,
|
||||
Comment: req.Comment,
|
||||
TaxNumber: req.TaxNumber,
|
||||
})
|
||||
|
||||
fmt.Println("repository.Update")
|
||||
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if req.Accepted {
|
||||
_, err := r.customer.UpdateAccountVerification(req.ID, req.Status)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
if req.Accepted {
|
||||
_, err := r.customer.UpdateAccountVerification(req.ID, req.Status)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
@ -17,3 +17,13 @@ type VerificationFile struct {
|
||||
Name string `json:"name" bson:"name"`
|
||||
Url string `json:"url" bson:"url"`
|
||||
}
|
||||
|
||||
type Test struct {
|
||||
UserID string `json:"userID" bson:"user_id,omitempty"`
|
||||
Accepted bool `json:"accepted" bson:"accepted"`
|
||||
Status string `json:"status" bson:"status,omitempty"`
|
||||
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
|
||||
Comment string `json:"comment" bson:"comment,omitempty"`
|
||||
Files []VerificationFile `json:"files" bson:"files,omitempty"`
|
||||
TaxNumber string `json:"taxnumber" bson:"taxnumber,omitempty"`
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
@ -10,8 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/policy"
|
||||
"github.com/minio/minio-go/v7/pkg/set"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@ -33,6 +30,12 @@ const (
|
||||
)
|
||||
|
||||
func NewVerificationRepository(logger *zap.Logger, mongoDb *mongo.Database, s3 *minio.Client) *VerificationRepository {
|
||||
// Получаем список названий коллекций
|
||||
collections, err := mongoDb.ListCollectionNames(context.Background(), bson.M{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(collections)
|
||||
return &VerificationRepository{
|
||||
logger: logger,
|
||||
mongo: mongoDb.Collection(VerificationCollection),
|
||||
@ -41,67 +44,67 @@ func NewVerificationRepository(logger *zap.Logger, mongoDb *mongo.Database, s3 *
|
||||
}
|
||||
|
||||
func (r *VerificationRepository) Init(ctx context.Context) error {
|
||||
ok, err := r.s3.BucketExists(ctx, VerificationBucket)
|
||||
if r.err(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if !ok {
|
||||
err = r.s3.MakeBucket(ctx, VerificationBucket, minio.MakeBucketOptions{ObjectLocking: false})
|
||||
if r.err(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
policyConsoleStatement := policy.Statement{
|
||||
Actions: set.CreateStringSet("*"),
|
||||
Conditions: policy.ConditionMap{
|
||||
"StringLike": policy.ConditionKeyMap{
|
||||
"aws:referer": set.CreateStringSet(fmt.Sprintf("https://console.cloud.yandex.*/folders/*/storage/buckets/%s*", VerificationBucket)),
|
||||
},
|
||||
},
|
||||
Effect: "Allow",
|
||||
Principal: policy.User{AWS: set.CreateStringSet("*")},
|
||||
Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
Sid: "console-statement",
|
||||
}
|
||||
|
||||
policyServiceAccount := policy.Statement{
|
||||
Actions: set.CreateStringSet("*"),
|
||||
Conditions: nil,
|
||||
Effect: "Allow",
|
||||
Principal: policy.User{CanonicalUser: set.CreateStringSet("ajelmc4tjbct675tjdh9")},
|
||||
Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
Sid: "service-account-statement",
|
||||
}
|
||||
|
||||
policySharingBucket := policy.Statement{
|
||||
Actions: set.CreateStringSet("s3:GetObject"),
|
||||
Conditions: nil,
|
||||
Effect: "Allow",
|
||||
Principal: policy.User{AWS: set.CreateStringSet("*")},
|
||||
Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
Sid: "sharing-bucket",
|
||||
}
|
||||
|
||||
p := policy.BucketAccessPolicy{Version: "2012-10-17", Statements: []policy.Statement{
|
||||
policyConsoleStatement,
|
||||
policyServiceAccount,
|
||||
policySharingBucket,
|
||||
}}
|
||||
|
||||
outPolicy, err := json.Marshal(&p)
|
||||
if r.err(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.s3.SetBucketPolicy(ctx, VerificationBucket, string(outPolicy))
|
||||
if r.err(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//ok, err := r.s3.BucketExists(ctx, VerificationBucket)
|
||||
//if r.err(err) {
|
||||
// return err
|
||||
//}
|
||||
//
|
||||
//if !ok {
|
||||
// err = r.s3.MakeBucket(ctx, VerificationBucket, minio.MakeBucketOptions{ObjectLocking: false})
|
||||
// if r.err(err) {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// policyConsoleStatement := policy.Statement{
|
||||
// Actions: set.CreateStringSet("*"),
|
||||
// Conditions: policy.ConditionMap{
|
||||
// "StringLike": policy.ConditionKeyMap{
|
||||
// "aws:referer": set.CreateStringSet(fmt.Sprintf("https://console.cloud.yandex.*/folders/*/storage/buckets/%s*", VerificationBucket)),
|
||||
// },
|
||||
// },
|
||||
// Effect: "Allow",
|
||||
// Principal: policy.User{AWS: set.CreateStringSet("*")},
|
||||
// Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
// fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
// Sid: "console-statement",
|
||||
// }
|
||||
//
|
||||
// policyServiceAccount := policy.Statement{
|
||||
// Actions: set.CreateStringSet("*"),
|
||||
// Conditions: nil,
|
||||
// Effect: "Allow",
|
||||
// Principal: policy.User{CanonicalUser: set.CreateStringSet("ajelmc4tjbct675tjdh9")},
|
||||
// Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
// fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
// Sid: "service-account-statement",
|
||||
// }
|
||||
//
|
||||
// policySharingBucket := policy.Statement{
|
||||
// Actions: set.CreateStringSet("s3:GetObject"),
|
||||
// Conditions: nil,
|
||||
// Effect: "Allow",
|
||||
// Principal: policy.User{AWS: set.CreateStringSet("*")},
|
||||
// Resources: set.CreateStringSet(fmt.Sprintf("arn:aws:s3:::%s/*", VerificationBucket),
|
||||
// fmt.Sprintf("arn:aws:s3:::%s", VerificationBucket)),
|
||||
// Sid: "sharing-bucket",
|
||||
// }
|
||||
//
|
||||
// p := policy.BucketAccessPolicy{Version: "2012-10-17", Statements: []policy.Statement{
|
||||
// policyConsoleStatement,
|
||||
// policyServiceAccount,
|
||||
// policySharingBucket,
|
||||
// }}
|
||||
//
|
||||
// outPolicy, err := json.Marshal(&p)
|
||||
// if r.err(err) {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// err = r.s3.SetBucketPolicy(ctx, VerificationBucket, string(outPolicy))
|
||||
// if r.err(err) {
|
||||
// return err
|
||||
// }
|
||||
//}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -246,12 +249,25 @@ func (r *VerificationRepository) Get(ctx context.Context, id string) (*models.Ve
|
||||
func (r *VerificationRepository) Update(ctx context.Context, record *models.Verification) (*models.Verification, error) {
|
||||
record.UpdatedAt = time.Now()
|
||||
|
||||
var result models.Verification
|
||||
err := r.mongo.FindOneAndUpdate(ctx, bson.M{"_id": record.ID}, bson.M{"$set": record}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&result)
|
||||
if r.err(err) {
|
||||
return nil, err
|
||||
test := models.Test{
|
||||
UserID: record.UserID,
|
||||
Accepted: record.Accepted,
|
||||
Status: record.Status,
|
||||
UpdatedAt: record.UpdatedAt,
|
||||
Comment: record.Comment,
|
||||
Files: record.Files,
|
||||
TaxNumber: record.TaxNumber,
|
||||
}
|
||||
|
||||
id, _ := primitive.ObjectIDFromHex(record.ID)
|
||||
|
||||
var result models.Verification
|
||||
err := r.mongo.FindOneAndUpdate(ctx, bson.M{"_id": id}, bson.M{"$set": test}, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&result)
|
||||
if r.err(err) {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println(result)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gofiber/contrib/fiberzap"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||
"go.uber.org/zap"
|
||||
"penahub.gitlab.yandexcloud.net/backend/verification/internal/config"
|
||||
"penahub.gitlab.yandexcloud.net/backend/verification/internal/initialize"
|
||||
@ -22,12 +20,12 @@ func NewHTTP(cfg *config.Config, logger *zap.Logger) *HTTP {
|
||||
BodyLimit: 50 << 20,
|
||||
})
|
||||
|
||||
srv.Use(
|
||||
recover.New(recover.Config{EnableStackTrace: true}),
|
||||
fiberzap.New(fiberzap.Config{Logger: logger}),
|
||||
LocalJwt(),
|
||||
Jwt(),
|
||||
)
|
||||
//srv.Use(
|
||||
// recover.New(recover.Config{EnableStackTrace: true}),
|
||||
// fiberzap.New(fiberzap.Config{Logger: logger}),
|
||||
// LocalJwt(),
|
||||
// Jwt(),
|
||||
//)
|
||||
|
||||
return &HTTP{fiber: srv, cfg: cfg, logger: logger}
|
||||
}
|
||||
|
12
staging.env
12
staging.env
@ -1,3 +1,9 @@
|
||||
MONGO_DATABASE_NAME=verification
|
||||
HTTP_ADDRESS=:7035
|
||||
CUSTOMER_SVC_ADDRESS=https://admin.pena.digital/customer
|
||||
MONGO_DATABASE_NAME=admin
|
||||
HTTP_ADDRESS=:8080
|
||||
CUSTOMER_SVC_ADDRESS=http://localhost:8003
|
||||
S3_SECRET_KEY=1
|
||||
S3_ACCESS_KEY_ID=1
|
||||
S3_ENDPOINT=1
|
||||
MONGO_URI=mongodb://localhost:27017
|
||||
TELEGRAM_CHANNEL_ID=1
|
||||
TELEGRAM_TOKEN=1
|
Loading…
Reference in New Issue
Block a user