mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-12 13:46:51 +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] Read from the database
|
||||||
- [x] Build the pdf invoice
|
- [x] Build the pdf invoice
|
||||||
- [ ] Refactor the env variable calls to a Config struct with proper defaults
|
- [ ] 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
|
## Built With
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,6 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4/middleware"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s Server) MountHandlers() {
|
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
|
// landing page
|
||||||
s.Router.GET("/", handleHomePage())
|
s.Router.GET("/", handleHomePage())
|
||||||
s.Router.GET("/bookings", handleListBookingPage(s.bs))
|
s.Router.GET("/bookings", handleListBookingPage(s.bs))
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/echo/v4/middleware"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/rjNemo/rentease/internal/booking"
|
"github.com/rjNemo/rentease/internal/booking"
|
||||||
|
|
@ -26,12 +28,14 @@ type Server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(bs *booking.Service, ps *pdf.PdfService) *Server {
|
func New(bs *booking.Service, ps *pdf.PdfService) *Server {
|
||||||
return &Server{
|
s := &Server{
|
||||||
Router: echo.New(),
|
Router: NewRouter(),
|
||||||
bs: bs,
|
bs: bs,
|
||||||
ps: ps,
|
ps: ps,
|
||||||
addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")),
|
addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")),
|
||||||
}
|
}
|
||||||
|
s.MountHandlers()
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) Start() {
|
func (s Server) Start() {
|
||||||
|
|
@ -62,27 +66,36 @@ func renderTempl(c echo.Context, status int, t templ.Component) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) renderTempl(c echo.Context, status int, t templ.Component) error {
|
func NewRouter() *echo.Echo {
|
||||||
c.Response().Writer.WriteHeader(status)
|
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)
|
return e
|
||||||
if err != nil {
|
|
||||||
return c.String(http.StatusInternalServerError, "failed to render response template")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Server) customHTTPErrorHandler(err error, c echo.Context) {
|
func customHTTPErrorHandler(e *echo.Echo) echo.HTTPErrorHandler {
|
||||||
code := http.StatusInternalServerError
|
return func(err error, c echo.Context) {
|
||||||
var he *echo.HTTPError
|
code := http.StatusInternalServerError
|
||||||
if errors.As(err, &he) {
|
var he *echo.HTTPError
|
||||||
code = he.Code
|
if errors.As(err, &he) {
|
||||||
}
|
code = he.Code
|
||||||
s.Router.Logger.Error(err)
|
}
|
||||||
|
e.Logger.Error(err)
|
||||||
|
|
||||||
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
errorPage := fmt.Sprintf("assets/html/HTTP%d.html", code)
|
||||||
if err := c.File(errorPage); err != nil {
|
if err := c.File(errorPage); err != nil {
|
||||||
c.Logger().Error(err)
|
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)
|
log.Fatalf("error migrating the database %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := server.New(booking.NewService(db), pdf.NewPdfService())
|
server.New(booking.NewService(db), pdf.NewPdfService()).Start()
|
||||||
s.MountHandlers()
|
|
||||||
s.Start()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue