server better organized
This commit is contained in:
@@ -47,7 +47,7 @@ func SetupRoutes(
|
|||||||
rawEvents *raw_events.Store,
|
rawEvents *raw_events.Store,
|
||||||
accts *accounts.Store,
|
accts *accounts.Store,
|
||||||
etsy *etsy_platform.Platform,
|
etsy *etsy_platform.Platform,
|
||||||
auth *middleware.Auth,
|
authenticate gin.HandlerFunc,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
s := &webpageRouter{
|
s := &webpageRouter{
|
||||||
@@ -98,7 +98,7 @@ func SetupRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.GET("", response.Handler(s.serveTemplate))
|
r.GET("", response.Handler(s.serveTemplate))
|
||||||
r.GET("/*rest", response.Handler(auth.Authenticate()), response.Handler(s.serveTemplate))
|
r.GET("/*rest", authenticate, response.Handler(s.serveTemplate))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /
|
// GET /
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func NewAuth(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIdentity will add an Identity to the context that can then be retrieved via GetIdentity.
|
// Identify will add an Identity to the context that can then be retrieved via GetIdentity.
|
||||||
func (a *Auth) AddIdentity(c *gin.Context) {
|
func (a *Auth) Identify(c *gin.Context) {
|
||||||
if err := a.addIdentity(c); err != nil {
|
if err := a.addIdentity(c); err != nil {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
@@ -98,9 +98,14 @@ func (a *Auth) addIdentity(c *gin.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Authenticate should only be used along with and after AddIdentity
|
// Authenticate should only be used along with and after Identify
|
||||||
// Typically used with response.Handler to make a gin.HandlerFunc.
|
// Typically used with response.Handler to make a gin.HandlerFunc.
|
||||||
func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) {
|
func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) {
|
||||||
|
return response.Handler(a.AuthenticateHandler(assertions...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// See Authenticate.
|
||||||
|
func (a *Auth) AuthenticateHandler(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) {
|
||||||
return func(c *gin.Context) (response.Response, error) {
|
return func(c *gin.Context) (response.Response, error) {
|
||||||
id, ok := getIdentity(c)
|
id, ok := getIdentity(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
+32
-51
@@ -39,26 +39,38 @@ func NewRouter(
|
|||||||
etsy *etsy_platform.Platform,
|
etsy *etsy_platform.Platform,
|
||||||
auth *authentication.Authenticator,
|
auth *authentication.Authenticator,
|
||||||
) *Router {
|
) *Router {
|
||||||
r := gin.Default()
|
authM := middleware.NewAuth(
|
||||||
|
logger.WithGroup("auth-middleware"),
|
||||||
|
auth,
|
||||||
|
accts,
|
||||||
|
)
|
||||||
|
|
||||||
|
r := gin.Default()
|
||||||
r.Use(
|
r.Use(
|
||||||
|
authM.Identify,
|
||||||
response.HandleResponses,
|
response.HandleResponses,
|
||||||
response.HandleErrors,
|
response.HandleErrors,
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
|
||||||
authM := middleware.NewAuth(
|
|
||||||
logger.WithGroup("auth-middleware"),
|
|
||||||
auth,
|
|
||||||
//auth_api.NewLoginURL,
|
|
||||||
accts,
|
|
||||||
)
|
|
||||||
|
|
||||||
// webpage content
|
// webpage content
|
||||||
|
|
||||||
// non-html content: scripts, styles, images, etc
|
// top level GET is assumed to be for the home page.
|
||||||
|
r.GET("/", func(c *gin.Context) {
|
||||||
|
c.Redirect(http.StatusMovedPermanently, "/ui")
|
||||||
|
})
|
||||||
|
|
||||||
|
templates_api.SetupRoutes(
|
||||||
|
logger.WithGroup("templates"),
|
||||||
|
r.Group("/ui"),
|
||||||
|
"/ui",
|
||||||
|
contentDir,
|
||||||
|
rawEvents,
|
||||||
|
accts,
|
||||||
|
etsy,
|
||||||
|
authM.Authenticate(),
|
||||||
|
)
|
||||||
|
|
||||||
|
// non-html content: scripts, styles, images, etc
|
||||||
r.Use(fileServer("/scripts", contentDir+"/scripts", func(c *gin.Context) {
|
r.Use(fileServer("/scripts", contentDir+"/scripts", func(c *gin.Context) {
|
||||||
w := c.Writer
|
w := c.Writer
|
||||||
w.Header().Set("Content-Type", "text/javascript")
|
w.Header().Set("Content-Type", "text/javascript")
|
||||||
@@ -70,40 +82,17 @@ func NewRouter(
|
|||||||
r.Static("/favicon", "./favicon")
|
r.Static("/favicon", "./favicon")
|
||||||
r.Static("/images", "./images")
|
r.Static("/images", "./images")
|
||||||
|
|
||||||
// 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
|
|
||||||
templates_api.SetupRoutes(
|
|
||||||
logger.WithGroup("templates"),
|
|
||||||
r.Group(
|
|
||||||
"/ui",
|
|
||||||
authM.AddIdentity,
|
|
||||||
),
|
|
||||||
"/ui",
|
|
||||||
contentDir,
|
|
||||||
rawEvents,
|
|
||||||
accts,
|
|
||||||
etsy,
|
|
||||||
authM,
|
|
||||||
)
|
|
||||||
|
|
||||||
// sse setup
|
|
||||||
|
|
||||||
sq := sse.NewQueue()
|
|
||||||
unp := middleware.NewUpdateNotificationPublisher(
|
|
||||||
logger.WithGroup("update.notification.publisher"),
|
|
||||||
sq,
|
|
||||||
)
|
|
||||||
|
|
||||||
// api endpoints
|
// api endpoints
|
||||||
|
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
apiLogger := logger.WithGroup("/api")
|
apiLogger := logger.WithGroup("/api")
|
||||||
unp = unp.Trim("/api")
|
|
||||||
|
// sse setup
|
||||||
|
sq := sse.NewQueue()
|
||||||
|
unp := middleware.NewUpdateNotificationPublisher(
|
||||||
|
logger.WithGroup("update.notification.publisher"),
|
||||||
|
sq,
|
||||||
|
).Trim("/api")
|
||||||
|
|
||||||
auth_api.Routes(
|
auth_api.Routes(
|
||||||
api.Group("/auth"),
|
api.Group("/auth"),
|
||||||
@@ -111,20 +100,12 @@ func NewRouter(
|
|||||||
auth,
|
auth,
|
||||||
)
|
)
|
||||||
sse_api.Routes(
|
sse_api.Routes(
|
||||||
api.Group(
|
api.Group("/events", authM.Authenticate()),
|
||||||
"/events",
|
|
||||||
authM.AddIdentity,
|
|
||||||
response.Handler(authM.Authenticate()),
|
|
||||||
),
|
|
||||||
apiLogger.WithGroup("/events"),
|
apiLogger.WithGroup("/events"),
|
||||||
sq,
|
sq,
|
||||||
)
|
)
|
||||||
accounts_api.Routes(
|
accounts_api.Routes(
|
||||||
api.Group(
|
api.Group("/accounts", authM.Authenticate()),
|
||||||
"/accounts",
|
|
||||||
authM.AddIdentity,
|
|
||||||
response.Handler(authM.Authenticate()),
|
|
||||||
),
|
|
||||||
apiLogger.WithGroup("/accounts"),
|
apiLogger.WithGroup("/accounts"),
|
||||||
accts,
|
accts,
|
||||||
unp.Group("/accounts"),
|
unp.Group("/accounts"),
|
||||||
|
|||||||
Reference in New Issue
Block a user