installed gin
This commit is contained in:
@@ -17,55 +17,74 @@ import (
|
||||
"ruben/inventory2/internal/logging"
|
||||
"ruben/inventory2/internal/server/middleware"
|
||||
"ruben/inventory2/internal/server/response"
|
||||
"ruben/inventory2/internal/server/router"
|
||||
|
||||
"github.com/angelbeltran/templater"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type webpageRouter struct {
|
||||
log *logging.Logger
|
||||
contentDir string
|
||||
templater *templater.Templater
|
||||
rawEvents *raw_events.Store
|
||||
accts *accounts.Store
|
||||
etsy *etsy_platform.Platform
|
||||
router.Subrouter
|
||||
}
|
||||
type (
|
||||
webpageRouter struct {
|
||||
log *logging.Logger
|
||||
contentDir string
|
||||
templater *templater.Templater
|
||||
rawEvents *raw_events.Store
|
||||
accts *accounts.Store
|
||||
etsy *etsy_platform.Platform
|
||||
authMiddleware *middleware.Auth
|
||||
}
|
||||
|
||||
func NewWebpageRouter(
|
||||
// ErrTemplateNotFound is returned if the reason the template failed to compile
|
||||
// is due to the template not being found.
|
||||
ErrTemplateNotFound struct {
|
||||
err error
|
||||
}
|
||||
)
|
||||
|
||||
func SetupRoutes(
|
||||
logger *logging.Logger,
|
||||
r gin.IRouter,
|
||||
contentDir string,
|
||||
tmpl *templater.Templater,
|
||||
rawEvents *raw_events.Store,
|
||||
accts *accounts.Store,
|
||||
etsy *etsy_platform.Platform,
|
||||
authMiddleware *middleware.Auth,
|
||||
) *webpageRouter {
|
||||
mux := router.NewSubMux(logger)
|
||||
|
||||
wr := &webpageRouter{
|
||||
log: logger,
|
||||
contentDir: contentDir,
|
||||
templater: tmpl,
|
||||
rawEvents: rawEvents,
|
||||
accts: accts,
|
||||
etsy: etsy,
|
||||
Subrouter: mux,
|
||||
) {
|
||||
s := &webpageRouter{
|
||||
log: logger,
|
||||
contentDir: contentDir,
|
||||
templater: tmpl,
|
||||
rawEvents: rawEvents,
|
||||
accts: accts,
|
||||
etsy: etsy,
|
||||
authMiddleware: authMiddleware,
|
||||
}
|
||||
|
||||
// non-authenticated
|
||||
mux.Handle("GET /{$}", authMiddleware.AddIdentity(wr.serveTemplates))
|
||||
fn2 := s.authMiddleware.AuthenticateAndAddIdentity(s.serveTemplates)
|
||||
|
||||
// authenticated
|
||||
mux.Handle("GET /", authMiddleware.AuthenticateAndAddIdentity(wr.serveTemplates))
|
||||
r.GET("/*rest", response.Handler(func(c *gin.Context) (response.Response, error) {
|
||||
c.Request.URL.Path = c.Request.URL.Path[3:]
|
||||
defer func() {
|
||||
c.Request.URL.Path = "/ui" + c.Request.URL.Path
|
||||
}()
|
||||
|
||||
return wr
|
||||
p := c.Request.URL.Path
|
||||
if p == "/" || p == "" {
|
||||
c, err := s.authMiddleware.AddIdentityToRequest(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.serveTemplates(c)
|
||||
}
|
||||
return fn2(c)
|
||||
}))
|
||||
}
|
||||
|
||||
// GET /
|
||||
// compiles the page template or component template matching the url
|
||||
// func (s *Server) serveTemplates(r *http.Request) (response.Response, error) {
|
||||
func (s *webpageRouter) serveTemplates(r *http.Request) (response.Response, error) {
|
||||
func (s *webpageRouter) serveTemplates(c *gin.Context) (response.Response, error) {
|
||||
r := c.Request
|
||||
name, args := s.getTemplateNameAndArgs(r, s.contentDir+"/templates/component_bodies")
|
||||
|
||||
b, err := s.templater.ExecuteComponentBody(name, args...)
|
||||
@@ -190,7 +209,9 @@ func getMatchingGlobPatternsCapturingFilepathIncludingParametrizedFilepaths(file
|
||||
func (s *webpageRouter) handleTemplateError(err error, templateArgs ...any) (response.Response, error) {
|
||||
if isFileNotFoundError(err) {
|
||||
return nil, response.NotFound().
|
||||
Wrap(err).
|
||||
Wrap(ErrTemplateNotFound{
|
||||
err: err,
|
||||
}).
|
||||
Msg("resource not found")
|
||||
}
|
||||
|
||||
@@ -280,3 +301,14 @@ func authorizeByMatchingAccountID(r *http.Request, acctIDPathPosition int) error
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e ErrTemplateNotFound) Error() string {
|
||||
if e.err != nil {
|
||||
return fmt.Sprintf("template not found: %v", e.err)
|
||||
}
|
||||
return fmt.Sprintf("template not found")
|
||||
}
|
||||
|
||||
func (e ErrTemplateNotFound) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user