this repo has no description
1package server
2
3import (
4 "log/slog"
5 "net/http"
6
7 "github.com/eagleusb/proxycon/internal/data"
8 "github.com/eagleusb/proxycon/internal/handlers"
9 "github.com/eagleusb/proxycon/internal/utility"
10)
11
12// Server holds the HTTP server configuration.
13type Server struct {
14 handler http.Handler
15}
16
17// NewServer returns a configured HTTP server with all routes registered.
18func NewServer(p *data.BitcoinPrices, pageSize int) *Server {
19 mux := http.NewServeMux()
20 handlers.Register(mux, p, pageSize)
21 return &Server{
22 handler: utility.Logger(mux),
23 }
24}
25
26// Listen starts the HTTP server with HTTP/1 and HTTP/2 support.
27func (s *Server) Listen(addr string) error {
28 srv := &http.Server{
29 Addr: addr,
30 Handler: s.handler,
31 Protocols: &http.Protocols{},
32 }
33
34 srv.Protocols.SetHTTP1(true)
35 srv.Protocols.SetHTTP2(true)
36 srv.Protocols.SetUnencryptedHTTP2(true)
37
38 slog.Debug("http server protocol", "http1", srv.Protocols.HTTP1(), "http2", srv.Protocols.HTTP2(), "unencrypted", srv.Protocols.UnencryptedHTTP2())
39
40 if err := srv.ListenAndServe(); err != nil {
41 return err
42 }
43
44 return nil
45}