WrapHandler wraps an httpctx.Handler in the paraphernalia needed by devd for logging, latency, and so forth.
(log termlog.TermLog, next httpctx.Handler)
| 163 | // WrapHandler wraps an httpctx.Handler in the paraphernalia needed by devd for |
| 164 | // logging, latency, and so forth. |
| 165 | func (dd *Devd) WrapHandler(log termlog.TermLog, next httpctx.Handler) http.Handler { |
| 166 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 167 | r.URL.Scheme = dd.ServingScheme |
| 168 | revertOriginalHost(r) |
| 169 | timr := timer.Timer{} |
| 170 | sublog := log.Group() |
| 171 | defer func() { |
| 172 | timing := termlog.DefaultPalette.Timestamp.SprintFunc()("timing: ") |
| 173 | sublog.SayAs("timer", timing+timr.String()) |
| 174 | sublog.Done() |
| 175 | }() |
| 176 | if matchStringAny(dd.IgnoreLogs, fmt.Sprintf("%s%s", r.URL.Host, r.RequestURI)) { |
| 177 | sublog.Quiet() |
| 178 | } |
| 179 | timr.RequestHeaders() |
| 180 | time.Sleep(time.Millisecond * time.Duration(dd.Latency)) |
| 181 | |
| 182 | dpath := r.RequestURI |
| 183 | if !strings.HasPrefix(dpath, "/") { |
| 184 | dpath = "/" + dpath |
| 185 | } |
| 186 | sublog.Say("%s %s", r.Method, dpath) |
| 187 | LogHeader(sublog, r.Header) |
| 188 | ctx := timr.NewContext(context.Background()) |
| 189 | ctx = termlog.NewContext(ctx, sublog) |
| 190 | if dd.AddHeaders != nil { |
| 191 | for h, vals := range *dd.AddHeaders { |
| 192 | for _, v := range vals { |
| 193 | w.Header().Set(h, v) |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | flusher, _ := w.(http.Flusher) |
| 198 | next.ServeHTTPContext( |
| 199 | ctx, |
| 200 | &ResponseLogWriter{Log: sublog, Resp: w, Flusher: flusher, Timer: &timr}, |
| 201 | r, |
| 202 | ) |
| 203 | }) |
| 204 | return h |
| 205 | } |
| 206 | |
| 207 | // HasLivereload tells us if livereload is enabled |
| 208 | func (dd *Devd) HasLivereload() bool { |