-
Notifications
You must be signed in to change notification settings - Fork 5
/
gateway.go
49 lines (41 loc) · 1.26 KB
/
gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gateway
import (
"context"
"fmt"
"net/http"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/samverrall/go-task-application/gateway-service/config"
"github.com/samverrall/go-task-application/logger"
"github.com/samverrall/go-task-application/task-application-proto/gen"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type Gateway struct {
logger logger.Logger
config *config.Config
}
// New returns a pointer to a new Gateway
func New(log logger.Logger, c *config.Config) *Gateway {
return &Gateway{
logger: log,
config: c,
}
}
func (g *Gateway) newServeMux() *gwruntime.ServeMux {
return gwruntime.NewServeMux(
gwruntime.WithForwardResponseOption(g.httpResponseModifier),
)
}
func (g *Gateway) Handler(ctx context.Context) (http.Handler, error) {
mux := g.newServeMux()
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
userServiceAddr := buildAddress(g.config.GetString("user-service.host"), g.config.GetInt("user-service.port"))
err := gen.RegisterUserHandlerFromEndpoint(ctx, mux, userServiceAddr, dialOpts)
if err != nil {
return nil, err
}
return mux, nil
}
func buildAddress(host string, port int) string {
return fmt.Sprintf("%s:%d", host, port)
}