package registry import ( "fmt" "git.capotej.com/capotej/communique/config" "git.capotej.com/capotej/communique/resources" ) 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 { // TODO clean up fqn := fmt.Sprintf("acct:%s@%s", v.Name, cfg.Domain) reg.handlerMap[fqn] = Handler{handlerCfg: v} } return ® } func (r *Registry) Profile(name string) (map[string]interface{}, error) { handler := r.findByName(name) if handler == nil { return nil, nil } return resources.RenderProfile(handler.handlerCfg.Name, r.cfg.Domain) } func (r *Registry) WebfingerResource(fqn string) (*resources.WebfingerResource, error) { handler := r.findByFQN(fqn) if handler == nil { handler = r.findByFQN("acct:" + fqn) } if handler == nil { return nil, nil } return resources.RenderWebfingerResource(handler.handlerCfg.Name, r.cfg.Domain) } func (r *Registry) findByFQN(fqn string) *Handler { handler, ok := r.handlerMap[fqn] if !ok { return nil } return &handler } func (r *Registry) findByName(name string) *Handler { fqn := fmt.Sprintf("acct:%s@%s", name, r.cfg.Domain) return r.findByFQN(fqn) }