GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — kit/httpserver/httpserver.go at master · gearnode/kit

Expand file tree

/

httpserver.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
142 lines (126 loc) · 3.77 KB
/

httpserver.go

Copy path

File metadata and controls

142 lines (126 loc) · 3.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2024 Bryan Frimin <[email protected]>.
//
// Permission to use, copy, modify, and/or distribute this software
// for any purpose with or without fee is hereby granted, provided
// that the above copyright notice and this permission notice appear
// in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package httpserver
import (
"context"
"io"
stdlog "log"
"net"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.gearno.de/kit/internal/version"
"go.gearno.de/kit/log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type (
Option func(o *Options)
Options struct {
tracerProvider trace.TracerProvider
logger *log.Logger
registerer prometheus.Registerer
readTimeout time.Duration
writeTimeout time.Duration
}
)
// WithLogger is an option setter for specifying a logger for HTTP
// telemetry and error logging.
func WithLogger(l *log.Logger) Option {
return func(o *Options) {
o.logger = l.Named("http.server")
}
}
// WithTracerProvider configures OpenTelemetry tracing with the
// provided tracer provider.
func WithTracerProvider(tp trace.TracerProvider) Option {
return func(o *Options) {
o.tracerProvider = tp
}
}
// WithRegisterer sets a custom Prometheus registerer for metrics.
func WithRegisterer(r prometheus.Registerer) Option {
return func(o *Options) {
o.registerer = r
}
}
// WithReadTimeout overrides the default 30 s read timeout. Pass 0 to disable.
func WithReadTimeout(d time.Duration) Option {
return func(o *Options) {
o.readTimeout = d
}
}
// WithWriteTimeout overrides the default 30 s write timeout. Pass 0 to disable.
func WithWriteTimeout(d time.Duration) Option {
return func(o *Options) {
o.writeTimeout = d
}
}
func NewServer(addr string, h http.Handler, options ...Option) *http.Server {
opts := &Options{
logger: log.NewLogger(log.WithOutput(io.Discard)),
tracerProvider: otel.GetTracerProvider(),
registerer: prometheus.DefaultRegisterer,
readTimeout: 30 * time.Second,
writeTimeout: 30 * time.Second,
}
for _, o := range options {
o(opts)
}
logger := opts.logger.With(log.String("http_server_addr", addr))
handler := newHandlerWrapper(
h,
logger,
opts.tracerProvider,
opts.registerer,
)
tracer := opts.tracerProvider.Tracer(
"go.gearno.de/kit/httpserver",
trace.WithInstrumentationVersion(
version.New(0).Alpha(1),
),
)
connSpans := &sync.Map{}
return &http.Server{
Addr: addr,
Handler: handler,
ErrorLog: stdlog.New(logger.NewWriter(log.LevelError), "", 0),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: opts.readTimeout,
MaxHeaderBytes: maxHeaderBytes,
WriteTimeout: opts.writeTimeout,
IdleTimeout: 15 * time.Second,
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
ctx, span := tracer.Start(ctx, "http.connection",
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(
attribute.String("net.peer.addr", c.RemoteAddr().String()),
),
)
connSpans.Store(c, span)
return ctx
},
ConnState: func(c net.Conn, state http.ConnState) {
if state == http.StateClosed || state == http.StateHijacked {
if span, ok := connSpans.LoadAndDelete(c); ok {
span.(trace.Span).End()
}
}
},
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.