Skip to content

Commit

Permalink
Merge pull request #10 from carverauto/updates/test_updates
Browse files Browse the repository at this point in the history
Updates/test updates
  • Loading branch information
mfreeman451 authored Oct 6, 2024
2 parents c79f2cb + 480135a commit 63f1899
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 248 deletions.
21 changes: 21 additions & 0 deletions cmd/api/migrations/20240226153000_create_collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build !migrations

package migrations

import (
"gofr.dev/pkg/gofr/migration"
)

func createCollections() migration.Migrate {
return migration.Migrate{
UP: func(d migration.Datasource) error {
collections := []string{"tenants", "users", "api_keys"}
for _, coll := range collections {
if err := d.Mongo.CreateCollection(d.Context, coll); err != nil {

Check failure on line 14 in cmd/api/migrations/20240226153000_create_collections.go

View workflow job for this annotation

GitHub Actions / lint

d.Mongo undefined (type migration.Datasource has no field or method Mongo)

Check failure on line 14 in cmd/api/migrations/20240226153000_create_collections.go

View workflow job for this annotation

GitHub Actions / lint

d.Context undefined (type migration.Datasource has no field or method Context)
return err
}
}
return nil
},
}
}
35 changes: 35 additions & 0 deletions cmd/api/migrations/20240226153100_create_indexes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build !migrations

package migrations

import (
"gofr.dev/pkg/gofr/migration"
)

func createIndexes() migration.Migrate {
return migration.Migrate{
UP: func(d migration.Datasource) error {
// Create unique index on tenant name
if err := d.Mongo.CreateIndex(d.Context, "tenants", map[string]interface{}{"name": 1}, true); err != nil {

Check failure on line 13 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Mongo undefined (type migration.Datasource has no field or method Mongo)

Check failure on line 13 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Context undefined (type migration.Datasource has no field or method Context)
return err
}

// Create unique index on user email
if err := d.Mongo.CreateIndex(d.Context, "users", map[string]interface{}{"email": 1}, true); err != nil {

Check failure on line 18 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Mongo undefined (type migration.Datasource has no field or method Mongo)

Check failure on line 18 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Context undefined (type migration.Datasource has no field or method Context)
return err
}

// Create index on user's tenant_id for faster queries
if err := d.Mongo.CreateIndex(d.Context, "users", map[string]interface{}{"tenant_id": 1}, false); err != nil {

Check failure on line 23 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Mongo undefined (type migration.Datasource has no field or method Mongo)

Check failure on line 23 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Context undefined (type migration.Datasource has no field or method Context)
return err
}

// Create unique index on API key
if err := d.Mongo.CreateIndex(d.Context, "api_keys", map[string]interface{}{"key": 1}, true); err != nil {

Check failure on line 28 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Mongo undefined (type migration.Datasource has no field or method Mongo)

Check failure on line 28 in cmd/api/migrations/20240226153100_create_indexes.go

View workflow job for this annotation

GitHub Actions / lint

d.Context undefined (type migration.Datasource has no field or method Context)
return err
}

return nil
},
}
}
2 changes: 2 additions & 0 deletions cmd/api/migrations/all.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !migrations

package migrations

import (
Expand Down
36 changes: 23 additions & 13 deletions cmd/event-ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func main() {
if err != nil {
log.Fatalf("Failed to connect to gRPC server: %v", err)
}

defer conn.Close()

// Create gRPC event forwarder
Expand All @@ -46,7 +45,7 @@ func main() {
jwtMiddleware.Validate,
middleware.AuthenticateAPIKey,
middleware.RequireRole("admin", "event_publisher"),
func(cc *customctx.Context) (interface{}, error) {
func(cc customctx.Context) (interface{}, error) {
return httpServer.HandleEvent(cc)
},
))
Expand All @@ -58,24 +57,35 @@ func main() {
// combineMiddleware chains multiple middleware functions together
func combineMiddleware(middlewares ...interface{}) gofr.Handler {
return func(c *gofr.Context) (interface{}, error) {
// Create the initial custom context from the GoFr context
cc := customctx.NewCustomContext(c)

var handler func(*customctx.Context) (interface{}, error)
// Define the final handler that will be called after applying all middleware
finalHandler := func(ctx customctx.Context) (interface{}, error) {
return nil, eventingest.NewInternalError("No handler provided")
}

// Apply middlewares in reverse order
// Apply middlewares in reverse order to build the middleware chain
for i := len(middlewares) - 1; i >= 0; i-- {
switch m := middlewares[i].(type) {
case func(*customctx.Context) (interface{}, error):
handler = m
case func(func(*customctx.Context) (interface{}, error)) func(*customctx.Context) (interface{}, error):
handler = m(handler)
case func(gofr.Handler) gofr.Handler:
return m(func(*gofr.Context) (interface{}, error) {
return handler(cc)
})(c)
case func(customctx.Context) (interface{}, error):
// Set the final handler to the current one if no other handler is set
if i == len(middlewares)-1 {
finalHandler = m
} else {
// Wrap the final handler in the current function
// nextHandler := finalHandler
finalHandler = func(ctx customctx.Context) (interface{}, error) {
return m(ctx)
}
}
case func(func(customctx.Context) (interface{}, error)) func(customctx.Context) (interface{}, error):
// Wrap the final handler in middleware if it's a middleware function
finalHandler = m(finalHandler)
}
}

return handler(cc)
// Execute the final middleware chain with the custom context
return finalHandler(cc)
}
}
211 changes: 0 additions & 211 deletions cmd/eventrunner/main_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !migrations

package migrations

import (
Expand Down
2 changes: 2 additions & 0 deletions cmd/eventrunner/migrations/all.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !migrations

package migrations

import "gofr.dev/pkg/gofr/migration"
Expand Down
14 changes: 7 additions & 7 deletions pkg/api/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package handlers

import (
"github.com/carverauto/eventrunner/pkg/api/models"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"gofr.dev/pkg/gofr"
)

type TenantHandler struct{}

func (*TenantHandler) Create(c *gofr.Context) (interface{}, error) {
func (*TenantHandler) Create(c *gofr.Context) (models.Tenant, error) {
var tenant models.Tenant
if err := c.Bind(&tenant); err != nil {
return nil, err
return models.Tenant{}, err
}

result, err := c.Mongo.InsertOne(c, "tenants", tenant)
if err != nil {
return nil, err
return models.Tenant{}, err
}

tenant.ID = result.(primitive.ObjectID)
tenant.ID = result.(uuid.UUID)

return tenant, nil
}
Expand All @@ -47,13 +47,13 @@ func (*UserHandler) Create(c *gofr.Context) (interface{}, error) {
return nil, err
}

user.ID = result.(primitive.ObjectID)
user.ID = result.(uuid.UUID)

return user, nil
}

func (*UserHandler) GetAll(c *gofr.Context) (interface{}, error) {
tenantID, err := primitive.ObjectIDFromHex(c.Param("tenant_id"))
tenantID, err := uuid.Parse(c.Param("tenant_id"))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 63f1899

Please sign in to comment.