-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
48 lines (40 loc) · 1.31 KB
/
api.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
package api
import (
"github.com/gin-gonic/gin"
vclient "github.com/vine-io/vine/core/client"
ahandler "github.com/vine-io/vine/lib/api/handler"
"github.com/vine-io/vine/lib/api/handler/openapi"
arpc "github.com/vine-io/vine/lib/api/handler/rpc"
"github.com/vine-io/vine/lib/api/resolver"
"github.com/vine-io/vine/lib/api/resolver/grpc"
"github.com/vine-io/vine/lib/api/router"
regRouter "github.com/vine-io/vine/lib/api/router/registry"
"github.com/vine-io/vine/util/namespace"
)
// PrimpHandler primp *gin.Engine with rpc handler
func PrimpHandler(ns, name string, app *gin.Engine, co vclient.Client, opts ...ahandler.Option) {
Type := "api"
HandlerType := "rpc"
openapi.RegisterOpenAPI(name, co, app)
// create the namespace resolver
nsResolver := namespace.NewResolver(Type, ns)
// resolver options
rops := []resolver.Option{
resolver.WithNamespace(nsResolver.ResolveWithType),
resolver.WithHandler(HandlerType),
}
rr := grpc.NewResolver(rops...)
rt := regRouter.NewRouter(
router.WithHandler(arpc.Handler),
router.WithResolver(rr),
router.WithRegistry(co.Options().Registry),
)
arpcOpts := []ahandler.Option{
ahandler.WithNamespace(ns),
ahandler.WithRouter(rt),
ahandler.WithClient(co),
}
arpcOpts = append(arpcOpts, opts...)
rp := arpc.NewHandler(arpcOpts...)
app.Use(rp.Handle)
}