-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60807fb
commit 41f949c
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
couchbase: | ||
host: "<cb_host>" | ||
bucketname: "foo_bucket" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := resp.Content(result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |