-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (119 loc) · 3.34 KB
/
main.go
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
// Package main provides a Discord bot that reads msgs from discord channels and stores them in PebbleDB
// which are then sent to a specified Discord channel. It includes functionality to handle
// message content and attachments, and ensures proper error handling and resource cleanup.
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"os"
"time"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
var numberOfHTTPRequests = prometheus.NewCounter(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of HTTP requests",
})
func init() {
prometheus.MustRegister(numberOfHTTPRequests)
}
func main() {
log := logr.FromSlogHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
}))
conf := struct {
addr string
metricsAddr string
pprofAddr string
enablePprof bool
}{
addr: ":3311",
metricsAddr: ":3001",
pprofAddr: ":6060",
enablePprof: true,
}
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
EnvVars: []string{"ADDR"},
Destination: &conf.addr,
Value: conf.addr,
},
&cli.StringFlag{
Name: "metrics-addr",
EnvVars: []string{"METRICS_ADDR"},
Destination: &conf.metricsAddr,
Value: conf.metricsAddr,
},
&cli.StringFlag{
Name: "pprof-addr",
EnvVars: []string{"PPROF_ADDR"},
Destination: &conf.pprofAddr,
Value: conf.pprofAddr,
},
&cli.BoolFlag{
Name: "enable-pprof",
EnvVars: []string{"ENABLE_PPROF"},
Destination: &conf.enablePprof,
Value: conf.enablePprof,
},
},
Action: func(c *cli.Context) error {
eg, ctx := errgroup.WithContext(c.Context)
if conf.enablePprof {
eg.Go(func() error {
return runHTTP(ctx, log, conf.pprofAddr, "pprof", http.DefaultServeMux)
})
}
eg.Go(func() error {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
return runHTTP(ctx, log, conf.metricsAddr, "metrics", mux)
})
eg.Go(func() error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
numberOfHTTPRequests.Inc()
_, _ = w.Write([]byte("hello world"))
})
return runHTTP(ctx, log, conf.addr, "app", mux)
})
return eg.Wait()
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err, "run")
os.Exit(1)
}
}
func runHTTP(ctx context.Context, log logr.Logger, addr, name string, handler http.Handler) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("could not listen for %s on address %w", name, err)
}
s := &http.Server{
Handler: handler,
}
go func() {
<-ctx.Done()
shutdownContext, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
log.Info(fmt.Sprintf("initiated a graceful shutdown of the %s server", name))
err := s.Shutdown(shutdownContext)
if errors.Is(err, context.DeadlineExceeded) {
log.Info(fmt.Sprintf("%s server terminated abruptly, necessitating a forced closure.", name))
s.Close() // nolint: errcheck
}
}()
log.Info(fmt.Sprintf("%s server is up and running", name), "addr", l.Addr().String())
return s.Serve(l)
}