-
Notifications
You must be signed in to change notification settings - Fork 5
/
meowchat.go
56 lines (47 loc) · 1.51 KB
/
meowchat.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
package main
import (
"flag"
"fmt"
"net/http"
"github.com/xh-polaris/meowchat-bff/internal/config"
"github.com/xh-polaris/meowchat-bff/internal/errorx"
"github.com/xh-polaris/meowchat-bff/internal/handler"
"github.com/xh-polaris/meowchat-bff/internal/svc"
"github.com/xh-polaris/meowchat-bff/internal/types"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/rest/httpx"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"google.golang.org/grpc/status"
)
var configFile = flag.String("f", "etc/meowchat.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf, rest.WithCors())
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(b3.New(), propagation.Baggage{}, propagation.TraceContext{}))
httpx.SetErrorHandler(func(err error) (int, interface{}) {
if s, ok := status.FromError(err); ok {
return http.StatusBadRequest, &types.Status{
Code: int(s.Code()),
Msg: s.Message(),
}
}
switch err.(type) {
case *errorx.CodeError:
return http.StatusBadRequest, err
default:
logx.Error(err.Error())
return http.StatusInternalServerError, err.Error()
}
})
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}