2021-09-29 15:38:50 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/tarampampam/error-pages/internal/checkers"
|
2022-01-27 12:29:49 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/config"
|
2021-09-29 15:38:50 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/http/common"
|
|
|
|
errorpageHandler "github.com/tarampampam/error-pages/internal/http/handlers/errorpage"
|
|
|
|
healthzHandler "github.com/tarampampam/error-pages/internal/http/handlers/healthz"
|
2021-10-06 17:38:00 +00:00
|
|
|
indexHandler "github.com/tarampampam/error-pages/internal/http/handlers/index"
|
2022-01-28 15:42:08 +00:00
|
|
|
metricsHandler "github.com/tarampampam/error-pages/internal/http/handlers/metrics"
|
2021-10-06 17:38:00 +00:00
|
|
|
notfoundHandler "github.com/tarampampam/error-pages/internal/http/handlers/notfound"
|
2021-09-29 15:38:50 +00:00
|
|
|
versionHandler "github.com/tarampampam/error-pages/internal/http/handlers/version"
|
2022-01-28 15:42:08 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/metrics"
|
2022-01-31 08:44:21 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/tpl"
|
2021-09-29 15:38:50 +00:00
|
|
|
"github.com/tarampampam/error-pages/internal/version"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2022-01-28 15:42:08 +00:00
|
|
|
log *zap.Logger
|
2021-09-29 15:38:50 +00:00
|
|
|
fast *fasthttp.Server
|
|
|
|
router *router.Router
|
2022-01-31 08:43:40 +00:00
|
|
|
rdr *tpl.TemplateRenderer
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-10-06 17:38:00 +00:00
|
|
|
defaultWriteTimeout = time.Second * 4
|
|
|
|
defaultReadTimeout = time.Second * 4
|
|
|
|
defaultIdleTimeout = time.Second * 6
|
2021-09-29 15:38:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewServer(log *zap.Logger) Server {
|
2022-01-31 08:43:40 +00:00
|
|
|
rdr := tpl.NewTemplateRenderer()
|
|
|
|
|
2021-09-29 15:38:50 +00:00
|
|
|
return Server{
|
|
|
|
// fasthttp docs: <https://github.com/valyala/fasthttp>
|
|
|
|
fast: &fasthttp.Server{
|
|
|
|
WriteTimeout: defaultWriteTimeout,
|
|
|
|
ReadTimeout: defaultReadTimeout,
|
|
|
|
IdleTimeout: defaultIdleTimeout,
|
|
|
|
NoDefaultServerHeader: true,
|
|
|
|
ReduceMemoryUsage: true,
|
|
|
|
CloseOnShutdown: true,
|
|
|
|
Logger: zap.NewStdLog(log),
|
|
|
|
},
|
2022-01-28 15:42:08 +00:00
|
|
|
router: router.New(),
|
|
|
|
log: log,
|
2022-01-31 08:43:40 +00:00
|
|
|
rdr: rdr,
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start server.
|
|
|
|
func (s *Server) Start(ip string, port uint16) error {
|
|
|
|
return s.fast.ListenAndServe(ip + ":" + strconv.Itoa(int(port)))
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:29:49 +00:00
|
|
|
type templatePicker interface {
|
|
|
|
// Pick the template name for responding.
|
|
|
|
Pick() string
|
|
|
|
}
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2021-10-06 17:38:00 +00:00
|
|
|
// Register server routes, middlewares, etc.
|
|
|
|
// Router docs: <https://github.com/fasthttp/router>
|
2022-01-03 16:51:30 +00:00
|
|
|
func (s *Server) Register(
|
2022-01-27 12:29:49 +00:00
|
|
|
cfg *config.Config,
|
2022-01-03 16:51:30 +00:00
|
|
|
templatePicker templatePicker,
|
|
|
|
defaultPageCode string,
|
|
|
|
defaultHTTPCode uint16,
|
2022-01-27 12:29:49 +00:00
|
|
|
showDetails bool,
|
2022-02-23 06:09:54 +00:00
|
|
|
proxyHTTPHeaders []string,
|
2022-01-28 15:42:08 +00:00
|
|
|
) error {
|
|
|
|
reg, m := metrics.NewRegistry(), metrics.NewMetrics()
|
|
|
|
|
|
|
|
if err := m.Register(reg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.fast.Handler = common.DurationMetrics(common.LogRequest(s.router.Handler, s.log), &m)
|
|
|
|
|
2022-02-23 06:09:54 +00:00
|
|
|
s.router.GET("/", indexHandler.NewHandler(cfg, templatePicker, s.rdr, defaultPageCode, defaultHTTPCode, showDetails, proxyHTTPHeaders)) //nolint:lll
|
|
|
|
s.router.GET("/{code}.html", errorpageHandler.NewHandler(cfg, templatePicker, s.rdr, showDetails, proxyHTTPHeaders)) //nolint:lll
|
2021-09-29 15:38:50 +00:00
|
|
|
s.router.GET("/version", versionHandler.NewHandler(version.Version()))
|
2022-01-27 12:29:49 +00:00
|
|
|
|
|
|
|
liveHandler := healthzHandler.NewHandler(checkers.NewLiveChecker())
|
|
|
|
s.router.ANY("/healthz", liveHandler)
|
|
|
|
s.router.ANY("/health/live", liveHandler) // deprecated
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2022-01-28 15:42:08 +00:00
|
|
|
s.router.GET("/metrics", metricsHandler.NewHandler(reg))
|
|
|
|
|
2021-10-06 17:38:00 +00:00
|
|
|
s.router.NotFound = notfoundHandler.NewHandler()
|
2022-01-28 15:42:08 +00:00
|
|
|
|
|
|
|
return nil
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop server.
|
2022-01-31 08:43:40 +00:00
|
|
|
func (s *Server) Stop() error {
|
|
|
|
if err := s.rdr.Close(); err != nil {
|
|
|
|
defer func() { _ = s.fast.Shutdown() }()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.fast.Shutdown()
|
|
|
|
}
|