forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (57 loc) · 1.49 KB
/
main.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
//go:generate ../../bin/webrpc-gen -schema=example.ridl -target=go -pkg=main -server -client -out=./example.gen.go
package main
import (
"context"
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func main() {
err := startServer()
if err != nil {
log.Fatal(err)
}
}
func startServer() error {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("."))
})
webrpcHandler := NewExampleServiceServer(&ExampleServiceRPC{})
r.Handle("/*", webrpcHandler)
return http.ListenAndServe(":4242", r)
}
type ExampleServiceRPC struct {
}
func (s *ExampleServiceRPC) Ping(ctx context.Context) error {
return nil
}
func (s *ExampleServiceRPC) Status(ctx context.Context) (bool, error) {
return true, nil
}
func (s *ExampleServiceRPC) Version(ctx context.Context) (*Version, error) {
return &Version{
WebrpcVersion: WebRPCVersion(),
SchemaVersion: WebRPCSchemaVersion(),
SchemaHash: WebRPCSchemaHash(),
}, nil
}
func (s *ExampleServiceRPC) GetUser(ctx context.Context, header map[string]string, userID uint64) (uint32, *User, error) {
if userID == 911 {
return 0, nil, ErrorNotFound("user doest exist")
}
return 200, &User{
ID: userID,
Username: "hihi",
}, nil
}
func (s *ExampleServiceRPC) FindUser(ctx context.Context, f *SearchFilter) (string, *User, error) {
name := f.Q
return f.Q, &User{
ID: 123, Username: name,
}, nil
}