forked from vmkteam/rpcgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
73 lines (57 loc) · 1.73 KB
/
generator.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
package rpcgen
import (
"fmt"
"net/http"
"github.com/vmkteam/rpcgen/v2/golang"
"github.com/vmkteam/rpcgen/v2/openrpc"
"github.com/vmkteam/rpcgen/v2/php"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/rpcgen/v2/typescript"
smd1 "github.com/vmkteam/zenrpc/smd"
"github.com/vmkteam/zenrpc/v2/smd"
)
type RPCGen struct {
schema smd.Schema
}
type Generator interface {
Generate() ([]byte, error)
}
func (g RPCGen) GoClient(settings golang.Settings) Generator {
return golang.NewClient(g.schema, settings)
}
func (g RPCGen) PHPClient(phpNamespace string) Generator {
return php.NewClient(g.schema, phpNamespace)
}
func (g RPCGen) TSClient(typeMapper typescript.TypeMapper) Generator {
return typescript.NewClient(g.schema, typescript.Settings{TypeMapper: typeMapper})
}
func (g RPCGen) TSCustomClient(settings typescript.Settings) Generator {
return typescript.NewClient(g.schema, settings)
}
func (g RPCGen) SwiftClient(settings swift.Settings) Generator {
return swift.NewClient(g.schema, settings)
}
func (g RPCGen) OpenRPC(title, host string) Generator {
return openrpc.NewClient(g.schema, title, host)
}
// FromSMD create Generator from smd schema
func FromSMD(schema smd.Schema) *RPCGen {
return &RPCGen{schema: schema}
}
// FromSMDv1 create Generator from smd v1 schema
func FromSMDv1(schema smd1.Schema) *RPCGen {
return FromSMD(smdv1ToSMD(schema))
}
// HTTP handlers
// Handler create http handler with fn
func Handler(gen Generator) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
bts, err := gen.Generate()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("error: %s", err)))
return
}
_, _ = w.Write(bts)
}
}