Skip to content

Commit

Permalink
Updated vercel tool api to call resource applcation
Browse files Browse the repository at this point in the history
added empty handle
  • Loading branch information
futugyou committed Oct 25, 2024
1 parent 98f9929 commit ab7ca02
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
86 changes: 80 additions & 6 deletions infr-project/api/tool.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}
}
16 changes: 16 additions & 0 deletions infr-project/application/resource_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package application

import (
"context"
"time"

models "github.com/futugyou/infr-project/view_models"
)
Expand All @@ -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"`
}
2 changes: 1 addition & 1 deletion infr-project/domain/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ab7ca02

Please sign in to comment.