gcpconnect

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: MIT Imports: 16 Imported by: 0

README

Google Cloud Connect package

GoDoc

This package makes it easier to create a Connect server running in GCP environment.

We customize JSON marshaller to include unpopulated fields in the response.

Example:

package main

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

	"buf.build/gen/go/bufbuild/eliza/connectrpc/go/buf/connect/demo/eliza/v1/elizav1connect"
	elizav1 "buf.build/gen/go/bufbuild/eliza/protocolbuffers/go/buf/connect/demo/eliza/v1"
	"connectrpc.com/connect"
	"github.com/mycujoo/go-stdlib/pkg/ctxslog"
	"github.com/mycujoo/go-stdlib/pkg/gcpconnect"
)

func main() {
	ctx := context.Background()
	logger := slog.Default()

	service := &mockService{}

	path, handler := elizav1connect.NewElizaServiceHandler(
		service,
		gcpconnect.GetHandlerOptions(logger)...,
	)

	srv, err := gcpconnect.NewServer(ctx, "localhost:8080", path, handler)
	if err != nil {
		logger.Error("failed to create server", "error", err)
	}

	logger.InfoContext(ctx, "server started",
		"addr", "localhost:8080",
	)

	err = srv.ListenAndServe()
	if err != nil && !errors.Is(err, http.ErrServerClosed) {
		logger.Error("failed to start server", "error", err)
	}
}

type mockService struct{}

func (mockService) Say(ctx context.Context, req *connect.Request[elizav1.SayRequest]) (*connect.Response[elizav1.SayResponse], error) {
	ctxslog.AddArgs(ctx, slog.Int("state", 5), "sentence", req.Msg.GetSentence())
	return connect.NewResponse(&elizav1.SayResponse{}), nil
}

func (mockService) Converse(ctx context.Context, c *connect.BidiStream[elizav1.ConverseRequest, elizav1.ConverseResponse]) error {
	// TODO implement me
	panic("implement me")
}

func (mockService) Introduce(ctx context.Context, req *connect.Request[elizav1.IntroduceRequest], c2 *connect.ServerStream[elizav1.IntroduceResponse]) error {
	ctxslog.AddArgs(ctx, slog.String("name", req.Msg.GetName()))
	panic("implement me")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHandlerOptions

func GetHandlerOptions(logger *slog.Logger, opts ...Option) []connect.HandlerOption

GetHandlerOptions returns the default options for a connect.Handler.

func NewJSONCodec added in v0.0.2

func NewJSONCodec(opts protojson.MarshalOptions) connect.Codec

func NewServer

func NewServer(ctx context.Context, addr string, path string, handler http.Handler) (*http.Server, error)

NewServer creates a new HTTP server. It contains a healthz endpoint and a handler for the given path. Healthz will return 200 OK if the given context is not done.

Types

type Option added in v0.0.2

type Option func(o *options)

func WithJSONMarshalOptions added in v0.0.2

func WithJSONMarshalOptions(opts protojson.MarshalOptions) Option

WithJSONMarshalOptions sets the marshaling options for the JSON codec. Example: `WithJSONMarshalOptions(protojson.MarshalOptions{EmitUnpopulated: true, UseProtoNames: true})`

func WithLogOptions added in v0.0.2

func WithLogOptions(opts ...connectlog.Option) Option

WithLogOptions sets the options for the logging interceptor. E.g. WithLogOptions(connectlog.WithSuccess())

Jump to

Keyboard shortcuts

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