Compare commits
4 Commits
3d4c4744fe
...
2bf3ce7aa4
| Author | SHA1 | Date | |
|---|---|---|---|
| 2bf3ce7aa4 | |||
| 895af674e3 | |||
| 3f96dcfde3 | |||
| 96bade5a53 |
|
|
@ -24,6 +24,7 @@ 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"
|
||||||
|
|
@ -97,16 +98,16 @@ type App struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the app
|
// Run runs the app
|
||||||
func (app *App) Run() {
|
func (app *App) Run() error {
|
||||||
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)
|
||||||
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
|
// NewApp initializes the App
|
||||||
func NewApp(options AppOptions) *App {
|
func NewApp(options AppOptions) (*App, error) {
|
||||||
app := &App{
|
app := &App{
|
||||||
options: options,
|
options: options,
|
||||||
}
|
}
|
||||||
|
|
@ -137,12 +138,12 @@ func NewApp(options AppOptions) *App {
|
||||||
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 {
|
||||||
log.Fatal(err)
|
return nil, errors.Wrap(err, "could not create main handler")
|
||||||
}
|
}
|
||||||
http.Handle("/", handler)
|
http.Handle("/", handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
return app
|
return app, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -204,5 +205,13 @@ func main() {
|
||||||
pool := newPool(options.RedisServer)
|
pool := newPool(options.RedisServer)
|
||||||
options.pool = pool
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -21,6 +22,43 @@ 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 = µsub.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
|
||||||
|
|
@ -34,12 +72,13 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
conn := h.pool.Get()
|
conn := h.pool.Get()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
r.ParseForm()
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "bad request", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodPost {
|
||||||
// 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")
|
||||||
|
|
@ -49,57 +88,26 @@ 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, "Unknown source", 400)
|
http.Error(w, "unauthorized", 401)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var item microsub.Item
|
item, err := parseIncomingItem(r)
|
||||||
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, fmt.Sprintf("Error decoding: %v", err), 400)
|
http.Error(w, err.Error(), 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 = µsub.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("error: while updating channel unread count for %s: %s\n", channel, err)
|
log.Printf("could not update channel unread content %s: %v", channel, err)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user