Compare commits

...

4 Commits

Author SHA1 Message Date
2bf3ce7aa4
Remove GET /micropub request
All checks were successful
continuous-integration/drone/push Build is passing
2019-03-23 21:04:42 +01:00
895af674e3
Extract parseIncomingItem 2019-03-23 21:03:45 +01:00
3f96dcfde3
Clean up errors in micropub.go 2019-03-23 20:53:52 +01:00
96bade5a53
Improve error handling in main 2019-03-23 20:48:47 +01:00
2 changed files with 70 additions and 53 deletions

View File

@ -24,6 +24,7 @@ import (
"time"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"p83.nl/go/ekster/pkg/auth"
"p83.nl/go/ekster/pkg/server"
@ -97,16 +98,16 @@ type App struct {
}
// Run runs the app
func (app *App) Run() {
func (app *App) Run() error {
app.backend.run()
app.hubBackend.run()
log.Printf("Listening on port %d\n", app.options.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil))
return http.ListenAndServe(fmt.Sprintf(":%d", app.options.Port), nil)
}
// NewApp initializes the App
func NewApp(options AppOptions) *App {
func NewApp(options AppOptions) (*App, error) {
app := &App{
options: options,
}
@ -137,12 +138,12 @@ func NewApp(options AppOptions) *App {
if !options.Headless {
handler, err := newMainHandler(app.backend, options.BaseURL, options.TemplateDir, options.pool)
if err != nil {
log.Fatal(err)
return nil, errors.Wrap(err, "could not create main handler")
}
http.Handle("/", handler)
}
return app
return app, nil
}
func main() {
@ -204,5 +205,13 @@ func main() {
pool := newPool(options.RedisServer)
options.pool = pool
NewApp(options).Run()
app, err := NewApp(options)
if err != nil {
log.Fatal(err)
}
err = app.Run()
if err != nil {
log.Fatal(err)
}
}

View File

@ -13,6 +13,7 @@ import (
"p83.nl/go/ekster/pkg/microsub"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"willnorris.com/go/microformats"
)
@ -21,6 +22,43 @@ type micropubHandler struct {
pool *redis.Pool
}
func parseIncomingItem(r *http.Request) (*microsub.Item, error) {
var item microsub.Item
contentType := r.Header.Get("content-type")
if contentType == "application/jf2+json" {
dec := json.NewDecoder(r.Body)
err := dec.Decode(&item)
if err != nil {
return nil, errors.Wrapf(err, "could not decode request body as jf2: %v", err)
}
} else if contentType == "application/json" {
var mfItem microformats.Microformat
dec := json.NewDecoder(r.Body)
err := dec.Decode(&mfItem)
if err != nil {
return nil, errors.Wrapf(err, "could not decode request body as json: %v", err)
}
author := microsub.Card{}
var ok bool
item, ok = jf2.SimplifyMicroformatItem(&mfItem, author)
if !ok {
return nil, fmt.Errorf("could not simplify microformat item to jf2")
}
} else if contentType == "application/x-www-form-urlencoded" {
content := r.FormValue("content")
name := r.FormValue("name")
item.Type = "entry"
item.Name = name
item.Content = &microsub.Content{Text: content}
item.Published = time.Now().Format(time.RFC3339)
} else {
return nil, fmt.Errorf("content-type %s is not supported", contentType)
}
return &item, nil
}
/*
* URLs needed:
* - / with endpoint urls
@ -34,12 +72,13 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := h.pool.Get()
defer conn.Close()
r.ParseForm()
err := r.ParseForm()
if err != nil {
http.Error(w, "bad request", 400)
return
}
if r.Method == http.MethodGet {
// show profile with endpoint urls
} else if r.Method == http.MethodPost {
if r.Method == http.MethodPost {
sourceID := r.URL.Query().Get("source_id")
authHeader := r.Header.Get("Authorization")
@ -49,57 +88,26 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err := redis.String(conn.Do("HGET", "sources", sourceID))
if err != nil {
channel, err = redis.String(conn.Do("HGET", "token:"+sourceID, "channel"))
if err != nil {
http.Error(w, "Unknown source", 400)
http.Error(w, "unauthorized", 401)
return
}
}
var item microsub.Item
ok := false
if r.Header.Get("Content-Type") == "application/jf2+json" {
dec := json.NewDecoder(r.Body)
err := dec.Decode(&item)
if err != nil {
http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400)
return
}
ok = true
} else if r.Header.Get("Content-Type") == "application/json" {
var mfItem microformats.Microformat
dec := json.NewDecoder(r.Body)
err := dec.Decode(&mfItem)
if err != nil {
http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400)
return
}
author := microsub.Card{}
item, ok = jf2.SimplifyMicroformatItem(&mfItem, author)
} else if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
content := r.FormValue("content")
name := r.FormValue("name")
item.Type = "entry"
item.Name = name
item.Content = &microsub.Content{Text: content}
item.Published = time.Now().Format(time.RFC3339)
ok = true
} else {
http.Error(w, "Unsupported Content-Type", 400)
item, err := parseIncomingItem(r)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if ok {
item.Read = false
id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id"))
item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
err = h.Backend.channelAddItemWithMatcher(channel, item)
err = h.Backend.updateChannelUnreadCount(channel)
if err != nil {
log.Printf("error: while updating channel unread count for %s: %s\n", channel, err)
}
item.Read = false
id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id"))
item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
err = h.Backend.channelAddItemWithMatcher(channel, *item)
err = h.Backend.updateChannelUnreadCount(channel)
if err != nil {
log.Printf("could not update channel unread content %s: %v", channel, err)
}
w.Header().Set("Content-Type", "application/json")