refactored GetRepo: serve JSON from fs and write directly into http.ResponseWriter

This commit is contained in:
Philipp Mieden 2019-05-24 16:41:57 +02:00
parent 8224e92d4d
commit 871c844f7b
3 changed files with 24 additions and 6 deletions

View File

@ -4,6 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
@ -218,6 +220,15 @@ func (repo *Repo) GetRepo() map[string]*content.RepoNode {
return response
}
// WriteRepoBytes get the whole repo in all dimensions
func (repo *Repo) WriteRepoBytes(w io.Writer) {
f, err := os.Open(repo.history.getCurrentFilename())
_, err = io.Copy(w, f)
if err != nil {
Log.Error("failed to serve Repo JSON", zap.Error(err))
}
}
// Update - reload contents of repository with json from repo.server
func (repo *Repo) Update() (updateResponse *responses.Update) {
floatSeconds := func(nanoSeconds int64) float64 {

View File

@ -51,11 +51,12 @@ func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, source strin
processIfJSONIsOk(json.Unmarshal(jsonBytes, &updateRequest), func() {
reply = r.Update()
})
case HandlerGetRepo:
repoRequest := &requests.Repo{}
processIfJSONIsOk(json.Unmarshal(jsonBytes, &repoRequest), func() {
reply = r.GetRepo()
})
// case HandlerGetRepo:
// repoRequest := &requests.Repo{}
// processIfJSONIsOk(json.Unmarshal(jsonBytes, &repoRequest), func() {
// reply = r.GetRepo()
// })
default:
reply = responses.NewError(1, "unknown handler: "+string(handler))
}

View File

@ -35,7 +35,13 @@ func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "failed to read incoming request", http.StatusBadRequest)
return
}
reply, errReply := handleRequest(s.r, Handler(strings.TrimPrefix(r.URL.Path, s.path+"/")), jsonBytes, "webserver")
h := Handler(strings.TrimPrefix(r.URL.Path, s.path+"/"))
if h == HandlerGetRepo {
s.r.WriteRepoBytes(w)
w.Header().Set("Content-Type", "application/json")
return
}
reply, errReply := handleRequest(s.r, h, jsonBytes, "webserver")
if errReply != nil {
http.Error(w, errReply.Error(), http.StatusInternalServerError)
return