server

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterHandler

func RegisterHandler[ReqT, RespT any](
	router *Server,
	method ctypes.Method,
	innerHandler func(context.Context, ctypes.ID, ReqT) (*RespT, error),
	errorMapping map[error]ctypes.ErrorCode,
)

RegisterHandler will register new handler for this method in this router. If you want to send concrete lrpc code on concrete errors - you can provide mapping "error 2 lrpc error code".

func RegisterHandlerNoResponse

func RegisterHandlerNoResponse[ReqT any](
	router *Server,
	method ctypes.Method,
	innerHandler func(context.Context, ctypes.ID, ReqT) error,
	errorMapping map[error]ctypes.ErrorCode,
)

RegisterHandlerNoResponse registers a method with empty response body.

Types

type HandlerSchema

type HandlerSchema struct {
	Method   string `json:"method"`
	Request  Struct `json:"request"`
	Response Struct `json:"response"`
}

HandlerSchema describes one registered method.

type HandlerSpec

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

HandlerSpec stores handler internals and schema examples.

type OptOptionsSetter

type OptOptionsSetter func(o *Options)

func WithLogger

func WithLogger(opt *slog.Logger) OptOptionsSetter

func WithName

func WithName(opt string) OptOptionsSetter

func WithNs

func WithNs(opt string) OptOptionsSetter

func WithPropagator

func WithPropagator(opt Propagator) OptOptionsSetter

func WithRegisterer added in v0.1.3

func WithRegisterer(opt prometheus.Registerer) OptOptionsSetter

func WithTracerProvider

func WithTracerProvider(opt TracerProvider) OptOptionsSetter

type Options

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

Options configures the server.

func NewOptions

func NewOptions(
	options ...OptOptionsSetter,
) Options

func (*Options) Validate

func (o *Options) Validate() error

type Propagator

type Propagator = interface {
	Extract(ctx context.Context, headers http.Header) context.Context
}

Propagator extracts trace headers from incoming requests.

type Server

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

Server handles RPC requests.

func New

func New(opts Options) (*Server, error)

New creates a new server.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"time"

	"github.com/kazhuravlev/just"
	"github.com/kazhuravlev/lrpc/ctypes"
	"github.com/kazhuravlev/lrpc/server"
)

func main() {
	router := just.Must(server.New(server.NewOptions(server.WithName("example_server"))))

	{
		type AuthReq struct {
			Username string `json:"username"`
			Password string `json:"password"`
		}

		type AuthResp struct {
			Token string `json:"token"`
		}

		errUnableToAuth := errors.New("unable to auth")
		authHandler := func(ctx context.Context, id ctypes.ID, req AuthReq) (*AuthResp, error) {
			if req.Username == "root" && req.Password == "admin" {
				return &AuthResp{Token: "some-token"}, nil
			}

			return nil, fmt.Errorf("bad credentials: %w", errUnableToAuth)
		}

		server.RegisterHandler(
			router,
			"auth-v1",
			authHandler,
			map[error]ctypes.ErrorCode{
				errUnableToAuth: 334455,
			},
		)
	}

	{
		type LogoutReq struct {
			Token string `json:"token"`
		}

		logoutHandler := func(ctx context.Context, id ctypes.ID, req LogoutReq) error {
			if req.Token == "some-token" {
				return nil
			}

			return errors.New("unable to logout")
		}

		server.RegisterHandlerNoResponse(router, "logout-v1", logoutHandler, nil)
	}

	mux := http.NewServeMux()
	mux.Handle("/api/v1/lrpc/{method}", router.HTTPHandler())

	srv := &http.Server{
		Addr:    ":8000",
		Handler: mux,
	}
	go func() {
		time.Sleep(time.Second)
		_ = srv.Shutdown(context.Background()) //nolint:errcheck
	}()
	_ = srv.ListenAndServe() //nolint:errcheck

}

func (*Server) HTTPHandler

func (r *Server) HTTPHandler() http.Handler

HTTPHandler returns an HTTP handler for lrpc requests.

func (*Server) HTTPHandlerSchema

func (r *Server) HTTPHandlerSchema() http.Handler

HTTPHandlerSchema returns a handler with methods schema.

type Span

type Span = internalobs.Span

Span represents an active trace span.

type Struct

type Struct struct {
	Example json.RawMessage `json:"example"`
}

Struct keeps one schema example.

type Tracer

type Tracer = internalobs.Tracer

Tracer starts spans.

type TracerProvider

type TracerProvider = internalobs.TracerProvider

TracerProvider creates tracers.

Jump to

Keyboard shortcuts

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