Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add couchbase module #33

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions example/withcouchbase/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
couchbase:
host: "<cb_host>"
bucketname: "foo_bucket"
34 changes: 34 additions & 0 deletions example/withcouchbase/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"

"github.com/Trendyol/chaki/modules/server/controller"
"github.com/Trendyol/chaki/modules/server/route"
)

type getFooRequest struct {
ID string `param:"id" validate:"required"`
}

type fooController struct {
controller.Controller
repo FooRepository
}

func newController(repo FooRepository) controller.Controller {
return &fooController{
Controller: controller.New("foo-controller").SetPrefix("/foo"),
repo: repo,
}
}

func (ct *fooController) getFoo(ctx context.Context, req getFooRequest) (*Foo, error) {
return ct.repo.Get(ctx, req.ID)
}

func (ct *fooController) Routes() []route.Route {
return []route.Route{
route.Get("/:id", ct.getFoo),
}
}
28 changes: 28 additions & 0 deletions example/withcouchbase/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"github.com/Trendyol/chaki"
"github.com/Trendyol/chaki/logger"
"github.com/Trendyol/chaki/modules/couchbase"
"github.com/Trendyol/chaki/modules/server"
)

func main() {
app := chaki.New()

app.WithOption(
chaki.WithConfigPath("config.yaml"),
)

app.Use(
server.Module(),
couchbase.Module(),
)

app.Provide(
NewRepository,
newController,
)

logger.Fatal(app.Start())
}
45 changes: 45 additions & 0 deletions example/withcouchbase/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"

"github.com/Trendyol/chaki/config"
"github.com/Trendyol/chaki/modules/couchbase"
"github.com/couchbase/gocb/v2"
)

type Foo struct {
ID string
Bar string
}

type FooRepository interface {
Get(ctx context.Context, id string) (*Foo, error)
}

type fooRepository struct {
coll *gocb.Collection
}

func NewRepository(cluster *gocb.Cluster, cfg *config.Config) FooRepository {
return &fooRepository{
coll: cluster.Bucket(cfg.GetString("couchbase.bucketname")).DefaultCollection(),
}
}

func (r *fooRepository) Get(ctx context.Context, id string) (*Foo, error) {
result := new(Foo)
resp, err := r.coll.Get(id, &gocb.GetOptions{
// Enable tracing
ParentSpan: couchbase.ParentSpan(ctx),
ispiroglu marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
return nil, err
}

if err := resp.Content(result); err != nil {
return nil, err
}

return result, nil
}
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/Trendyol/kafka-konsumer/v2 v2.3.3
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/couchbase/gocb/v2 v2.9.1
github.com/go-playground/validator/v10 v10.22.0
github.com/go-resty/resty/v2 v2.13.1
github.com/gofiber/contrib/fibernewrelic v1.3.0
Expand Down Expand Up @@ -37,6 +38,10 @@ require (
github.com/ansrivas/fiberprometheus/v2 v2.6.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/couchbase/gocbcore/v10 v10.5.1 // indirect
github.com/couchbase/gocbcoreps v0.1.3 // indirect
github.com/couchbase/goprotostellar v1.0.2 // indirect
github.com/couchbaselabs/gocbconnstr/v2 v2.0.0-20240607131231-fb385523de28 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -47,6 +52,8 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gofiber/adaptor/v2 v2.2.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand Down Expand Up @@ -86,15 +93,16 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
go.opentelemetry.io/contrib v1.20.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading