Skip to content

Commit 4f53f04

Browse files
committed
refactor(serverHandler): extract page rendering func
1 parent ac027e4 commit 4f53f04

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/serverHandler/main.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5050
}
5151
}
5252

53-
data, notFound, internalError := h.getResponseData(r)
53+
data := h.getResponseData(r)
5454
if len(data.Errors) > 0 {
5555
go func() {
5656
for _, err := range data.Errors {
@@ -99,18 +99,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9999
return
100100
}
101101

102-
header := w.Header()
103-
header.Set("Content-Type", "text/html; charset=utf-8")
104-
header.Set("Cache-Control", "public, max-age=0")
105-
if internalError {
106-
w.WriteHeader(http.StatusInternalServerError)
107-
} else if notFound {
108-
w.WriteHeader(http.StatusNotFound)
109-
} else {
110-
w.WriteHeader(http.StatusOK)
111-
}
112-
err := h.template.Execute(w, data)
113-
h.errHandler.LogError(err)
102+
h.page(w, r, data)
114103
}
115104

116105
func NewHandler(

src/serverHandler/page.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package serverHandler
2+
3+
import "net/http"
4+
5+
func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseData) {
6+
header := w.Header()
7+
header.Set("Content-Type", "text/html; charset=utf-8")
8+
header.Set("Cache-Control", "public, max-age=0")
9+
10+
if data.hasInternalError {
11+
w.WriteHeader(http.StatusInternalServerError)
12+
} else if data.hasNotFoundError {
13+
w.WriteHeader(http.StatusNotFound)
14+
} else {
15+
w.WriteHeader(http.StatusOK)
16+
}
17+
18+
err := h.template.Execute(w, data)
19+
h.errHandler.LogError(err)
20+
}

src/serverHandler/responseData.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type responseData struct {
1919
rawReqPath string
2020
handlerReqPath string
2121

22+
hasNotFoundError bool
23+
hasInternalError bool
24+
2225
IsRoot bool
2326
Path string
2427
Paths []*pathEntry
@@ -294,13 +297,15 @@ func (h *handler) getCanCors(rawReqPath, reqFsPath string) bool {
294297
return hasUrlOrDirPrefix(h.corsUrls, rawReqPath, h.corsDirs, reqFsPath)
295298
}
296299

297-
func (h *handler) getResponseData(r *http.Request) (data *responseData, notFound, internalError bool) {
300+
func (h *handler) getResponseData(r *http.Request) (data *responseData) {
298301
requestUri := r.URL.Path
299302
tailSlash := requestUri[len(requestUri)-1] == '/'
300303

301304
rawReqPath := util.CleanUrlPath(requestUri)
302305
reqPath := util.CleanUrlPath(rawReqPath[len(h.urlPrefix):]) // strip url prefix path
303306
errs := []error{}
307+
notFound := false
308+
internalError := false
304309

305310
isRoot := rawReqPath == "/"
306311

@@ -347,6 +352,9 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData, notFound
347352
rawReqPath: rawReqPath,
348353
handlerReqPath: reqPath,
349354

355+
hasNotFoundError: notFound,
356+
hasInternalError: internalError,
357+
350358
IsRoot: isRoot,
351359
Path: rawReqPath,
352360
Paths: pathEntries,

0 commit comments

Comments
 (0)