Documentation
¶
Index ¶
- func RegisterHandler[ReqT, RespT any](router *Server, method ctypes.Method, ...)
- func RegisterHandlerNoResponse[ReqT any](router *Server, method ctypes.Method, ...)
- type HandlerSchema
- type HandlerSpec
- type OptOptionsSetter
- func WithLogger(opt *slog.Logger) OptOptionsSetter
- func WithName(opt string) OptOptionsSetter
- func WithNs(opt string) OptOptionsSetter
- func WithPropagator(opt Propagator) OptOptionsSetter
- func WithRegisterer(opt prometheus.Registerer) OptOptionsSetter
- func WithTracerProvider(opt TracerProvider) OptOptionsSetter
- type Options
- type Propagator
- type Server
- type Span
- type Struct
- type Tracer
- type TracerProvider
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".
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
type Propagator ¶
Propagator extracts trace headers from incoming requests.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server handles RPC requests.
func New ¶
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
}
Output:
func (*Server) HTTPHandler ¶
HTTPHandler returns an HTTP handler for lrpc requests.
func (*Server) HTTPHandlerSchema ¶
HTTPHandlerSchema returns a handler with methods schema.
type Struct ¶
type Struct struct {
Example json.RawMessage `json:"example"`
}
Struct keeps one schema example.
type TracerProvider ¶
type TracerProvider = internalobs.TracerProvider
TracerProvider creates tracers.
Click to show internal directories.
Click to hide internal directories.