Last active
September 9, 2018 07:09
-
-
Save amsokol/fa4c6046b8bc7d3dcc19329355e60173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"log" | |
"time" | |
"github.com/golang/protobuf/ptypes" | |
"google.golang.org/grpc" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/api/v1" | |
) | |
const ( | |
// apiVersion is version of API is provided by server | |
apiVersion = "v1" | |
) | |
func main() { | |
// get configuration | |
address := flag.String("server", "", "gRPC server in format host:port") | |
flag.Parse() | |
// Set up a connection to the server. | |
conn, err := grpc.Dial(*address, grpc.WithInsecure()) | |
if err != nil { | |
log.Fatalf("did not connect: %v", err) | |
} | |
defer conn.Close() | |
c := v1.NewToDoServiceClient(conn) | |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
defer cancel() | |
t := time.Now().In(time.UTC) | |
reminder, _ := ptypes.TimestampProto(t) | |
pfx := t.Format(time.RFC3339Nano) | |
// Call Create | |
req1 := v1.CreateRequest{ | |
Api: apiVersion, | |
ToDo: &v1.ToDo{ | |
Title: "title (" + pfx + ")", | |
Description: "description (" + pfx + ")", | |
Reminder: reminder, | |
}, | |
} | |
res1, err := c.Create(ctx, &req1) | |
if err != nil { | |
log.Fatalf("Create failed: %v", err) | |
} | |
log.Printf("Create result: <%+v>\n\n", res1) | |
id := res1.Id | |
// Read | |
req2 := v1.ReadRequest{ | |
Api: apiVersion, | |
Id: id, | |
} | |
res2, err := c.Read(ctx, &req2) | |
if err != nil { | |
log.Fatalf("Read failed: %v", err) | |
} | |
log.Printf("Read result: <%+v>\n\n", res2) | |
// Update | |
req3 := v1.UpdateRequest{ | |
Api: apiVersion, | |
ToDo: &v1.ToDo{ | |
Id: res2.ToDo.Id, | |
Title: res2.ToDo.Title, | |
Description: res2.ToDo.Description + " + updated", | |
Reminder: res2.ToDo.Reminder, | |
}, | |
} | |
res3, err := c.Update(ctx, &req3) | |
if err != nil { | |
log.Fatalf("Update failed: %v", err) | |
} | |
log.Printf("Update result: <%+v>\n\n", res3) | |
// Call ReadAll | |
req4 := v1.ReadAllRequest{ | |
Api: apiVersion, | |
} | |
res4, err := c.ReadAll(ctx, &req4) | |
if err != nil { | |
log.Fatalf("ReadAll failed: %v", err) | |
} | |
log.Printf("ReadAll result: <%+v>\n\n", res4) | |
// Delete | |
req5 := v1.DeleteRequest{ | |
Api: apiVersion, | |
Id: id, | |
} | |
res5, err := c.Delete(ctx, &req5) | |
if err != nil { | |
log.Fatalf("Delete failed: %v", err) | |
} | |
log.Printf("Delete result: <%+v>\n\n", res5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment