Skip to content

Commit

Permalink
added db integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Danushka96 committed Nov 28, 2024
1 parent 37b2c6f commit fba02c7
Show file tree
Hide file tree
Showing 23 changed files with 2,407 additions and 153 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,54 @@ type MyRepository struct {
}
```

### MongoDB Configuration

GinBoot provides a flexible MongoDB configuration system through the `MongoConfig` struct. Here's how to use it:

```go
// Create a new MongoDB configuration
config := ginboot.NewMongoConfig().
WithHost("localhost", 27017).
WithCredentials("username", "password").
WithDatabase("mydb").
WithOption("authSource", "admin")

// Connect to MongoDB
db, err := config.Connect()
if err != nil {
log.Fatal(err)
}

// Create a repository for your model
type User struct {
ID string `bson:"_id"`
Name string `bson:"name"`
}

func (u User) GetID() interface{} { return u.ID }
func (u User) SetID(id interface{}) { u.ID = id.(string) }
func (u *User) GetCollectionName() string { return "users" }

// Initialize the repository
userRepo := ginboot.NewMongoRepository[User](db)

// Use the repository
user := User{Name: "John Doe"}
err = userRepo.Save(user)
```

For more secure handling of credentials, you can use environment variables:

```go
config := ginboot.NewMongoConfig().
WithHost(os.Getenv("MONGO_HOST"), 27017).
WithCredentials(
os.Getenv("MONGO_USERNAME"),
os.Getenv("MONGO_PASSWORD"),
).
WithDatabase(os.Getenv("MONGO_DATABASE"))
```

## API Request Context

GinBoot simplifies the extraction of request and authentication context from the Gin context, making it easier to handle requests in controllers.
Expand Down
88 changes: 88 additions & 0 deletions dynamo_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package ginboot

import (
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"golang.org/x/net/context"
)

type DynamoConfig struct {
Region string
AccessKeyID string
SecretAccessKey string
Endpoint string
Profile string
}

func NewDynamoConfig() *DynamoConfig {
return &DynamoConfig{
Region: "us-east-1",
Endpoint: "http://localhost:8000",
}
}

func (c *DynamoConfig) WithRegion(region string) *DynamoConfig {
c.Region = region
return c
}

func (c *DynamoConfig) WithCredentials(accessKeyID, secretAccessKey string) *DynamoConfig {
c.AccessKeyID = accessKeyID
c.SecretAccessKey = secretAccessKey
return c
}

func (c *DynamoConfig) WithEndpoint(endpoint string) *DynamoConfig {
c.Endpoint = endpoint
return c
}

func (c *DynamoConfig) WithProfile(profile string) *DynamoConfig {
c.Profile = profile
return c
}

func (c *DynamoConfig) Connect() (*dynamodb.Client, error) {
ctx := context.Background()
var cfg aws.Config
var err error

if c.Profile != "" {
cfg, err = config.LoadDefaultConfig(ctx,
config.WithRegion(c.Region),
config.WithSharedConfigProfile(c.Profile),
)
} else if c.AccessKeyID != "" && c.SecretAccessKey != "" {
cfg, err = config.LoadDefaultConfig(ctx,
config.WithRegion(c.Region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
c.AccessKeyID,
c.SecretAccessKey,
"",
)),
)
} else {
cfg, err = config.LoadDefaultConfig(ctx, config.WithRegion(c.Region))
}

if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %v", err)
}

options := &dynamodb.Options{
Credentials: cfg.Credentials,
Region: cfg.Region,
}

if c.Endpoint != "" {
options.EndpointResolver = dynamodb.EndpointResolverFromURL(c.Endpoint)
}

return dynamodb.NewFromConfig(cfg, func(o *dynamodb.Options) {
*o = *options
}), nil
}
Loading

0 comments on commit fba02c7

Please sign in to comment.