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" "bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -218,6 +220,15 @@ func (repo *Repo) GetRepo() map[string]*content.RepoNode {
return response 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 // Update - reload contents of repository with json from repo.server
func (repo *Repo) Update() (updateResponse *responses.Update) { func (repo *Repo) Update() (updateResponse *responses.Update) {
floatSeconds := func(nanoSeconds int64) float64 { 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() { processIfJSONIsOk(json.Unmarshal(jsonBytes, &updateRequest), func() {
reply = r.Update() reply = r.Update()
}) })
case HandlerGetRepo: // case HandlerGetRepo:
repoRequest := &requests.Repo{} // repoRequest := &requests.Repo{}
processIfJSONIsOk(json.Unmarshal(jsonBytes, &repoRequest), func() { // processIfJSONIsOk(json.Unmarshal(jsonBytes, &repoRequest), func() {
reply = r.GetRepo() // reply = r.GetRepo()
}) // })
default: default:
reply = responses.NewError(1, "unknown handler: "+string(handler)) 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) http.Error(w, "failed to read incoming request", http.StatusBadRequest)
return 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 { if errReply != nil {
http.Error(w, errReply.Error(), http.StatusInternalServerError) http.Error(w, errReply.Error(), http.StatusInternalServerError)
return return