installed gin

This commit is contained in:
2026-01-13 22:07:20 -07:00
parent 851234d9fa
commit 7481cec0d5
29 changed files with 544 additions and 1094 deletions
+49 -49
View File
@@ -3,14 +3,15 @@ package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"path"
"strconv"
"strings"
"github.com/angelbeltran/templater"
"github.com/gin-gonic/gin"
"ruben/inventory2/internal/domains/accounts"
"ruben/inventory2/internal/domains/authentication"
@@ -23,11 +24,11 @@ import (
"ruben/inventory2/internal/server/api/webhooks"
etsy_webhooks "ruben/inventory2/internal/server/api/webhooks/etsy"
"ruben/inventory2/internal/server/middleware"
"ruben/inventory2/internal/server/response"
"ruben/inventory2/internal/server/router"
)
func NewServer(
// r gin.IRouter, // TODO: use this everywhere
func Router(
ctx context.Context,
logger *logging.Logger,
contentDir string,
@@ -35,11 +36,8 @@ func NewServer(
accts *accounts.Store,
etsy *etsy_platform.Platform,
auth *authentication.Authenticator,
) http.Handler {
mux := router.NewMux(
logger,
middleware.LogRequests(ctx, logger.WithGroup("request")),
)
) *gin.Engine {
r := gin.Default()
// TODO: shouldn't this ACTUALLY be a middleware?
// - only try to make this an actual middleware AFTER all the routers are broken out, so that way how the middleware is supposed to work can be known
@@ -54,22 +52,26 @@ func NewServer(
// non-html content: scripts, styles, images, etc
scfs := http.FileServer(http.Dir(contentDir + "/scripts"))
mux.Mux.Handle("GET /scripts/", http.StripPrefix("/scripts", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Use(fileServer("/scripts", contentDir+"/scripts", func(c *gin.Context) {
w := c.Writer
w.Header().Set("Content-Type", "text/javascript")
if path.Ext(r.URL.Path) == ".gz" {
if path.Ext(c.Request.URL.Path) == ".gz" {
w.Header().Set("Content-Encoding", "gzip")
}
scfs.ServeHTTP(w, r)
})))
mux.Mux.Handle("GET /styles/", http.StripPrefix("/styles", http.FileServer(http.Dir(contentDir+"/styles"))))
mux.Mux.Handle("GET /favicon/", http.StripPrefix("/favicon", http.FileServer(http.Dir(contentDir+"/favicon"))))
}))
r.Static("/styles", "./styles")
r.Static("/favicon", "./favicon")
// html (must be before the api endpoints, because the webpage middleware has to be installed beforehand
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/ui")
})
// kind of a dumb way to capture routes for webpages
wpr := templates_api.NewWebpageRouter(
templates_api.SetupRoutes(
logger.WithGroup("templates"),
r.Group("/ui"),
contentDir,
templater.NewTemplater(
contentDir+"/templates",
@@ -125,42 +127,27 @@ func NewServer(
etsy,
authMiddleware,
)
mux.AddMiddleware(
func(fn response.HandlerFunc) response.HandlerFunc {
return func(r *http.Request) (response.Response, error) {
res, err := fn(r)
if err == nil || !errors.Is(err, router.ErrHandlerNotFound) {
return res, err
}
renderWebPage, pathParams, found := wpr.Handler(r)
if !found {
return nil, response.NotFound()
}
for k, v := range pathParams {
r.SetPathValue(k, v)
}
return renderWebPage(r)
}
},
)
mux.Route("/", wpr)
// api endpoints
mux.Route("/auth", auth_api.NewLoginSubrouter(logger.WithGroup("/auth"), auth))
mux.Route("/accounts", accounts_api.NewAccountSubrouter(
api := r.Group("/api")
auth_api.Routes(
api.Group("/auth"),
logger.WithGroup("/auth"),
auth,
)
accounts_api.Routes(
api.Group("/accounts"),
logger.WithGroup("/accounts"),
accts,
authMiddleware,
))
)
// api webhooks (TODO: make a router for these)
webhookHandler := http.StripPrefix("/webhooks", webhooks.New(
webhooks.Webhooks(
api.Group("/webhooks"),
logger.WithGroup("webhooks"),
rawEvents,
etsy,
@@ -169,10 +156,23 @@ func NewServer(
OAuthRedirectURIWithAcctIDParam: "/oauth/account/{acctID}/auth_code",
},
},
))
mux.Mux.Handle("GET /webhooks/", webhookHandler)
mux.Mux.Handle("POST /webhooks/", webhookHandler)
mux.Mux.Handle("PUT /webhooks/", webhookHandler)
)
return mux
return r
}
func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.HandlerFunc {
scfs := http.StripPrefix(urlPrefix, http.FileServer(http.Dir(dir)))
return func(c *gin.Context) {
r := c.Request
if r.URL.Path == urlPrefix || strings.HasPrefix(r.URL.Path, path.Join(urlPrefix, "/")) {
if beforeServe != nil {
beforeServe(c)
}
scfs.ServeHTTP(c.Writer, r)
c.Abort()
}
// TODO: need a c.Next()?
}
}