From 7baee5b892afcddb72a575282c6c8983d477ed44 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Sat, 17 Dec 2022 16:19:49 -0500 Subject: introduce concept of registry, start of webfinger impl --- config/config.go | 1 + http/server.go | 19 ++++++++++++++----- main.go | 4 +++- registry/registry.go | 42 ++++++++++++++++++++++++++++++++++++++++++ sample-config.toml | 2 ++ webfinger/webfinger.go | 13 +++++++++++++ 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 registry/registry.go create mode 100644 webfinger/webfinger.go diff --git a/config/config.go b/config/config.go index 2d63026..84024d7 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config type Config struct { + Domain string Handlers []Handler } diff --git a/http/server.go b/http/server.go index 25c51d8..6d68d0e 100644 --- a/http/server.go +++ b/http/server.go @@ -4,13 +4,16 @@ import ( "io" "net/http" + "git.capotej.com/capotej/communique/registry" "github.com/gin-gonic/gin" ) -type Server struct{} +type Server struct { + registry *registry.Registry +} -func NewServer() *Server { - return &Server{} +func NewServer(registry *registry.Registry) *Server { + return &Server{registry: registry} } func (s *Server) Start(zapWriter io.Writer) { @@ -20,8 +23,14 @@ func (s *Server) Start(zapWriter io.Writer) { gin.DefaultWriter = zapWriter // send gin logs to zap router.GET("/.well-known/webfinger", func(c *gin.Context) { - // resource := c.Query("resource") - c.JSON(http.StatusOK, nil) + resourceParam := c.Query("resource") + resource := s.registry.LookupResource(resourceParam) + if resource != nil { + c.JSON(http.StatusOK, resource) + } else { + c.JSON(http.StatusNotFound, nil) + } + }) router.Run() diff --git a/main.go b/main.go index 5935bc9..165cacb 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "git.capotej.com/capotej/communique/cgi" "git.capotej.com/capotej/communique/config" "git.capotej.com/capotej/communique/http" + "git.capotej.com/capotej/communique/registry" "github.com/BurntSushi/toml" "go.uber.org/zap" "go.uber.org/zap/zapio" @@ -26,6 +27,7 @@ func main() { log.Fatal(err) } log.Debugf("Loaded TOML Config: %+v", cfg) + registry := registry.NewRegistry(cfg) var mainWg sync.WaitGroup @@ -37,7 +39,7 @@ func main() { // External Http Server writer := &zapio.Writer{Log: logger, Level: zap.DebugLevel} defer writer.Close() - server := http.NewServer() + server := http.NewServer(registry) mainWg.Add(1) go server.Start(writer) diff --git a/registry/registry.go b/registry/registry.go new file mode 100644 index 0000000..04c4565 --- /dev/null +++ b/registry/registry.go @@ -0,0 +1,42 @@ +package registry + +import ( + "fmt" + + "git.capotej.com/capotej/communique/config" + "git.capotej.com/capotej/communique/webfinger" +) + +type Handler struct { + handlerCfg config.Handler +} + +type Registry struct { + cfg config.Config + handlerMap map[string]Handler +} + +func NewRegistry(cfg config.Config) *Registry { + reg := Registry{cfg: cfg} + reg.handlerMap = make(map[string]Handler) + for _, v := range cfg.Handlers { + fqdn := fmt.Sprintf("acct:%s@%s", v.Name, cfg.Domain) + reg.handlerMap[fqdn] = Handler{handlerCfg: v} + } + return ® +} + +func (r *Registry) LookupResource(name string) *webfinger.Resource { + handler, ok := r.handlerMap[name] + if !ok { + return nil + } + rs := webfinger.Resource{ + Subject: fmt.Sprintf("acct:%s@%s", handler.handlerCfg.Name, "activitybub.xyz"), + Aliases: []string{}, + Links: []webfinger.Link{{ + Rel: "asd", + }}, + } + return &rs +} diff --git a/sample-config.toml b/sample-config.toml index 5f18cc0..2d0423f 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -1,3 +1,5 @@ +domain = "activitybub.xyz" + [[handlers]] name = "sample" rpc = "cgi" diff --git a/webfinger/webfinger.go b/webfinger/webfinger.go new file mode 100644 index 0000000..8e43dd2 --- /dev/null +++ b/webfinger/webfinger.go @@ -0,0 +1,13 @@ +package webfinger + +type Link struct { + Rel string `json:"rel"` + Type string `json:"type"` + Href string `json:"href"` +} + +type Resource struct { + Subject string `json:"subject"` + Aliases []string `json:"aliases"` + Links []Link `json:"links"` +} -- cgit v1.2.3