Introduce ServeMux and OpenAPI export endpoint
This commit is contained in:
69
pkg/protocol/openapi/export.go
Normal file
69
pkg/protocol/openapi/export.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package openapi
|
||||
|
||||
import (
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
"net/http"
|
||||
"orly.dev/pkg/app/relay/helpers"
|
||||
"orly.dev/pkg/utils/context"
|
||||
"orly.dev/pkg/utils/log"
|
||||
"orly.dev/pkg/utils/lol"
|
||||
)
|
||||
|
||||
// ExportInput is the parameters for the HTTP API Export method.
|
||||
type ExportInput struct {
|
||||
Auth string `header:"Authorization" doc:"nostr nip-98 (and expiring variant)" required:"true"`
|
||||
}
|
||||
|
||||
// ExportOutput is the return value of Export. It usually will be line structured JSON. In
|
||||
// future there may be more output formats.
|
||||
type ExportOutput struct{ RawBody []byte }
|
||||
|
||||
// RegisterExport implements the Export HTTP API method.
|
||||
func (x *Operations) RegisterExport(api huma.API) {
|
||||
lol.Tracer("RegisterExport")
|
||||
defer func() { lol.Tracer("end RegisterExport") }()
|
||||
name := "Export"
|
||||
description := "Export all events (only works with NIP-98 capable client, will not work with UI)"
|
||||
path := x.path + "/export"
|
||||
scopes := []string{"admin", "read"}
|
||||
method := http.MethodGet
|
||||
huma.Register(
|
||||
api, huma.Operation{
|
||||
OperationID: name,
|
||||
Summary: name,
|
||||
Path: path,
|
||||
Method: method,
|
||||
Tags: []string{"admin"},
|
||||
Description: helpers.GenerateDescription(description, scopes),
|
||||
Security: []map[string][]string{{"auth": scopes}},
|
||||
}, func(ctx context.T, input *ExportInput) (
|
||||
resp *huma.StreamResponse, err error,
|
||||
) {
|
||||
r := ctx.Value("http-request").(*http.Request)
|
||||
remote := helpers.GetRemoteFromReq(r)
|
||||
log.I.F("processing export from %s", remote)
|
||||
authed, pubkey := x.AdminAuth(r, remote)
|
||||
if !authed {
|
||||
err = huma.Error401Unauthorized("Not Authorized")
|
||||
return
|
||||
}
|
||||
log.I.F(
|
||||
"%s export of event data requested on admin port pubkey %0x",
|
||||
remote, pubkey,
|
||||
)
|
||||
sto := x.Storage()
|
||||
resp = &huma.StreamResponse{
|
||||
Body: func(ctx huma.Context) {
|
||||
ctx.SetHeader("Content-Type", "application/nostr+jsonl")
|
||||
sto.Export(x.Context(), ctx.BodyWriter())
|
||||
if f, ok := ctx.BodyWriter().(http.Flusher); ok {
|
||||
f.Flush()
|
||||
} else {
|
||||
log.W.F("error: unable to flush")
|
||||
}
|
||||
},
|
||||
}
|
||||
return
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -7,14 +7,11 @@ import (
|
||||
"github.com/danielgtaylor/huma/v2/adapters/humago"
|
||||
|
||||
"orly.dev/pkg/protocol/servemux"
|
||||
"orly.dev/pkg/utils/lol"
|
||||
)
|
||||
|
||||
// ExposeMiddleware adds the http.Request and http.ResponseWriter to the context
|
||||
// for the Operations handler.
|
||||
func ExposeMiddleware(ctx huma.Context, next func(huma.Context)) {
|
||||
lol.Tracer("ExposeMiddleware")
|
||||
defer func() { lol.Tracer("end ExposeMiddleware") }()
|
||||
// Unwrap the request and response objects.
|
||||
r, w := humago.Unwrap(ctx)
|
||||
ctx = huma.WithValue(ctx, "http-request", r)
|
||||
@@ -27,8 +24,6 @@ func ExposeMiddleware(ctx huma.Context, next func(huma.Context)) {
|
||||
func NewHuma(
|
||||
router *servemux.S, name, version, description string,
|
||||
) (api huma.API) {
|
||||
lol.Tracer("NewHuma", name, version, description)
|
||||
defer func() { lol.Tracer("end NewHuma") }()
|
||||
config := huma.DefaultConfig(name, version)
|
||||
config.Info.Description = description
|
||||
config.DocsPath = ""
|
||||
|
||||
Reference in New Issue
Block a user