From 7f6eafdce52972021643512d09e46eaef88c810d Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Fri, 6 Nov 2015 14:46:55 +0000 Subject: [PATCH 1/2] continuserv: set Content-Type header --- scripts/continuserv/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/continuserv/main.go b/scripts/continuserv/main.go index 573c2c95..425a1af9 100644 --- a/scripts/continuserv/main.go +++ b/scripts/continuserv/main.go @@ -113,6 +113,7 @@ func filter(e fsnotify.Event) bool { func serve(w http.ResponseWriter, req *http.Request) { wg.Wait() b := toServe.Load().([]byte) + w.Header().Set("Content-Type", "text/html") w.Write(b) } From 1be5b856bd2447aa557ada4fcde8b8ddf63a3f70 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Fri, 6 Nov 2015 16:05:07 +0000 Subject: [PATCH 2/2] Preserve text/plain for errors Newlines are nice --- scripts/continuserv/main.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/continuserv/main.go b/scripts/continuserv/main.go index 425a1af9..4613437a 100644 --- a/scripts/continuserv/main.go +++ b/scripts/continuserv/main.go @@ -25,7 +25,7 @@ import ( var ( port = flag.Int("port", 8000, "Port on which to serve HTTP") - toServe atomic.Value // Always contains valid []byte to serve. May be stale unless wg is zero. + toServe atomic.Value // Always contains valid bytesOrErr to serve. May be stale unless wg is zero. wg sync.WaitGroup // Indicates how many updates are pending. mu sync.Mutex // Prevent multiple updates in parallel. ) @@ -112,9 +112,14 @@ func filter(e fsnotify.Event) bool { func serve(w http.ResponseWriter, req *http.Request) { wg.Wait() - b := toServe.Load().([]byte) - w.Header().Set("Content-Type", "text/html") - w.Write(b) + b := toServe.Load().(bytesOrErr) + if b.err != nil { + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(b.err.Error())) + } else { + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(b.bytes)) + } } func populateOnce(dir string) { @@ -127,15 +132,15 @@ func populateOnce(dir string) { cmd.Stderr = &b err := cmd.Run() if err != nil { - toServe.Store([]byte(fmt.Errorf("error generating spec: %v\nOutput from gendoc:\n%v", err, b.String()).Error())) + toServe.Store(bytesOrErr{nil, fmt.Errorf("error generating spec: %v\nOutput from gendoc:\n%v", err, b.String())}) return } specBytes, err := ioutil.ReadFile(path.Join(dir, "scripts", "gen", "specification.html")) if err != nil { - toServe.Store([]byte(fmt.Errorf("error reading spec: %v", err).Error())) + toServe.Store(bytesOrErr{nil, fmt.Errorf("error reading spec: %v", err)}) return } - toServe.Store(specBytes) + toServe.Store(bytesOrErr{specBytes, nil}) } func doPopulate(ch chan struct{}, dir string) { @@ -160,3 +165,8 @@ func exists(path string) bool { _, err := os.Stat(path) return !os.IsNotExist(err) } + +type bytesOrErr struct { + bytes []byte + err error +}