From c4943a96b1ae3995c4c83002f68380f60cf389b5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 4 Oct 2022 12:53:42 +0200 Subject: [PATCH] Expose registered process tags via API --- process/api.go | 39 +++++++++++++++++++++++++++++++++++++++ process/module.go | 4 ++++ 2 files changed, 43 insertions(+) create mode 100644 process/api.go diff --git a/process/api.go b/process/api.go new file mode 100644 index 00000000..2e37502d --- /dev/null +++ b/process/api.go @@ -0,0 +1,39 @@ +package process + +import ( + "github.com/safing/portbase/api" +) + +func registerAPIEndpoints() error { + if err := api.RegisterEndpoint(api.Endpoint{ + Path: "process/tags", + Read: api.PermitUser, + BelongsTo: module, + StructFunc: handleProcessTagMetadata, + Name: "Get Process Tag Metadata", + Description: "Get information about process tags.", + }); err != nil { + return err + } + + return nil +} + +func handleProcessTagMetadata(ar *api.Request) (i interface{}, err error) { + tagRegistryLock.Lock() + defer tagRegistryLock.Unlock() + + // Create response struct. + resp := struct { + Tags []TagDescription + }{ + Tags: make([]TagDescription, 0, len(tagRegistry)*2), + } + + // Get all tag descriptions. + for _, th := range tagRegistry { + resp.Tags = append(resp.Tags, th.TagDescriptions()...) + } + + return resp, nil +} diff --git a/process/module.go b/process/module.go index a530bd21..b33be8ca 100644 --- a/process/module.go +++ b/process/module.go @@ -26,5 +26,9 @@ func start() error { updatesPath += string(os.PathSeparator) } + if err := registerAPIEndpoints(); err != nil { + return err + } + return nil }