fix: avatar, add logging (#109)

Co-authored-by: xuu <me@sour.is>
Reviewed-on: #109
pull/102/head^2
xuu 6 months ago
parent 3373a58908
commit b11d14a274
  1. 2
      internal/api.go
  2. 1
      internal/handlers.go
  3. 3
      internal/router.go
  4. 2
      internal/server.go
  5. BIN
      internal/web/app.wasm
  6. 19
      lookup.go
  7. 2
      resolv.go

@ -70,7 +70,7 @@ func (a *API) SendEndpoint() httprouter.Handle {
// TODO: Queue up an internal retry and return immediately on failure?
if err := saltyim.Send(req.Endpoint, req.Message, req.Capabilities); err != nil {
log.WithError(err).Errorf("error sending message to %s: %w", req.Endpoint, err)
log.WithError(err).Errorf("error sending message to %s: %s", req.Endpoint, err)
http.Error(w, "Send Error", http.StatusInternalServerError)
return
}

@ -24,6 +24,7 @@ func (s *Server) ConfigHandler() httprouter.Handle {
configDir := filepath.Join(s.config.Data, wellknownPath)
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
w.Header().Set("X-Salty-Accept-Encoding", "br, gzip, deflate")
http.ServeFile(w, r, filepath.Join(configDir, p.ByName("config")))
}
}

@ -30,6 +30,9 @@ func NewRouter() *Router {
GlobalOPTIONS: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
w.Header().Set("X-Salty-Accept-Encoding", "br, gzip, deflate")
}),
},
}

@ -372,6 +372,8 @@ func NewServer(bind string, options ...Option) (*Server, error) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
next(w, r, p)
}
}))

BIN
internal/web/app.wasm (Stored with Git LFS)

Binary file not shown.

@ -35,7 +35,7 @@ func fetchConfig(addr string) (Config, Capabilities, error) {
if err := json.NewDecoder(res.Body).Decode(&config); err != nil {
return Config{}, Capabilities{}, err
}
log.Debug(res.Header)
cap := Capabilities{
AcceptEncoding: res.Header.Get("Accept-Encoding"),
}
@ -54,6 +54,10 @@ type Capabilities struct {
AcceptEncoding string
}
func (c Capabilities) String() string {
return fmt.Sprint("accept-encoding: ", c.AcceptEncoding)
}
// Addr represents a Salty IM User's Address
type Addr struct {
User string
@ -78,12 +82,14 @@ func (a *Addr) MarshalJSON() ([]byte, error) {
Domain string
Key string
Endpoint string
Avatar string
}{
User: a.User,
Domain: a.Domain,
Addr: a.String(),
Key: a.key.ID().String(),
Endpoint: a.Endpoint().String(),
Avatar: a.Avatar(),
})
}
@ -94,6 +100,7 @@ func (a *Addr) UnmarshalJSON(data []byte) error {
Domain string
Key string
Endpoint string
Avatar string
}{}
if err := json.Unmarshal(data, &res); err != nil {
@ -114,6 +121,7 @@ func (a *Addr) UnmarshalJSON(data []byte) error {
return fmt.Errorf("error parsing public key %q: %w", res.Key, err)
}
a.key = key
a.avatar = res.Avatar
return nil
}
@ -196,6 +204,9 @@ func (a *Addr) Refresh() error {
a.endpoint = u
a.capabilities = cap
log.Debugf("Discovered endpoint: %v", a.endpoint)
log.Debugf("Discovered capability: %v", a.capabilities)
return nil
}
@ -205,9 +216,9 @@ func (a *Addr) Avatar() string {
}
log.Debugf("Looking up SRV record for _avatars._tcp.%s", a.Domain)
if target, err := resolver.LookupSRV("salty", "tcp", a.Domain); err == nil {
a.avatar = fmt.Sprintf("%s/avatar/%x", target, a.Hash())
}
if target, err := resolver.LookupSRV("avatars", "tcp", a.Domain); err == nil {
a.avatar = fmt.Sprintf("https://%s/avatar/%s", target, a.Hash())
} else { log.Info(target, err) }
return a.avatar
}

@ -37,7 +37,7 @@ type StandardResolver struct{}
func (r *StandardResolver) LookupSRV(service, proto, domain string) (string, error) {
log.Debugf("Using StandardResolver, looking up SRV _%s._%s.%s", service, proto, domain)
_, records, err := net.LookupSRV("salty", "tcp", domain)
_, records, err := net.LookupSRV(service, proto, domain)
if err != nil {
return "", fmt.Errorf("error looking up _%s._%s.%s : %w", service, proto, domain, err)
}

Loading…
Cancel
Save