removed intermediate /internal directory
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"ruben/inventory2/domains/accounts"
|
||||
"ruben/inventory2/domains/authentication"
|
||||
etsy_platform "ruben/inventory2/domains/platforms/etsy"
|
||||
"ruben/inventory2/domains/raw_events"
|
||||
"ruben/inventory2/logging"
|
||||
"ruben/inventory2/server/api"
|
||||
"ruben/inventory2/server/auth"
|
||||
"ruben/inventory2/server/response"
|
||||
"ruben/inventory2/server/sse"
|
||||
"ruben/inventory2/server/ui"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
*gin.Engine
|
||||
sse *sse.Queue
|
||||
}
|
||||
|
||||
func NewRouter(
|
||||
logger *logging.Logger,
|
||||
contentDir string,
|
||||
rawEvents *raw_events.Store,
|
||||
accts *accounts.Store,
|
||||
etsy *etsy_platform.Platform,
|
||||
authr *authentication.Authenticator,
|
||||
) *Router {
|
||||
authM := auth.NewAuth(
|
||||
logger.WithGroup("auth-middleware"),
|
||||
authr,
|
||||
accts,
|
||||
)
|
||||
|
||||
r := gin.Default()
|
||||
r.Use(
|
||||
authM.Identify,
|
||||
response.HandleResponses,
|
||||
response.HandleErrors,
|
||||
)
|
||||
|
||||
// webpage content
|
||||
|
||||
// top level GET is assumed to be for the home page.
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.Redirect(http.StatusMovedPermanently, "/ui")
|
||||
})
|
||||
|
||||
ui.Routes(
|
||||
logger.WithGroup("ui"),
|
||||
r.Group("/ui"),
|
||||
"/ui",
|
||||
rawEvents,
|
||||
accts,
|
||||
etsy,
|
||||
authM.Authenticate(),
|
||||
)
|
||||
|
||||
// non-html content: scripts, styles, images, etc
|
||||
r.Use(fileServer("/scripts", contentDir+"/scripts", func(c *gin.Context) {
|
||||
w := c.Writer
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
if path.Ext(c.Request.URL.Path) == ".gz" {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
}))
|
||||
r.Static("/styles", "./styles")
|
||||
r.Static("/favicon", "./favicon")
|
||||
r.Static("/images", "./images")
|
||||
|
||||
// api endpoints
|
||||
|
||||
// sse setup
|
||||
sq := sse.NewQueue()
|
||||
unp := sq.NewUpdateNotificationPublisher(
|
||||
logger.WithGroup("update.notification.publisher"),
|
||||
func(c *gin.Context) int64 {
|
||||
return auth.GetIdentity(c).Account.AccountID
|
||||
},
|
||||
).Trim("/api")
|
||||
|
||||
api.Routes(
|
||||
r.Group("/api"),
|
||||
logger.WithGroup("/api"),
|
||||
authM,
|
||||
sq,
|
||||
accts,
|
||||
unp,
|
||||
rawEvents,
|
||||
etsy,
|
||||
)
|
||||
|
||||
return &Router{
|
||||
Engine: r,
|
||||
sse: sq,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) RunSSE(ctx context.Context) error {
|
||||
return r.sse.Start(ctx)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user