forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
53 lines (41 loc) · 983 Bytes
/
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
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)
log.Printf("Starting webrpc server on localhost:4242")
return http.ListenAndServe(":4242", r)
}
type ExampleServiceRPC struct {
}
func (s *ExampleServiceRPC) Ping(ctx context.Context) (bool, error) {
return true, nil
}
func (s *ExampleServiceRPC) GetUser(ctx context.Context, req *GetUserRequest) (*User, error) {
if req.UserID == 911 {
return nil, ErrorNotFound("unknown userID %d", 911)
}
return &User{
ID: req.UserID,
Username: "hihi",
}, nil
}