File: server.go

package info (click to toggle)
sigsum-go 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,076 kB
  • sloc: sh: 809; makefile: 93
file content (99 lines) | stat: -rw-r--r-- 3,039 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// package server implements the http-layer of the Sigsum apis.
// It defines handlers for incoming HTTP requests, converting to
// request to a method call on the appropriate api interface. It checks
// for errors where it's clear that a request is bad according to the
// specs, regardless of what's backing the api interface. It converts
// the api method's return values (success or errors) into a http
// response to be returned to the client. Optionally, it can produce
// basic request and response metrics.
package server

import (
	"context"
	"net/http"
	"net/url"
	"time"

	"sigsum.org/sigsum-go/pkg/api"
	"sigsum.org/sigsum-go/pkg/log"
	"sigsum.org/sigsum-go/pkg/types"
)

type server struct {
	config Config
	mux    *http.ServeMux
}

func newServer(config *Config) *server {
	return &server{config: config.withDefaults(), mux: http.NewServeMux()}
}

// A response writer that records the status code.
type responseWriterWithStatus struct {
	statusCode int
	w          http.ResponseWriter
}

func (ws *responseWriterWithStatus) Header() http.Header {
	return ws.w.Header()
}

func (ws *responseWriterWithStatus) Write(data []byte) (int, error) {
	return ws.w.Write(data)
}
func (ws *responseWriterWithStatus) WriteHeader(statusCode int) {
	ws.statusCode = statusCode
	ws.w.WriteHeader(statusCode)
}

// Wrapper to produce metrics.
type handlerWithMetrics struct {
	config   *Config
	endpoint string
	handler  http.Handler
}

func (h *handlerWithMetrics) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	h.config.Metrics.OnRequest(h.endpoint)
	start := time.Now()

	response := responseWriterWithStatus{w: w, statusCode: http.StatusOK}
	defer func() {
		latency := time.Now().Sub(start)
		h.config.Metrics.OnResponse(h.endpoint, response.statusCode, latency)
	}()

	h.handler.ServeHTTP(&response, r)
}

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx, cancel := context.WithTimeout(r.Context(), s.config.Timeout)
	defer cancel()
	s.mux.ServeHTTP(w, r.WithContext(ctx))
}

func (s *server) register(method string, endpoint types.Endpoint, args string, handler http.Handler) {
	s.mux.Handle(method+" /"+endpoint.Path(s.config.Prefix)+args,
		&handlerWithMetrics{config: &s.config, endpoint: string(endpoint), handler: handler})
}

// Note that it's not useful to report errors that occur when writing
// the response: It's too late to change the status code, and the
// likely reason for the error is that the client has disconnected.
func reportError(w http.ResponseWriter, url *url.URL, err error) {
	statusCode := api.ErrorStatusCode(err)
	if statusCode == http.StatusInternalServerError {
		log.Error("Internal server error for %q: %v", url.Path, err)
	} else {
		log.Debug("%q: status %d, %v", url.Path, statusCode, err)
	}
	http.Error(w, err.Error(), statusCode)
}

func logError(url *url.URL, err error) {
	log.Debug("%q: request failed: %v", url.Path, err)
}

var handlerBadRequest = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	reportError(w, r.URL, api.ErrBadRequest)
})