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.
kcoram/uplift
Kevin C. Coram 2020-01-04 21:17:42 -05:00
parent 802109cf31
commit 6aeb4836a2
Signed by: kevin
GPG Key ID: 0342351B3D61AD35
4 changed files with 18 additions and 3 deletions

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

@ -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)
if SelectedMediaStorage != nil {
http.HandleFunc("/media", HandleMedia) 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)