Compare commits

..

No commits in common. "2bf3ce7aa48d58e4542265a83f4deaec5405999c" and "3d4c4744fe140d61edc7619501a018cdeea40738" have entirely different histories.

2 changed files with 53 additions and 70 deletions

View File

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

View File

@ -13,7 +13,6 @@ import (
"p83.nl/go/ekster/pkg/microsub" "p83.nl/go/ekster/pkg/microsub"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"willnorris.com/go/microformats" "willnorris.com/go/microformats"
) )
@ -22,43 +21,6 @@ type micropubHandler struct {
pool *redis.Pool 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: * URLs needed:
* - / with endpoint urls * - / with endpoint urls
@ -72,13 +34,12 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := h.pool.Get() conn := h.pool.Get()
defer conn.Close() defer conn.Close()
err := r.ParseForm() r.ParseForm()
if err != nil {
http.Error(w, "bad request", 400)
return
}
if r.Method == http.MethodPost { if r.Method == http.MethodGet {
// show profile with endpoint urls
} else if r.Method == http.MethodPost {
sourceID := r.URL.Query().Get("source_id") sourceID := r.URL.Query().Get("source_id")
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
@ -88,26 +49,57 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
channel, err := redis.String(conn.Do("HGET", "sources", sourceID)) channel, err := redis.String(conn.Do("HGET", "sources", sourceID))
if err != nil { if err != nil {
channel, err = redis.String(conn.Do("HGET", "token:"+sourceID, "channel")) channel, err = redis.String(conn.Do("HGET", "token:"+sourceID, "channel"))
if err != nil { if err != nil {
http.Error(w, "unauthorized", 401) http.Error(w, "Unknown source", 400)
return return
} }
} }
item, err := parseIncomingItem(r) 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 { if err != nil {
http.Error(w, err.Error(), 400) 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 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)
return
}
if ok {
item.Read = false item.Read = false
id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id")) 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)))) item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
err = h.Backend.channelAddItemWithMatcher(channel, *item) err = h.Backend.channelAddItemWithMatcher(channel, item)
err = h.Backend.updateChannelUnreadCount(channel) err = h.Backend.updateChannelUnreadCount(channel)
if err != nil { if err != nil {
log.Printf("could not update channel unread content %s: %v", channel, err) log.Printf("error: while updating channel unread count for %s: %s\n", channel, err)
}
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")