Hello World

This commit is contained in:
Sammy Shear 2024-07-18 15:44:51 -04:00
parent dcfdaf17a6
commit 2c2149fe1b
Signed by: sammyshear
GPG Key ID: 54DBDB0E658FB9F7
8 changed files with 1393 additions and 1 deletions

View File

@ -1,2 +1,4 @@
# rpc-go-example # RPC Go Example using Twirp
Example RPC server using Twirp

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module git.sshear.dev/sammyshear/rpc-go-example
go 1.22.5
require (
github.com/twitchtv/twirp v8.1.3+incompatible // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/twitchtv/twirp v8.1.3+incompatible h1:+F4TdErPgSUbMZMwp13Q/KgDVuI7HJXP61mNV3/7iuU=
github.com/twitchtv/twirp v8.1.3+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=

36
main.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
pb "git.sshear.dev/sammyshear/rpc-go-example/rpc/helloworld"
)
type HelloWorldServer struct{}
// Hello implements helloworld.HelloWorld.
func (h *HelloWorldServer) Hello(_ context.Context, req *pb.HelloReq) (*pb.HelloResp, error) {
return &pb.HelloResp{Text: fmt.Sprintf("Hello %s!", req.Subject)}, nil
}
func main() {
twirpHandler := pb.NewHelloWorldServer(&HelloWorldServer{})
mux := http.NewServeMux()
mux.Handle(twirpHandler.PathPrefix(), twirpHandler)
go http.ListenAndServe(":8080", mux)
client := pb.NewHelloWorldProtobufClient("http://localhost:8080", &http.Client{})
res, err := client.Hello(context.Background(), &pb.HelloReq{
Subject: "Sammy",
})
if err != nil {
log.Panicf("Error making request: %s", err)
}
fmt.Printf("%s\n", res.Text)
os.Exit(0)
}

15
proto/HelloWorld.proto Normal file
View File

@ -0,0 +1,15 @@
syntax = "proto3";
package git.sshear.dev.rpcgoexample.helloworld;
option go_package = "rpc/helloworld";
service HelloWorld {
rpc Hello(HelloReq) returns (HelloResp);
}
message HelloReq {
string subject = 1;
}
message HelloResp {
string text = 1;
}

View File

@ -0,0 +1,216 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.34.2
// protoc v5.27.2
// source: proto/HelloWorld.proto
package helloworld
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type HelloReq struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Subject string `protobuf:"bytes,1,opt,name=subject,proto3" json:"subject,omitempty"`
}
func (x *HelloReq) Reset() {
*x = HelloReq{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_HelloWorld_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *HelloReq) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*HelloReq) ProtoMessage() {}
func (x *HelloReq) ProtoReflect() protoreflect.Message {
mi := &file_proto_HelloWorld_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use HelloReq.ProtoReflect.Descriptor instead.
func (*HelloReq) Descriptor() ([]byte, []int) {
return file_proto_HelloWorld_proto_rawDescGZIP(), []int{0}
}
func (x *HelloReq) GetSubject() string {
if x != nil {
return x.Subject
}
return ""
}
type HelloResp struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Text string `protobuf:"bytes,1,opt,name=text,proto3" json:"text,omitempty"`
}
func (x *HelloResp) Reset() {
*x = HelloResp{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_HelloWorld_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *HelloResp) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*HelloResp) ProtoMessage() {}
func (x *HelloResp) ProtoReflect() protoreflect.Message {
mi := &file_proto_HelloWorld_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use HelloResp.ProtoReflect.Descriptor instead.
func (*HelloResp) Descriptor() ([]byte, []int) {
return file_proto_HelloWorld_proto_rawDescGZIP(), []int{1}
}
func (x *HelloResp) GetText() string {
if x != nil {
return x.Text
}
return ""
}
var File_proto_HelloWorld_proto protoreflect.FileDescriptor
var file_proto_HelloWorld_proto_rawDesc = []byte{
0x0a, 0x16, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x57, 0x6f, 0x72,
0x6c, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x26, 0x67, 0x69, 0x74, 0x2e, 0x73, 0x73,
0x68, 0x65, 0x61, 0x72, 0x2e, 0x64, 0x65, 0x76, 0x2e, 0x72, 0x70, 0x63, 0x67, 0x6f, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64,
0x22, 0x24, 0x0a, 0x08, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, 0x07,
0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73,
0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x22, 0x1f, 0x0a, 0x09, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52,
0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x32, 0x7a, 0x0a, 0x0a, 0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x12, 0x6c, 0x0a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x30,
0x2e, 0x67, 0x69, 0x74, 0x2e, 0x73, 0x73, 0x68, 0x65, 0x61, 0x72, 0x2e, 0x64, 0x65, 0x76, 0x2e,
0x72, 0x70, 0x63, 0x67, 0x6f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x68, 0x65, 0x6c,
0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71,
0x1a, 0x31, 0x2e, 0x67, 0x69, 0x74, 0x2e, 0x73, 0x73, 0x68, 0x65, 0x61, 0x72, 0x2e, 0x64, 0x65,
0x76, 0x2e, 0x72, 0x70, 0x63, 0x67, 0x6f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x68,
0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52,
0x65, 0x73, 0x70, 0x42, 0x10, 0x5a, 0x0e, 0x72, 0x70, 0x63, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f,
0x77, 0x6f, 0x72, 0x6c, 0x64, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_HelloWorld_proto_rawDescOnce sync.Once
file_proto_HelloWorld_proto_rawDescData = file_proto_HelloWorld_proto_rawDesc
)
func file_proto_HelloWorld_proto_rawDescGZIP() []byte {
file_proto_HelloWorld_proto_rawDescOnce.Do(func() {
file_proto_HelloWorld_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_HelloWorld_proto_rawDescData)
})
return file_proto_HelloWorld_proto_rawDescData
}
var file_proto_HelloWorld_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_proto_HelloWorld_proto_goTypes = []any{
(*HelloReq)(nil), // 0: git.sshear.dev.rpcgoexample.helloworld.HelloReq
(*HelloResp)(nil), // 1: git.sshear.dev.rpcgoexample.helloworld.HelloResp
}
var file_proto_HelloWorld_proto_depIdxs = []int32{
0, // 0: git.sshear.dev.rpcgoexample.helloworld.HelloWorld.Hello:input_type -> git.sshear.dev.rpcgoexample.helloworld.HelloReq
1, // 1: git.sshear.dev.rpcgoexample.helloworld.HelloWorld.Hello:output_type -> git.sshear.dev.rpcgoexample.helloworld.HelloResp
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_proto_HelloWorld_proto_init() }
func file_proto_HelloWorld_proto_init() {
if File_proto_HelloWorld_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_HelloWorld_proto_msgTypes[0].Exporter = func(v any, i int) any {
switch v := v.(*HelloReq); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_HelloWorld_proto_msgTypes[1].Exporter = func(v any, i int) any {
switch v := v.(*HelloResp); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_HelloWorld_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_HelloWorld_proto_goTypes,
DependencyIndexes: file_proto_HelloWorld_proto_depIdxs,
MessageInfos: file_proto_HelloWorld_proto_msgTypes,
}.Build()
File_proto_HelloWorld_proto = out.File
file_proto_HelloWorld_proto_rawDesc = nil
file_proto_HelloWorld_proto_goTypes = nil
file_proto_HelloWorld_proto_depIdxs = nil
}

File diff suppressed because it is too large Load Diff

9
tools.go Normal file
View File

@ -0,0 +1,9 @@
//go:build tools
// +build tools
package tools
import (
_ "github.com/twitchtv/twirp/protoc-gen-twirp"
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
)