kit

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 1 Imported by: 2

README

kit

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Nop

func Nop[REQ any, RES any](context.Context, REQ) (_ RES, _ error)

Nop is an endpoint that does nothing and returns a nil error. Useful for tests.

Types

type Endpoint

type Endpoint[REQ any, RES any] func(ctx context.Context, request REQ) (response RES, err error)

Endpoint is the fundamental building block of servers and clients. It represents a single RPC method.

type Failer

type Failer interface {
	Failed() error
}

Failer may be implemented by Harmoni kit response types that contain business logic error details. If Failed returns a non-nil error, the Go kit transport layer may interpret this as a business logic error, and may encode it differently than a regular, successful response.

It's not necessary for your response types to implement Failer, but it may help for more sophisticated use cases. The addsvc example shows how Failer should be used by a complete application.

type Middleware

type Middleware[REQ any, RES any] func(Endpoint[REQ, RES]) Endpoint[REQ, RES]

Middleware is a chainable behavior modifier for endpoints.

func Chain

func Chain[REQ any, RES any](outer Middleware[REQ, RES], others ...Middleware[REQ, RES]) Middleware[REQ, RES]

Chain is a helper function for composing middlewares. Requests will traverse them in the order they're declared. That is, the first middleware is treated as the outermost middleware.

Example
package main

import (
	"context"
	"fmt"
	"github.com/harmonikit/kit"
)

var (
	ctx = context.Background()
	req = struct{}{}
)

func main() {
	e := kit.Chain[struct{}, struct{}](
		annotate[struct{}, struct{}]("first"),
		annotate[struct{}, struct{}]("second"),
		annotate[struct{}, struct{}]("third"),
	)(myEndpoint)

	if _, err := e(ctx, req); err != nil {
		panic(err)
	}

}

func annotate[REQ any, RES any](s string) kit.Middleware[REQ, RES] {
	return func(next kit.Endpoint[REQ, RES]) kit.Endpoint[REQ, RES] {
		return func(ctx context.Context, request REQ) (RES, error) {
			fmt.Println(s, "pre")
			defer fmt.Println(s, "post")
			return next(ctx, request)
		}
	}
}

func myEndpoint(context.Context, struct{}) (struct{}, error) {
	fmt.Println("my endpoint!")
	return struct{}{}, nil
}
Output:

first pre
second pre
third pre
my endpoint!
third post
second post
first post

Directories

Path Synopsis
auth
Package conn provides utilities related to connections.
Package conn provides utilities related to connections.
sd
Package sd provides utilities related to service discovery.
Package sd provides utilities related to service discovery.
dnssrv
Package dnssrv provides an Instancer implementation for DNS SRV records.
Package dnssrv provides an Instancer implementation for DNS SRV records.
lb
Package lb implements the client-side load balancer pattern.
Package lb implements the client-side load balancer pattern.
http
Package http provides a general purpose HTTP binding for endpoints.
Package http provides a general purpose HTTP binding for endpoints.
http/jsonrpc
Package jsonrpc provides a JSON RPC (v2.0) binding for endpoints.
Package jsonrpc provides a JSON RPC (v2.0) binding for endpoints.

Jump to

Keyboard shortcuts

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