chore: cleanup code of Ekster
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2021-10-20 20:23:54 +02:00
parent 39e08e6026
commit 44b73e1c79
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 15 additions and 19 deletions

View File

@ -658,11 +658,8 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w) if err := json.NewEncoder(w).Encode(&res); err != nil {
err = enc.Encode(&res)
if err != nil {
log.Println(err) log.Println(err)
fmt.Fprintf(w, "ERROR: %q", err)
return return
} }
return return

View File

@ -88,6 +88,7 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err = json.NewEncoder(w).Encode(map[string]string{"ok": "1"}); err != nil { if err = json.NewEncoder(w).Encode(map[string]string{"ok": "1"}); err != nil {
http.Error(w, "internal server error", 500) http.Error(w, "internal server error", 500)
} }
return return
} }
@ -103,40 +104,38 @@ func generateItemID(conn redis.Conn, channel string) (string, error) {
} }
func parseIncomingItem(r *http.Request) (*microsub.Item, error) { func parseIncomingItem(r *http.Request) (*microsub.Item, error) {
var item microsub.Item
contentType := r.Header.Get("content-type") contentType := r.Header.Get("content-type")
if contentType == "application/jf2+json" { if contentType == "application/jf2+json" {
dec := json.NewDecoder(r.Body) var item microsub.Item
err := dec.Decode(&item) if err := json.NewDecoder(r.Body).Decode(&item); err != nil {
if err != nil { return nil, fmt.Errorf("could not decode request body as %q: %v", contentType, err)
return nil, errors.Wrapf(err, "could not decode request body as jf2: %v", err)
} }
return &item, nil
} else if contentType == "application/json" { } else if contentType == "application/json" {
var mfItem microformats.Microformat var mfItem microformats.Microformat
dec := json.NewDecoder(r.Body) if err := json.NewDecoder(r.Body).Decode(&mfItem); err != nil {
err := dec.Decode(&mfItem) return nil, fmt.Errorf("could not decode request body as %q: %v", contentType, err)
if err != nil {
return nil, errors.Wrapf(err, "could not decode request body as json: %v", err)
} }
author := microsub.Card{} author := microsub.Card{}
var ok bool item, ok := jf2.SimplifyMicroformatItem(&mfItem, author)
item, ok = jf2.SimplifyMicroformatItem(&mfItem, author)
if !ok { if !ok {
return nil, fmt.Errorf("could not simplify microformat item to jf2") return nil, fmt.Errorf("could not simplify microformat item to jf2")
} }
return &item, nil
} else if contentType == "application/x-www-form-urlencoded" { } else if contentType == "application/x-www-form-urlencoded" {
// TODO: improve handling of form-urlencoded
var item microsub.Item
content := r.FormValue("content") content := r.FormValue("content")
name := r.FormValue("name") name := r.FormValue("name")
item.Type = "entry" item.Type = "entry"
item.Name = name item.Name = name
item.Content = &microsub.Content{Text: content} item.Content = &microsub.Content{Text: content}
item.Published = time.Now().Format(time.RFC3339) item.Published = time.Now().Format(time.RFC3339)
} else {
return nil, fmt.Errorf("content-type %s is not supported", contentType)
}
return &item, nil return &item, nil
}
return nil, fmt.Errorf("content-type %q is not supported", contentType)
} }
func getChannelFromAuthorization(r *http.Request, conn redis.Conn) (string, error) { func getChannelFromAuthorization(r *http.Request, conn redis.Conn) (string, error) {