Files
realy/openapi/http-import.go
mleku bbebbe2b02 Add tracing with lol.Tracer in multiple functions.
Introduced `lol.Tracer` for function entry/exit logging across various packages. This improves traceability and debugging of function executions while preserving existing behavior. Removed unused files `doc.go` and `nothing.go` to clean up the repository.
2025-06-29 07:32:24 +01:00

66 lines
1.9 KiB
Go

package openapi
import (
"fmt"
"io"
"net/http"
"time"
"github.com/danielgtaylor/huma/v2"
"realy.lol/context"
"realy.lol/log"
"realy.lol/lol"
"realy.lol/realy/helpers"
)
// ImportInput is the parameters of an import operation, authentication and the stream of line
// structured JSON events.
type ImportInput struct {
Auth string `header:"Authorization" doc:"nostr nip-98 token for authentication" required:"true"`
}
// ImportOutput is nothing, basically, a 204 or 200 status is expected.
type ImportOutput struct{}
// RegisterImport is the implementation of the Import operation.
func (x *Operations) RegisterImport(api huma.API) {
lol.Tracer("RegisterImport")
defer func() { lol.Tracer("end RegisterImport") }()
name := "Import"
description := "Import events from line structured JSON (jsonl)"
path := x.path + "/import"
scopes := []string{"admin", "write"}
method := http.MethodPost
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}},
DefaultStatus: 204,
}, func(ctx context.T, input *ImportInput) (output *ImportOutput, err error) {
lol.Tracer("Import", input)
defer func() { lol.Tracer("end Import", output, err) }()
r := ctx.Value("http-request").(*http.Request)
remote := helpers.GetRemoteFromReq(r)
authed, pubkey := x.AdminAuth(r, remote, 10*time.Minute)
if !authed {
// pubkey = ev.Pubkey
err = huma.Error401Unauthorized(
fmt.Sprintf("user %0x not authorized for action", pubkey))
return
}
sto := x.Storage()
log.I.F("import of event data requested on admin port from %s pubkey %0x", remote,
pubkey)
read := io.LimitReader(r.Body, r.ContentLength)
<-sto.Import(read)
x.Server.ZeroLists()
x.Server.CheckOwnerLists(context.Bg())
return
})
}