go-project-template/httpserver/server.go
2024-05-05 15:10:32 -07:00

85 lines
1.8 KiB
Go

package httpserver
import (
"context"
"database/sql"
"errors"
"net/http"
"time"
echo "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"git.janky.solutions/finn/go-project-template/config"
)
var server *echo.Echo
func ListenAndServe() {
server = echo.New()
server.HideBanner = true
server.HidePort = true
server.HTTPErrorHandler = handleError
server.Renderer = &Template{}
server.Use(accessLogMiddleware)
server.RouteNotFound("/*", notFoundHandler)
server.StaticFS("/static", Static)
logrus.WithField("address", config.C.HTTPBind).Info("starting http server")
err := server.Start(config.C.HTTPBind)
if err != http.ErrServerClosed {
logrus.WithError(err).Fatal("error starting http server")
}
}
func Shutdown(ctx context.Context) error {
if server == nil {
return nil
}
return server.Shutdown(ctx)
}
func accessLogMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
start := time.Now()
err := next(c)
log := logrus.WithFields(logrus.Fields{
"method": c.Request().Method,
"path": c.Request().URL.Path,
"duration": time.Since(start),
"status": c.Response().Status,
"source": c.Request().RemoteAddr,
})
if err != nil {
log = log.WithError(err)
}
log.Debug("request handled")
return err
}
}
func handleError(err error, c echo.Context) {
if errors.Is(err, sql.ErrNoRows) {
logrus.Debug("sending 404 because handler threw sql.ErrNoRows")
err = notFoundHandler(c)
if err == nil {
return
}
}
logrus.WithFields(logrus.Fields{
"path": c.Request().URL.Path,
"method": c.Request().Method,
"error": err,
}).Error("error handling request")
_ = c.Render(http.StatusNotFound, "500.html", nil)
}
func notFoundHandler(c echo.Context) error {
return c.Render(http.StatusNotFound, "404.html", nil)
}