Skip to content

Commit

Permalink
Improve the basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Nov 14, 2022
1 parent 4625647 commit 3ff1e96
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions _examples/golang-basics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main

import (
"context"
"errors"
"log"
"net/http"

Expand Down Expand Up @@ -31,34 +30,32 @@ func startServer() error {
webrpcHandler := NewExampleServiceServer(&ExampleServiceRPC{})
r.Handle("/*", webrpcHandler)

log.Printf("Listening on :4242")
return http.ListenAndServe(":4242", r)
}

type ExampleServiceRPC struct {
}

func (s *ExampleServiceRPC) Ping(ctx context.Context) error {
func (rpc *ExampleServiceRPC) Ping(ctx context.Context) error {
return nil
}

func (s *ExampleServiceRPC) Status(ctx context.Context) (bool, error) {
func (rpc *ExampleServiceRPC) Status(ctx context.Context) (bool, error) {
return true, nil
}

func (s *ExampleServiceRPC) Version(ctx context.Context) (*Version, error) {
func (rpc *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) {
func (rpc *ExampleServiceRPC) GetUser(ctx context.Context, header map[string]string, userID uint64) (uint32, *User, error) {
if userID == 911 {
return 0, nil, WrapError(ErrInternal, errors.New("bad"), "app msg here")
// return 0, nil, ErrorNotFound("unknown userID %d", 911)
// return 0, nil, Errorf(ErrNotFound, "unknown userID %d", 911)
// return 0, nil, WrapError(ErrNotFound, nil, "unknown userID %d", 911)
return 0, nil, Errorf(ErrNotFound, "unknown userID %d", 911)
}

return 200, &User{
Expand All @@ -67,9 +64,13 @@ func (s *ExampleServiceRPC) GetUser(ctx context.Context, header map[string]strin
}, nil
}

func (s *ExampleServiceRPC) FindUser(ctx context.Context, f *SearchFilter) (string, *User, error) {
name := f.Q
return f.Q, &User{
ID: 123, Username: name,
func (rpc *ExampleServiceRPC) FindUser(ctx context.Context, s *SearchFilter) (string, *User, error) {
if s == nil {
return "", nil, Errorf(ErrInvalidArgument, "s search filter required")
}
name := s.Q
return s.Q, &User{
ID: 123,
Username: name,
}, nil
}

0 comments on commit 3ff1e96

Please sign in to comment.