From 41f949c5141f5d476e826d62c895bfc1d38b3521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmetcan=20=C3=96ZCAN?= Date: Wed, 11 Sep 2024 10:40:22 +0300 Subject: [PATCH] feat(couchbase) Add example --- example/withcouchbase/config.yaml | 3 ++ example/withcouchbase/controller.go | 34 ++++++++++++++++++++++ example/withcouchbase/main.go | 28 ++++++++++++++++++ example/withcouchbase/repository.go | 45 +++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 example/withcouchbase/config.yaml create mode 100644 example/withcouchbase/controller.go create mode 100644 example/withcouchbase/main.go create mode 100644 example/withcouchbase/repository.go diff --git a/example/withcouchbase/config.yaml b/example/withcouchbase/config.yaml new file mode 100644 index 0000000..6e97e9b --- /dev/null +++ b/example/withcouchbase/config.yaml @@ -0,0 +1,3 @@ +couchbase: + host: "" + bucketname: "foo_bucket" \ No newline at end of file diff --git a/example/withcouchbase/controller.go b/example/withcouchbase/controller.go new file mode 100644 index 0000000..23a180b --- /dev/null +++ b/example/withcouchbase/controller.go @@ -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), + } +} diff --git a/example/withcouchbase/main.go b/example/withcouchbase/main.go new file mode 100644 index 0000000..9ebfe07 --- /dev/null +++ b/example/withcouchbase/main.go @@ -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()) +} diff --git a/example/withcouchbase/repository.go b/example/withcouchbase/repository.go new file mode 100644 index 0000000..82b74af --- /dev/null +++ b/example/withcouchbase/repository.go @@ -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), + }) + if err != nil { + return nil, err + } + + if err := resp.Content(result); err != nil { + return nil, err + } + + return result, nil +}