From 6aeb4836a2959f30e96da3ddf3fc6a3bb4f852ac Mon Sep 17 00:00:00 2001 From: "Kevin C. Coram" Date: Sat, 4 Jan 2020 21:17:42 -0500 Subject: [PATCH] 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. --- config.go | 2 +- main.go | 4 +++- mediaendpoint.go | 5 +++++ micropub.go | 10 +++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 47ef730..1f2a0d5 100644 --- a/config.go +++ b/config.go @@ -87,7 +87,7 @@ func initConfig() (err error) { return nil }() if SelectedMediaStorage == nil { - return errors.New("no media storage configured") + log.Println("no media storage configured") } // Find selected CDN (optional) SelectedCdn = func() Cdn { diff --git a/main.go b/main.go index 8153afe..dc1446d 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,9 @@ func main() { log.Println("Blog URL: " + BlogUrl) log.Println("Ignored URLs for Webmention: " + strings.Join(IgnoredWebmentionUrls, ", ")) http.HandleFunc("/micropub", HandleMicroPub) - http.HandleFunc("/media", HandleMedia) + if SelectedMediaStorage != nil { + http.HandleFunc("/media", HandleMedia) + } http.HandleFunc("/webmention", HandleWebmention) log.Fatal(http.ListenAndServe(":5555", nil)) } diff --git a/mediaendpoint.go b/mediaendpoint.go index 60297d9..adf07da 100644 --- a/mediaendpoint.go +++ b/mediaendpoint.go @@ -11,6 +11,11 @@ import ( ) 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" { w.WriteHeader(http.StatusMethodNotAllowed) _, _ = w.Write([]byte("The HTTP method is not allowed, make a POST request")) diff --git a/micropub.go b/micropub.go index 4fc146d..8417218 100644 --- a/micropub.go +++ b/micropub.go @@ -12,6 +12,14 @@ type MicropubConfig struct { MediaEndpoint string `json:"media-endpoint,omitempty"` } +func getMediaEndpoint() string { + if SelectedMediaStorage != nil { + return MediaEndpointUrl + } else { + return "" + } +} + func HandleMicroPub(w http.ResponseWriter, r *http.Request) { // a handler for GET requests, used for troubleshooting if r.Method == "GET" { @@ -20,7 +28,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) jsonBytes, err := json.Marshal(&MicropubConfig{ SyndicateTo: SyndicationTargets, - MediaEndpoint: MediaEndpointUrl, + MediaEndpoint: getMediaEndpoint(), }) if err != nil { w.WriteHeader(http.StatusBadRequest)