mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
40 lines
821 B
Go
40 lines
821 B
Go
package server
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/foomo/contentserver/repo"
|
|
)
|
|
|
|
const PathContentserver = "/contentserver"
|
|
|
|
type webServer struct {
|
|
r *repo.Repo
|
|
}
|
|
|
|
func newWebServer() (s *webServer, err error) {
|
|
s = &webServer{}
|
|
return
|
|
}
|
|
|
|
func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Body == nil {
|
|
http.Error(w, "no body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
jsonBytes, readErr := ioutil.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
if readErr != nil {
|
|
http.Error(w, "failed to read incoming request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
reply, errReply := handleRequest(s.r, Handler(strings.TrimPrefix(r.URL.Path, PathContentserver+"/")), jsonBytes)
|
|
if errReply != nil {
|
|
http.Error(w, errReply.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Write(reply)
|
|
}
|