Compare commits

...

3 Commits

Author SHA1 Message Date
Kevin C. Coram 6aeb4836a2
Make media storage optional
If generating the SelectedMediaStorage configuration results in an
error (because there is no MediaUrl or Bunny CDN is not configured)
then don't configure the `/media` end-point, and make sure that the
`/micropub?q=config` query does not return a media-endpoint URL.
2020-01-04 21:58:48 -05:00
Kevin C. Coram 802109cf31
Better support for Quill's "favorite" request
Quill doesn't send a `content` parameter along with the `like-of` data
when performing a "favorite" request.
2020-01-04 21:58:35 -05:00
Kevin C. Coram e0fc3147f7
Ignore `hugo-micropub` binary file 2020-01-04 21:13:50 -05:00
6 changed files with 33 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea .idea
*.iml *.iml
hugo-micropub

View File

@ -87,7 +87,7 @@ func initConfig() (err error) {
return nil return nil
}() }()
if SelectedMediaStorage == nil { if SelectedMediaStorage == nil {
return errors.New("no media storage configured") log.Println("no media storage configured")
} }
// Find selected CDN (optional) // Find selected CDN (optional)
SelectedCdn = func() Cdn { SelectedCdn = func() Cdn {

View File

@ -75,13 +75,23 @@ func parseRequestBody(r *http.Request) (string, error) {
return string(bodyBytes), nil return string(bodyBytes), nil
} }
func validEntry(values map[string][]string) bool {
if _, ok := values["content"]; ok {
return true
} else if _, ok := values["like-of"]; ok {
return true
}
return false;
}
func createEntryFromValueMap(values map[string][]string) (*Entry, error) { func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") { if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") {
return nil, errors.New("only entry type is supported so far") return nil, errors.New("only entry type is supported so far")
} }
if _, ok := values["content"]; ok { if validEntry(values) {
entry := &Entry{ entry := &Entry{}
content: values["content"][0], if content, ok := values["content"]; ok {
entry.content = content[0]
} }
if name, ok := values["name"]; ok { if name, ok := values["name"]; ok {
entry.title = name[0] entry.title = name[0]

View File

@ -17,7 +17,9 @@ func main() {
log.Println("Blog URL: " + BlogUrl) log.Println("Blog URL: " + BlogUrl)
log.Println("Ignored URLs for Webmention: " + strings.Join(IgnoredWebmentionUrls, ", ")) log.Println("Ignored URLs for Webmention: " + strings.Join(IgnoredWebmentionUrls, ", "))
http.HandleFunc("/micropub", HandleMicroPub) http.HandleFunc("/micropub", HandleMicroPub)
http.HandleFunc("/media", HandleMedia) if SelectedMediaStorage != nil {
http.HandleFunc("/media", HandleMedia)
}
http.HandleFunc("/webmention", HandleWebmention) http.HandleFunc("/webmention", HandleWebmention)
log.Fatal(http.ListenAndServe(":5555", nil)) log.Fatal(http.ListenAndServe(":5555", nil))
} }

View File

@ -11,6 +11,11 @@ import (
) )
func HandleMedia(w http.ResponseWriter, r *http.Request) { func HandleMedia(w http.ResponseWriter, r *http.Request) {
if SelectedMediaStorage == nil {
// This shouldn't happen since the HTTP handler isn't set up if there is no SelectedMediaStorage
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("No media endpoint configured"))
}
if r.Method != "POST" { if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
_, _ = w.Write([]byte("The HTTP method is not allowed, make a POST request")) _, _ = w.Write([]byte("The HTTP method is not allowed, make a POST request"))

View File

@ -12,6 +12,14 @@ type MicropubConfig struct {
MediaEndpoint string `json:"media-endpoint,omitempty"` MediaEndpoint string `json:"media-endpoint,omitempty"`
} }
func getMediaEndpoint() string {
if SelectedMediaStorage != nil {
return MediaEndpointUrl
} else {
return ""
}
}
func HandleMicroPub(w http.ResponseWriter, r *http.Request) { func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
// a handler for GET requests, used for troubleshooting // a handler for GET requests, used for troubleshooting
if r.Method == "GET" { if r.Method == "GET" {
@ -20,7 +28,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
jsonBytes, err := json.Marshal(&MicropubConfig{ jsonBytes, err := json.Marshal(&MicropubConfig{
SyndicateTo: SyndicationTargets, SyndicateTo: SyndicationTargets,
MediaEndpoint: MediaEndpointUrl, MediaEndpoint: getMediaEndpoint(),
}) })
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)