error-pages/internal/http/server.go

106 lines
3.0 KiB
Go
Raw Normal View History

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"
"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
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 {
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,
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)))
}
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>
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)
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()))
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))
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.
func (s *Server) Stop() error {
if err := s.rdr.Close(); err != nil {
defer func() { _ = s.fast.Shutdown() }()
return err
}
return s.fast.Shutdown()
}