server.go 2.08 KB
Newer Older
1 2 3 4 5 6 7
package server

import (
	"encoding/json"
	"net/http"
	"runtime/debug"
	"time"
8

9
	"github.com/ethereum-optimism/optimism/indexer/metrics"
10

11
	"github.com/ethereum/go-ethereum/log"
12 13
)

14
// RespondWithError writes the given error code and message to the writer.
15 16 17 18
func RespondWithError(w http.ResponseWriter, code int, message string) {
	RespondWithJSON(w, code, map[string]string{"error": message})
}

19
// RespondWithJSON writes the given payload marshalled as JSON to the writer.
20 21 22 23 24
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
	response, _ := json.Marshal(payload)

	w.WriteHeader(code)
	w.Header().Set("Content-Type", "application/json")
25
	_, _ = w.Write(response)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
}

// responseWriter is a minimal wrapper for http.ResponseWriter that allows the
// written HTTP status code to be captured for logging.
type responseWriter struct {
	http.ResponseWriter
	status      int
	wroteHeader bool
}

func wrapResponseWriter(w http.ResponseWriter) *responseWriter {
	return &responseWriter{ResponseWriter: w}
}

func (rw *responseWriter) Status() int {
	return rw.status
}

func (rw *responseWriter) WriteHeader(code int) {
	if rw.wroteHeader {
		return
	}

	rw.status = code
	rw.ResponseWriter.WriteHeader(code)
	rw.wroteHeader = true
}

// LoggingMiddleware logs the incoming HTTP request & its duration.
55
func LoggingMiddleware(metrics *metrics.Metrics, logger log.Logger) func(http.Handler) http.Handler {
56 57 58 59 60 61 62 63 64 65 66 67 68
	return func(next http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			defer func() {
				if err := recover(); err != nil {
					w.WriteHeader(http.StatusInternalServerError)
					logger.Error(
						"server panicked",
						"err", err,
						"trace", debug.Stack(),
					)
				}
			}()

69
			metrics.RecordHTTPRequest()
70 71 72
			start := time.Now()
			wrapped := wrapResponseWriter(w)
			next.ServeHTTP(wrapped, r)
73
			dur := time.Since(start)
74 75 76 77 78
			logger.Info(
				"served request",
				"status", wrapped.status,
				"method", r.Method,
				"path", r.URL.EscapedPath(),
79
				"duration", dur,
80
			)
81
			metrics.RecordHTTPResponse(wrapped.status, dur)
82 83 84 85 86
		}

		return http.HandlerFunc(fn)
	}
}