Initial OCS routes, version & format handling

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2020-07-14 14:15:53 +02:00
parent c93dfbf091
commit 7aae61dca7
13 changed files with 636 additions and 20 deletions
+24
View File
@@ -0,0 +1,24 @@
package middleware
import (
"context"
"net/http"
"github.com/go-chi/render"
)
// OCSFormatCtx middleware is used to determine the content type from
// the format URL parameter passed in an ocs request. Defaults to XML
func OCSFormatCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Query().Get("format") {
case "", "xml":
r.Header.Set("Accept", "application/xml")
r = r.WithContext(context.WithValue(r.Context(), render.ContentTypeCtxKey, render.ContentTypeXML))
case "json":
r.Header.Set("Accept", "application/json")
r = r.WithContext(context.WithValue(r.Context(), render.ContentTypeCtxKey, render.ContentTypeJSON))
}
next.ServeHTTP(w, r)
})
}