Skip to content

Commit 472e491

Browse files
authored
story(issue-91): grpc implement a builder and run func (#92)
1 parent dbc6ee3 commit 472e491

27 files changed

+2072
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Go Reference](https://pkg.go.dev/badge/github.com/z5labs/humus.svg)](https://pkg.go.dev/github.com/z5labs/humus)
44
[![Go Report Card](https://goreportcard.com/badge/github.com/z5labs/humus)](https://goreportcard.com/report/github.com/z5labs/humus)
5-
![Coverage](https://img.shields.io/badge/Coverage-76.3%25-brightgreen)
5+
![Coverage](https://img.shields.io/badge/Coverage-78.2%25-brightgreen)
66
[![build](https://github.com/z5labs/humus/actions/workflows/build.yaml/badge.svg)](https://github.com/z5labs/humus/actions/workflows/build.yaml)
77

88
**humus one stop shop framework for all Z5Labs projects in Go.**

example/grpc/petstore/app/app.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025 Z5Labs and Contributors
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
package app
7+
8+
import (
9+
"context"
10+
11+
"github.com/z5labs/humus/example/grpc/petstore/pet/registrar"
12+
"github.com/z5labs/humus/example/internal/pet"
13+
"github.com/z5labs/humus/grpc"
14+
)
15+
16+
type Config struct {
17+
grpc.Config `config:",squash"`
18+
}
19+
20+
func Init(ctx context.Context, cfg Config) (*grpc.Api, error) {
21+
store := pet.NewStore()
22+
23+
api := grpc.NewApi()
24+
25+
registrar.Register(api, store)
26+
27+
return api, nil
28+
}

example/grpc/petstore/config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2025 Z5Labs and Contributors
2+
#
3+
# This software is released under the MIT License.
4+
# https://opensource.org/licenses/MIT
5+

example/grpc/petstore/main.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2025 Z5Labs and Contributors
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
package main
7+
8+
import (
9+
"bytes"
10+
_ "embed"
11+
12+
"github.com/z5labs/humus/example/grpc/petstore/app"
13+
"github.com/z5labs/humus/grpc"
14+
)
15+
16+
//go:embed config.yaml
17+
var configBytes []byte
18+
19+
func main() {
20+
grpc.Run(bytes.NewReader(configBytes), app.Init)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2025 Z5Labs and Contributors
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
package registrar
7+
8+
import (
9+
"context"
10+
11+
"github.com/z5labs/humus/example/grpc/petstore/petpb"
12+
"github.com/z5labs/humus/example/internal/pet"
13+
"github.com/z5labs/humus/internal/ptr"
14+
15+
"google.golang.org/grpc"
16+
)
17+
18+
type Store interface {
19+
Register(context.Context, *pet.RegisterRequest) (*pet.RegisterResponse, error)
20+
}
21+
22+
type Registrar struct {
23+
petpb.UnimplementedRegistrarServer
24+
25+
store Store
26+
}
27+
28+
func Register(sr grpc.ServiceRegistrar, store Store) {
29+
r := &Registrar{
30+
store: store,
31+
}
32+
33+
petpb.RegisterRegistrarServer(sr, r)
34+
}
35+
36+
func (r *Registrar) RegisterPet(ctx context.Context, req *petpb.RegisterPetRequest) (*petpb.RegisterPetResponse, error) {
37+
registerResp, err := r.store.Register(ctx, &pet.RegisterRequest{
38+
Age: req.GetAge(),
39+
Name: req.GetName(),
40+
Kind: req.GetKind().String(),
41+
Breed: req.GetBreed(),
42+
Fur: pet.FurDesc{
43+
Kind: req.GetFur().GetKind().String(),
44+
Color: req.GetFur().GetColor(),
45+
},
46+
})
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
builder := petpb.RegisterPetResponse_builder{
52+
Id: ptr.Ref(registerResp.ID),
53+
}
54+
if len(registerResp.TempName) > 0 {
55+
builder.TempName = ptr.Ref(registerResp.TempName)
56+
}
57+
return builder.Build(), nil
58+
}

0 commit comments

Comments
 (0)