mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
refactored GetRepo: serve JSON from fs and write directly into http.ResponseWriter
This commit is contained in:
parent
8224e92d4d
commit
871c844f7b
11
repo/repo.go
11
repo/repo.go
@ -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 {
|
||||
|
||||
@ -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))
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user