server

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 14 Imported by: 0

README

Server

Server is an inspired google.api.http. Designed to ease implementation and pace up the development. internally it uses protoc which reads protobuf service definitions and generates a reverse-proxy server which translates a RESTful HTTP API into gRPC.

This helps you provide your APIs in both gRPC and RESTful style at the same time.

Kickstart

import (
	"context"
	"log"

	"github.com/go-grpc-http/server"
)

func main() {
	srv, err := server.New()
	if err != nil {
		log.Fatalf("failed to initialize server: %s", err)
	}

	err = srv.Run(context.Background(), HttpPort, GrpcPort)
	if err != nil {
		log.Fatalf("failed to start server: %s", err)
	}
}

Usage

1. Define your protobuf
syntax = "proto3";

option go_package="/protos";

import "google/api/annotations.proto";

// define empty message
message NameGetRequest {}

message NameGetResponse {
    int32 statusCode = 1;
    string status = 2;
    string message = 3;
}

// define empty message
message NameSetRequest {}

message NameSetResponse {
    int32 statusCode = 1;
    string status = 2;
    string message = 3;
}

service Name {
    rpc SetName(NameSetRequest) returns(NameSetResponse) {
        option (google.api.http) = {
            post: "/"
            body: "*"
        };
    }
    rpc GetName(NameGetRequest) returns(NameGetResponse) {
        option (google.api.http) = {
            get: "/"
            body: "*"
        };
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) (*server, error)

New used to initialize new server.

Types

type ErrorHandler

type ErrorHandler func(ctx context.Context, err error) error

type GrpcRegisterer

type GrpcRegisterer interface {
	RegisterGrpc(*grpc.Server) error
}

type HttpRegisterer

type HttpRegisterer interface {
	RegisterHttp(context.Context, *runtime.ServeMux,
		*grpc.ClientConn) error
}

type Option

type Option func(*config) error

func WithDualRegisterer

func WithDualRegisterer(regs ...Registry) Option

WithDualRegisterer used for registering http and gRPC handlers

func WithGrpcPort

func WithGrpcPort(port int) Option

WithGrpcPort used to assign grpc port

func WithGrpcRegisterer

func WithGrpcRegisterer(regs ...GrpcRegisterer) Option

WithGrpcRegisterer used for registering only gRPC handlers

func WithHttpPort

func WithHttpPort(port int) Option

WithHttpPort used to assign http port

func WithName

func WithName(name string) Option

WithName used to name the server

func WithReflection added in v0.0.1

func WithReflection(flag bool) Option

WithReflection used to set ReflectionFlag

func WithRequestTimeout

func WithRequestTimeout(t time.Duration) Option

WithRequestTimeout used to assign request timeout

func WithUnaryServerInterceptor

func WithUnaryServerInterceptor(intrcptrs ...grpc.UnaryServerInterceptor) Option

WithUnaryServerInterceptor used for registering only [UnaryServerInterceptor](https://grpc.io/blog/grpc-web-interceptor/)

type Registry

type Registry interface {
	GrpcRegisterer
	HttpRegisterer
}

Directories

Path Synopsis
protos
Package protos is a reverse proxy.
Package protos is a reverse proxy.
interceptors
ctx
Package protos is a reverse proxy.
Package protos is a reverse proxy.

Jump to

Keyboard shortcuts

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