api.go RawHistoryFolder
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3// Package rpc serves the ermite API contract (bnema.dev/ermite-api) with
4// ConnectRPC: handlers that map the messages of the contract to the use cases
5// of app and back, the interceptor that authenticates callers, and the error
6// mapping. The same handlers serve remote clients over HTTP and the web in
7// process (see InProcessClient).
8package rpc
9
10import (
11 "context"
12 "net/http"
13 "net/netip"
14
15 "connectrpc.com/connect"
16 "github.com/bnema/zerowrap"
17
18 "bnema.dev/ermite-api/gen/ermite/ermiteconnect"
19 "bnema.dev/ermite/internal/app"
20 "bnema.dev/ermite/internal/clientlimit"
21 "bnema.dev/ermite/internal/domain"
22)
23
24// MaxRequestBytes bounds the size of a request message. Responses are not
25// bounded: a diff has its own limits and blobs are streamed.
26const MaxRequestBytes = 1 << 20
27
28// Features are the optional capabilities of the contract that this server
29// implements, as GetVersion lists them.
30var Features = []string{"repo.update", "events.resume"}
31
32// Authenticator turns a token secret into a principal, or
33// domain.ErrUnauthenticated. app.Auth fits.
34type Authenticator interface {
35 Authenticate(ctx context.Context, secret string) (domain.Principal, error)
36}
37
38// Config configures the API.
39type Config struct {
40 Auth Authenticator
41 Repos *app.Repos
42 Tokens *app.Tokens
43 Browse *app.Browse
44 Events *app.Events
45 Version string
46 // TrustedProxies are the networks allowed to set X-Forwarded-For.
47 TrustedProxies []netip.Prefix
48 // Limiter counts failed authentications; nil builds a default one. Share
49 // it with the other adapters that authenticate, so that a client has one
50 // budget of failures.
51 Limiter *clientlimit.Limiter
52}
53
54// API is the set of Connect handlers of the contract.
55type API struct {
56 handler http.Handler
57}
58
59// New builds the API.
60func New(cfg Config) *API {
61 if cfg.Limiter == nil {
62 cfg.Limiter = clientlimit.NewLimiter(nil)
63 }
64 opts := []connect.HandlerOption{
65 connect.WithInterceptors(newInterceptor(cfg)),
66 connect.WithReadMaxBytes(MaxRequestBytes),
67 connect.WithRecover(func(ctx context.Context, spec connect.Spec, _ http.Header, panicValue any) error {
68 log := zerowrap.FromCtx(ctx)
69 log.Error().Interface("panic", panicValue).Str("procedure", spec.Procedure).Msg("api handler panicked")
70 return connect.NewError(connect.CodeInternal, errInternal)
71 }),
72 }
73 mux := http.NewServeMux()
74 mux.Handle(ermiteconnect.NewRepoServiceHandler(&repoService{cfg.Repos}, opts...))
75 mux.Handle(ermiteconnect.NewGitServiceHandler(&gitService{cfg.Browse}, opts...))
76 mux.Handle(ermiteconnect.NewTokenServiceHandler(&tokenService{cfg.Tokens}, opts...))
77 mux.Handle(ermiteconnect.NewEventServiceHandler(&eventService{cfg.Events}, opts...))
78 mux.Handle(ermiteconnect.NewServerServiceHandler(&serverService{cfg.Version}, opts...))
79 return &API{handler: mux}
80}
81
82// Handler returns the handler of all services. Its paths are the procedure
83// paths of the contract ("/ermite.RepoService/ListRepos"); httpserver mounts
84// it under /-/api/ and strips the prefix.
85func (a *API) Handler() http.Handler { return a.handler }
86
87// caller returns the principal the interceptor (or the in-process caller)
88// put in ctx; anonymous when there is none.
89func caller(ctx context.Context) domain.Principal {
90 p, _ := domain.PrincipalFromContext(ctx)
91 return p
92}