()
| 40 | ) |
| 41 | |
| 42 | func (c *Component) initWeb() error { |
| 43 | webOptions := []web.Option{ |
| 44 | web.WithContextFiller(c.FillContext), |
| 45 | web.WithTrustedProxies(c.config.HTTP.TrustedProxies...), |
| 46 | web.WithCookieKeys(c.config.HTTP.Cookie.HashKey, c.config.HTTP.Cookie.BlockKey), |
| 47 | web.WithStatic(c.config.HTTP.Static.Mount, c.config.HTTP.Static.SearchPath...), |
| 48 | web.WithLogIgnorePaths(c.config.HTTP.LogIgnorePaths), |
| 49 | } |
| 50 | if c.config.HTTP.RedirectToHost != "" { |
| 51 | webOptions = append(webOptions, web.WithRedirectToHost(c.config.HTTP.RedirectToHost)) |
| 52 | } |
| 53 | if c.config.HTTP.RedirectToHTTPS { |
| 54 | httpAddr, err := net.ResolveTCPAddr("tcp", c.config.HTTP.Listen) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | httpsAddr, err := net.ResolveTCPAddr("tcp", c.config.HTTP.ListenTLS) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | if httpsAddr.Port == 0 { |
| 63 | httpsAddr.Port = 443 |
| 64 | } |
| 65 | webOptions = append(webOptions, web.WithRedirectToHTTPS(httpAddr.Port, httpsAddr.Port)) |
| 66 | if httpAddr.Port != 80 && httpsAddr.Port != 443 { |
| 67 | webOptions = append(webOptions, web.WithRedirectToHTTPS(80, 443)) |
| 68 | } |
| 69 | } |
| 70 | web, err := web.New(c.ctx, webOptions...) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | if c.config.HTTP.PProf.Enable { |
| 76 | g := web.RootRouter().NewRoute().Subrouter() |
| 77 | g.Use(ratelimit.HTTPMiddleware(c.RateLimiter(), "http:pprof")) |
| 78 | if c.config.HTTP.PProf.Password != "" { |
| 79 | g.Use(mux.MiddlewareFunc(webmiddleware.BasicAuth( |
| 80 | "pprof", |
| 81 | webmiddleware.AuthUser(pprofUsername, c.config.HTTP.PProf.Password), |
| 82 | ))) |
| 83 | } |
| 84 | g.HandleFunc("/debug/pprof/profile", pprof.Profile) |
| 85 | g.HandleFunc("/debug/pprof/trace", pprof.Trace) |
| 86 | g.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index) |
| 87 | g.Handle("/debug/pprof", http.RedirectHandler("/debug/pprof/", http.StatusMovedPermanently)) |
| 88 | } |
| 89 | |
| 90 | if c.config.HTTP.Metrics.Enable { |
| 91 | g := web.RootRouter().NewRoute().Subrouter() |
| 92 | g.Use(ratelimit.HTTPMiddleware(c.RateLimiter(), "http:metrics")) |
| 93 | if c.config.HTTP.Metrics.Password != "" { |
| 94 | g.Use(mux.MiddlewareFunc(webmiddleware.BasicAuth( |
| 95 | "metrics", |
| 96 | webmiddleware.AuthUser(metricsUsername, c.config.HTTP.Metrics.Password), |
| 97 | ))) |
| 98 | } |
| 99 | g.Handle("/metrics", metrics.Exporter) |
no test coverage detected