serverus

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2021 License: MIT Imports: 6 Imported by: 0

README

Serverus

Serverus is a experimental gRPC server that should help small teams to up and running a very basic stream server. The idea behind this is be able to play aroung gRPC easly and have a server up and running fast.

No recommended to use in production environment since is an experiment.

Install

go get github.com/asccigcc/serverus
import "github.com/asccigcc/serverus"

Usage

Serverus provides methods that helps the developer to start easly a gRCP application. But it is important to understand few constraints before to start running the service.

The developer should know about the following topics:

  • Go lang basic.
  • gRPC basics you can found more information here.
Preparing Protobuf

Before to integrate Serverus is very important to have defined your protobuf files since those will be used for your gRPC server. You can find a really good guide in the here.

We are going to use the same quickstart protobuf as example for this documentation.

In the example from gRPC documentation you will have a protobuf with the following content:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

After you copiled the proto file you should have two files:

  • helloworld.pb.go
  • helloworld_grpc.pb.go

Those files should be imported into the main.go from your project (we suggest to keep those files inside a directory like hello/).

Preparing integration

In your main application; after to import the package and protobuf, you can create a server from serverus. The following guide should help you to have an idea about how should works.

Import your protobuf files:

import proto "github.com/my_user/application/hello/"

Define server struct with the UnimplementedGretterServer this configure gRPC to what to do when an endpoint does not exist.

type server struct {
	proto.UnimplementedGretterServer
}

Create RegisterHandlerServer this function will be used to inject into the server function so Serverus will recognize the proto.

func RegisterHandlerServer(s grpc.ServiceRegistrar) {
	log.Println("Calling RegisterHandlerServer")
	opa.RegisterGretterServer(s, &server{})
}

Create catcher method for SayHello request.

func (*server) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloReply, error) {

	log.Println(req)

	return &proto.HelloReply{Message: "World"}, nil
}

In your main function start a Serverus service.

func main() {
	server := serverus.NewServerus(":3000")
	server.InitGRPC()
	server.RegisterServer(RegisterHandlerServer)
	server.StartServerus()
}

License

MIT License Copyright (c) 2021 Pastorinni Ochoa

Contribution

PR(s) are Open and Welcomed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Serverus

type Serverus struct {
	// contains filtered or unexported fields
}

func NewServerus

func NewServerus(port string) *Serverus

Initialize new gRPC serverus server

func (*Serverus) ChainInterceptors

func (s *Serverus) ChainInterceptors(inter interface{})

Chain add interceptor to the interceptors array so we can add more and pass into the grpc server when this is instantiated.

func (*Serverus) InitGRPC

func (s *Serverus) InitGRPC()

Initialize gRPC server passing the interceptors

func (*Serverus) RegisterServer

func (s *Serverus) RegisterServer(fn registerServer)

Register the Protobuf server handler

func (*Serverus) StartServerus

func (s *Serverus) StartServerus()

Start the gRPC server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL