2021-09-29 15:38:50 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fasthttp/router"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
2023-02-23 17:49:45 +00:00
|
|
|
|
|
|
|
"gh.tarampamp.am/error-pages/internal/checkers"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/config"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/http/common"
|
|
|
|
errorpageHandler "gh.tarampamp.am/error-pages/internal/http/handlers/errorpage"
|
|
|
|
healthzHandler "gh.tarampamp.am/error-pages/internal/http/handlers/healthz"
|
|
|
|
indexHandler "gh.tarampamp.am/error-pages/internal/http/handlers/index"
|
|
|
|
metricsHandler "gh.tarampamp.am/error-pages/internal/http/handlers/metrics"
|
|
|
|
notfoundHandler "gh.tarampamp.am/error-pages/internal/http/handlers/notfound"
|
|
|
|
versionHandler "gh.tarampamp.am/error-pages/internal/http/handlers/version"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/metrics"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/options"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/tpl"
|
|
|
|
"gh.tarampamp.am/error-pages/internal/version"
|
2021-09-29 15:38:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-04-12 10:34:35 +00:00
|
|
|
func (s *Server) Register(cfg *config.Config, templatePicker templatePicker, opt options.ErrorPage) error {
|
2022-01-28 15:42:08 +00:00
|
|
|
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-04-12 10:34:35 +00:00
|
|
|
s.router.GET("/", indexHandler.NewHandler(cfg, templatePicker, s.rdr, opt))
|
|
|
|
s.router.GET("/{code}.html", errorpageHandler.NewHandler(cfg, templatePicker, s.rdr, opt))
|
|
|
|
|
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))
|
|
|
|
|
2023-04-07 10:42:00 +00:00
|
|
|
s.router.NotFound = notfoundHandler.NewHandler(cfg, templatePicker, s.rdr, opt)
|
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()
|
|
|
|
}
|