Add version management and cleanup subscription handling
Introduce version management using a new `version` package to embed and expose application version information. Refactor subscription handling in `publisher-openapi.go` for clarity by renaming variables and improving readability.
This commit is contained in:
34
relay/helpers/helpers.go
Normal file
34
relay/helpers/helpers.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GenerateDescription(text string, scopes []string) string {
|
||||
if len(scopes) == 0 {
|
||||
return text
|
||||
}
|
||||
result := make([]string, 0)
|
||||
for _, value := range scopes {
|
||||
result = append(result, "`"+value+"`")
|
||||
}
|
||||
return text + "<br/><br/>**Scopes**<br/>" + strings.Join(result, ", ")
|
||||
}
|
||||
|
||||
func GetRemoteFromReq(r *http.Request) (rr string) {
|
||||
// reverse proxy should populate this field so we see the remote not the proxy
|
||||
rem := r.Header.Get("X-Forwarded-For")
|
||||
if rem == "" {
|
||||
rr = r.RemoteAddr
|
||||
} else {
|
||||
splitted := strings.Split(rem, " ")
|
||||
if len(splitted) == 1 {
|
||||
rr = splitted[0]
|
||||
}
|
||||
if len(splitted) == 2 {
|
||||
rr = splitted[1]
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -54,19 +54,19 @@ func (p *S) Receive(msg publisher.Message) {
|
||||
|
||||
func (p *S) Deliver(authRequired, publicReadable bool, ev *event.E) {
|
||||
p.Mx.Lock()
|
||||
var subs []*H
|
||||
var toDelete []*H
|
||||
for sub := range p.Map {
|
||||
// check if the subscription's subscriber is still alive
|
||||
select {
|
||||
case <-sub.Ctx.Done():
|
||||
subs = append(subs, sub)
|
||||
toDelete = append(toDelete, sub)
|
||||
default:
|
||||
}
|
||||
}
|
||||
for _, sub := range subs {
|
||||
for _, sub := range toDelete {
|
||||
delete(p.Map, sub)
|
||||
}
|
||||
subs = subs[:0]
|
||||
toDelete = toDelete[:0]
|
||||
for sub := range p.Map {
|
||||
// if auth required, check the subscription pubkey matches
|
||||
if !publicReadable {
|
||||
|
||||
@@ -51,7 +51,7 @@ func NewHuma(router *servemux.S, name, version, description string) (api huma.AP
|
||||
w.Write([]byte(`<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>realy HTTP API UI</title>
|
||||
<title>manifold HTTP API UI</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
|
||||
40
relay/version.go
Normal file
40
relay/version.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
"manifold.mleku.dev/version"
|
||||
"net/http"
|
||||
"realy.lol/context"
|
||||
"realy.lol/realy/helpers"
|
||||
)
|
||||
|
||||
type VersionOutput struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// RegisterVersion just returns the version information of the relay.
|
||||
func (x *Operations) RegisterVersion(api huma.API) {
|
||||
name := "ConfigurationSet"
|
||||
description := "Set the configuration"
|
||||
path := "/configuration/set"
|
||||
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}},
|
||||
}, func(ctx context.T, input *struct{}) (output *VersionOutput, err error) {
|
||||
output = &VersionOutput{
|
||||
Name: version.Name,
|
||||
Version: version.V,
|
||||
Description: version.Description,
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
1
version/version
Normal file
1
version/version
Normal file
@@ -0,0 +1 @@
|
||||
v0.0.1
|
||||
10
version/version.go
Normal file
10
version/version.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package version
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed version
|
||||
var V string
|
||||
|
||||
var Name = "manifold"
|
||||
|
||||
var Description = "Reference implementation of the Manifold protocol"
|
||||
Reference in New Issue
Block a user