/
server.go
106 lines (83 loc) · 2.69 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package v1
import (
"context"
"fmt"
"net/http"
"github.com/segmentio/encoding/json"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.opentelemetry.io/otel/trace"
"golang.org/x/text/message"
"google.golang.org/grpc/status"
"github.com/batazor/shortlink/internal/pkg/logger"
link_cqrs "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/cqrs/link/v1"
link_rpc "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/link/v1"
sitemap_rpc "github.com/batazor/shortlink/internal/services/link/infrastructure/rpc/sitemap/v1"
"github.com/batazor/shortlink/pkg/http/server"
"github.com/batazor/shortlink/pkg/rpc"
)
// API ...
type API struct {
LinkServiceServer
http *http.Server
RPC *rpc.RPCServer
}
// Run HTTP-server
func (api *API) Run(
ctx context.Context,
i18n *message.Printer,
config http_server.Config,
log logger.Logger,
tracer *trace.TracerProvider,
// Delivery
link_rpc link_rpc.LinkServiceClient,
link_command link_cqrs.LinkCommandServiceClient,
link_query link_cqrs.LinkQueryServiceClient,
sitemap_rpc sitemap_rpc.SitemapServiceClient,
) error {
// Rug gRPC
RegisterLinkServiceServer(api.RPC.Server, api)
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux(
// Register custom error handler
runtime.WithErrorHandler(api.CustomHTTPError),
)
// DefaultContextTimeout is used for gRPC call context.WithTimeout whenever a Grpc-Timeout inbound
// header isn't present. If the value is 0 the sent `context` will not have a timeout.
runtime.DefaultContextTimeout = config.Timeout
err := RegisterLinkServiceHandlerServer(ctx, mux, api)
if err != nil {
return err
}
api.http = http_server.New(ctx, mux, config)
// Start HTTP server (and proxy calls to gRPC server endpoint)
log.Info(fmt.Sprintf("API run on port %d", config.Port))
err = api.http.ListenAndServe()
if err != nil {
return err
}
return nil
}
// Close ...
func (api *API) Close() error {
if err := api.http.Close(); err != nil {
return err
}
return nil
}
func (api *API) CustomHTTPError(_ context.Context, _ *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
const fallback = `{"error": "failed to marshal error message"}`
s := status.Convert(err)
pb := s.Proto()
w.Header().Del("Trailer")
w.Header().Del("Transfer-Encoding")
contentType := marshaler.ContentType(pb)
w.Header().Set("Content-Type", contentType)
w.WriteHeader(runtime.HTTPStatusFromCode(status.Code(err)))
jErr := json.NewEncoder(w).Encode(customError{
Error: status.Convert(err).Message(),
})
if jErr != nil {
_, _ = w.Write([]byte(fallback)) // #nosec
}
}