2019-12-12 15:29:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-12-22 12:58:50 +00:00
|
|
|
"encoding/json"
|
2019-12-12 15:29:26 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-12-22 13:11:59 +00:00
|
|
|
type MicropubConfig struct {
|
2020-01-01 12:44:44 +00:00
|
|
|
SyndicateTo []SyndicationTarget `json:"syndicate-to,omitempty"`
|
|
|
|
MediaEndpoint string `json:"media-endpoint,omitempty"`
|
2019-12-22 13:11:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:29:26 +00:00
|
|
|
func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// a handler for GET requests, used for troubleshooting
|
|
|
|
if r.Method == "GET" {
|
2019-12-22 13:11:59 +00:00
|
|
|
if q := r.URL.Query().Get("q"); q == "config" || q == "syndicate-to" {
|
2019-12-12 15:29:26 +00:00
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-12-22 13:11:59 +00:00
|
|
|
jsonBytes, err := json.Marshal(&MicropubConfig{
|
2020-01-01 12:44:44 +00:00
|
|
|
SyndicateTo: SyndicationTargets,
|
|
|
|
MediaEndpoint: MediaEndpointUrl,
|
2019-12-22 13:11:59 +00:00
|
|
|
})
|
2019-12-22 12:58:50 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _ = w.Write(jsonBytes)
|
2019-12-12 15:29:26 +00:00
|
|
|
return
|
|
|
|
} else if url := r.URL.Query().Get("url"); q == "source" {
|
|
|
|
limit := r.URL.Query().Get("limit")
|
|
|
|
limitInt, err := strconv.Atoi(limit)
|
|
|
|
jsonBytes, err := QueryURL(url, limitInt)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write(jsonBytes)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write([]byte("{}"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if the request is a POST
|
|
|
|
if r.Method != "POST" {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
_, _ = w.Write([]byte("The HTTP method is not allowed, make a POST request"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// check content type
|
|
|
|
contentType, err := GetContentType(r.Header.Get("content-type"))
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Create entry
|
|
|
|
entry, err := CreateEntry(contentType, r)
|
|
|
|
if err != nil || entry == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err != nil {
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write([]byte("There was an error creating the entry"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
if authHeader := r.Header.Get("authorization"); len(entry.token) == 0 && len(authHeader) > 0 {
|
|
|
|
entry.token = authHeader
|
|
|
|
}
|
|
|
|
if CheckAuthorization(entry.token) {
|
2019-12-12 15:29:26 +00:00
|
|
|
location, err := WriteEntry(entry)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte("There was an error committing the entry to the repository"))
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
w.Header().Add("Location", location)
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2020-01-16 12:10:37 +00:00
|
|
|
// Purge CDN after 30 seconds, send webmentions, post to social media
|
2019-12-12 15:29:26 +00:00
|
|
|
go func() {
|
2020-01-16 12:10:37 +00:00
|
|
|
time.Sleep(30 * time.Second)
|
2019-12-22 20:17:11 +00:00
|
|
|
if SelectedCdn != nil {
|
|
|
|
SelectedCdn.Purge(location)
|
2020-01-16 12:10:37 +00:00
|
|
|
time.Sleep(10 * time.Second)
|
2019-12-22 20:17:11 +00:00
|
|
|
}
|
2019-12-12 15:29:26 +00:00
|
|
|
// Send webmentions
|
2019-12-12 21:36:05 +00:00
|
|
|
go SendWebmentions(location)
|
2019-12-22 20:17:11 +00:00
|
|
|
go func() {
|
|
|
|
if SelectedSocials != nil {
|
|
|
|
SelectedSocials.Post(location, entry.title)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-12 15:29:26 +00:00
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
_, _ = w.Write([]byte("Forbidden, there was a problem with the provided access token"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|