mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
refactor server config
This commit is contained in:
parent
3dd36da114
commit
032df19805
4 changed files with 35 additions and 43 deletions
|
|
@ -9,7 +9,7 @@ Manage your holiday rental
|
|||
- [x] Read from the database
|
||||
- [x] Build the pdf invoice
|
||||
- [ ] Refactor the env variable calls to a Config struct with proper defaults
|
||||
- [ ] Refactor handlers to call their dependencies instead of taking them from the Server struct
|
||||
- [x] Refactor handlers to call their dependencies instead of taking them from the Server struct
|
||||
|
||||
## Built With
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
||||
func (s Server) MountHandlers() {
|
||||
// config
|
||||
s.Router.HideBanner = true
|
||||
s.Router.Debug = strings.ToLower(os.Getenv("DEBUG")) == "true"
|
||||
s.Router.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||
Format: "${time_rfc3339} [${method}: ${status}] ${uri}; ip=${remote_ip}; ${latency_human}; ${user_agent}\n",
|
||||
}))
|
||||
s.Router.HTTPErrorHandler = s.customHTTPErrorHandler
|
||||
// middlewares
|
||||
s.Router.Use(middleware.Recover())
|
||||
s.Router.Use(middleware.Secure())
|
||||
// static assets
|
||||
s.Router.Static("/static", "assets")
|
||||
// landing page
|
||||
s.Router.GET("/", handleHomePage())
|
||||
s.Router.GET("/bookings", handleListBookingPage(s.bs))
|
||||
|
|
|
|||
|
|
@ -7,10 +7,12 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/rjNemo/rentease/internal/booking"
|
||||
|
|
@ -26,12 +28,14 @@ type Server struct {
|
|||
}
|
||||
|
||||
func New(bs *booking.Service, ps *pdf.PdfService) *Server {
|
||||
return &Server{
|
||||
Router: echo.New(),
|
||||
s := &Server{
|
||||
Router: NewRouter(),
|
||||
bs: bs,
|
||||
ps: ps,
|
||||
addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")),
|
||||
}
|
||||
s.MountHandlers()
|
||||
return s
|
||||
}
|
||||
|
||||
func (s Server) Start() {
|
||||
|
|
@ -62,27 +66,36 @@ func renderTempl(c echo.Context, status int, t templ.Component) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s Server) renderTempl(c echo.Context, status int, t templ.Component) error {
|
||||
c.Response().Writer.WriteHeader(status)
|
||||
func NewRouter() *echo.Echo {
|
||||
e := echo.New()
|
||||
// config
|
||||
e.HideBanner = true
|
||||
e.Debug = strings.ToLower(os.Getenv("DEBUG")) == "true"
|
||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
||||
Format: "${time_rfc3339} [${method}: ${status}] ${uri}; ip=${remote_ip}; ${latency_human}; ${user_agent}\n",
|
||||
}))
|
||||
e.HTTPErrorHandler = customHTTPErrorHandler(e)
|
||||
// middlewares
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.Secure())
|
||||
// static assets
|
||||
e.Static("/static", "assets")
|
||||
|
||||
err := t.Render(context.Background(), c.Response().Writer)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "failed to render response template")
|
||||
}
|
||||
|
||||
return nil
|
||||
return e
|
||||
}
|
||||
|
||||
func (s Server) customHTTPErrorHandler(err error, c echo.Context) {
|
||||
code := http.StatusInternalServerError
|
||||
var he *echo.HTTPError
|
||||
if errors.As(err, &he) {
|
||||
code = he.Code
|
||||
}
|
||||
s.Router.Logger.Error(err)
|
||||
func customHTTPErrorHandler(e *echo.Echo) echo.HTTPErrorHandler {
|
||||
return func(err error, c echo.Context) {
|
||||
code := http.StatusInternalServerError
|
||||
var he *echo.HTTPError
|
||||
if errors.As(err, &he) {
|
||||
code = he.Code
|
||||
}
|
||||
e.Logger.Error(err)
|
||||
|
||||
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
||||
if err := c.File(errorPage); err != nil {
|
||||
c.Logger().Error(err)
|
||||
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
||||
if err := c.File(errorPage); err != nil {
|
||||
c.Logger().Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
4
main.go
4
main.go
|
|
@ -33,7 +33,5 @@ func main() {
|
|||
log.Fatalf("error migrating the database %s\n", err)
|
||||
}
|
||||
|
||||
s := server.New(booking.NewService(db), pdf.NewPdfService())
|
||||
s.MountHandlers()
|
||||
s.Start()
|
||||
server.New(booking.NewService(db), pdf.NewPdfService()).Start()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue