diff --git a/infr-project/api/tool.go b/infr-project/api/tool.go index 2f2832ec..c508ebd4 100644 --- a/infr-project/api/tool.go +++ b/infr-project/api/tool.go @@ -1,19 +1,24 @@ package api import ( + "context" "encoding/json" - "fmt" "net/http" "os" + "reflect" "time" _ "github.com/joho/godotenv/autoload" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "github.com/redis/go-redis/v9" "github.com/futugyou/extensions" + "github.com/futugyou/infr-project/application" "github.com/futugyou/infr-project/controller" + infra "github.com/futugyou/infr-project/infrastructure_mongo" models "github.com/futugyou/infr-project/view_models" ) @@ -73,19 +78,88 @@ func redistool(_ *controller.Controller, r *http.Request, w http.ResponseWriter) } func eventHandler(_ *controller.Controller, r *http.Request, w http.ResponseWriter) { + ctx := r.Context() bearer := r.Header.Get("Authorization") if bearer != os.Getenv("TRIGGER_AUTH_KEY") { w.Write([]byte("Authorization code error")) w.WriteHeader(500) return } - var aux models.TriggerEvent - if err := json.NewDecoder(r.Body).Decode(&aux); err != nil { + var event models.TriggerEvent + if err := json.NewDecoder(r.Body).Decode(&event); err != nil { w.Write([]byte(err.Error())) w.WriteHeader(500) return } - fmt.Println(aux) - w.Write([]byte("ok")) - w.WriteHeader(200) + + service, err := createResourceQueryService(ctx) + if err != nil { + w.Write([]byte(err.Error())) + w.WriteHeader(500) + return + } + + dataType := getDataType(event.TableName) + if dataType == nil { + w.Write([]byte("can not find data type")) + w.WriteHeader(500) + return + } + + dataBytes, err := json.Marshal(event.Data) + if err != nil { + w.Write([]byte(err.Error())) + w.WriteHeader(500) + return + } + + dataInstance := reflect.New(dataType).Interface() + + if err := json.Unmarshal(dataBytes, dataInstance); err != nil { + w.Write([]byte(err.Error())) + w.WriteHeader(500) + return + } + + if resourceData, ok := dataInstance.(*application.ResourceChangeData); ok { + if err = service.HandleResourceChaged(ctx, *resourceData); err != nil { + w.Write([]byte(err.Error())) + w.WriteHeader(500) + return + } + } else { + w.Write([]byte("can not find event handler")) + w.WriteHeader(200) + } +} + +func createResourceQueryService(ctx context.Context) (*application.ResourceQueryService, error) { + queryRepo, err := createResourceQueryRepository(ctx) + if err != nil { + return nil, err + } + return application.NewResourceQueryService(queryRepo), nil +} + +func createResourceQueryRepository(ctx context.Context) (*infra.ResourceQueryRepository, error) { + config := infra.QueryDBConfig{ + DBName: os.Getenv("query_db_name"), + ConnectString: os.Getenv("query_mongodb_url"), + } + + client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.ConnectString)) + if err != nil { + return nil, err + } + + return infra.NewResourceQueryRepository(client, config), nil +} + +func getDataType(tableName string) reflect.Type { + switch tableName { + case "resource_events": + return reflect.TypeOf(application.ResourceChangeData{}) + default: + return nil + } } diff --git a/infr-project/application/resource_query.go b/infr-project/application/resource_query.go index 44e28646..02c6c52d 100644 --- a/infr-project/application/resource_query.go +++ b/infr-project/application/resource_query.go @@ -2,6 +2,7 @@ package application import ( "context" + "time" models "github.com/futugyou/infr-project/view_models" ) @@ -23,3 +24,18 @@ func (s *ResourceQueryService) GetAllResources(ctx context.Context) ([]models.Re func (s *ResourceQueryService) CurrentResource(ctx context.Context, id string) (*models.ResourceView, error) { return s.repository.Get(ctx, id) } + +func (s *ResourceQueryService) HandleResourceChaged(ctx context.Context, data ResourceChangeData) error { + return nil +} + +type ResourceChangeData struct { + Id string `bson:"id" json:"id"` + ResourceVersion int `bson:"version" json:"version"` + EventType string `bson:"event_type" json:"event_type"` + CreatedAt time.Time `bson:"created_at" json:"created_at"` + Name string `bson:"name" json:"name"` + Type string `bson:"type" json:"type"` + Data string `bson:"data" json:"data"` + Tags []string `bson:"tags" json:"tags"` +} diff --git a/infr-project/domain/aggregate.go b/infr-project/domain/aggregate.go index d6df1f12..2e6e110c 100644 --- a/infr-project/domain/aggregate.go +++ b/infr-project/domain/aggregate.go @@ -7,7 +7,7 @@ type IAggregateRoot interface { } type Aggregate struct { - Id string `json:"id"` + Id string `json:"id" bson:"id" redis:"id"` } func (a Aggregate) AggregateId() string {