2022-08-22 18:51:46 -06:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2023-08-28 17:23:29 -06:00
|
|
|
"embed"
|
2022-08-22 18:51:46 -06:00
|
|
|
"fmt"
|
2023-08-27 18:09:46 -06:00
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/config"
|
2023-09-09 09:24:25 -06:00
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/event_bus"
|
2023-09-20 14:57:12 -06:00
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/models"
|
2023-08-27 18:09:46 -06:00
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/web/handler"
|
|
|
|
"github.com/fastenhealth/fasten-onprem/backend/pkg/web/middleware"
|
2022-08-22 18:51:46 -06:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
2023-09-13 14:55:44 -06:00
|
|
|
"runtime"
|
2022-08-22 18:51:46 -06:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppEngine struct {
|
2023-09-20 14:57:12 -06:00
|
|
|
Config config.Interface
|
|
|
|
Logger *logrus.Entry
|
|
|
|
EventBus event_bus.Interface
|
2022-08-22 18:51:46 -06:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:23:29 -06:00
|
|
|
func (ae *AppEngine) Setup() (*gin.RouterGroup, *gin.Engine) {
|
2022-08-22 18:51:46 -06:00
|
|
|
r := gin.New()
|
|
|
|
|
2023-08-28 17:23:29 -06:00
|
|
|
r.Use(middleware.LoggerMiddleware(ae.Logger))
|
2023-09-20 14:57:12 -06:00
|
|
|
r.Use(middleware.RepositoryMiddleware(ae.Config, ae.Logger, ae.EventBus))
|
2022-08-22 18:51:46 -06:00
|
|
|
r.Use(middleware.ConfigMiddleware(ae.Config))
|
2023-09-20 14:57:12 -06:00
|
|
|
r.Use(middleware.EventBusMiddleware(ae.EventBus))
|
2022-08-22 18:51:46 -06:00
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
|
|
basePath := ae.Config.GetString("web.listen.basepath")
|
2023-08-28 17:23:29 -06:00
|
|
|
ae.Logger.Debugf("basepath: %s", basePath)
|
2022-08-22 18:51:46 -06:00
|
|
|
|
|
|
|
base := r.Group(basePath)
|
|
|
|
{
|
|
|
|
api := base.Group("/api")
|
|
|
|
{
|
2023-10-09 11:51:38 -06:00
|
|
|
api.Use(middleware.CacheMiddleware())
|
2022-08-22 18:51:46 -06:00
|
|
|
api.GET("/health", func(c *gin.Context) {
|
|
|
|
//TODO:
|
|
|
|
// check if the /web folder is populated.
|
|
|
|
// check if access to database
|
2023-09-07 22:54:25 -06:00
|
|
|
|
2023-09-20 14:57:12 -06:00
|
|
|
keepAliveMsg := models.NewEventKeepAlive("heartbeat")
|
|
|
|
err := ae.EventBus.PublishMessage(keepAliveMsg)
|
2023-09-07 22:54:25 -06:00
|
|
|
|
2022-08-22 18:51:46 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2023-09-20 14:57:12 -06:00
|
|
|
"success": err == nil,
|
2022-08-22 18:51:46 -06:00
|
|
|
})
|
|
|
|
})
|
2022-09-11 21:59:13 -06:00
|
|
|
|
|
|
|
api.POST("/auth/signup", handler.AuthSignup)
|
2022-11-02 01:12:54 -06:00
|
|
|
api.POST("/auth/signin", handler.AuthSignin)
|
2022-12-02 20:40:58 -07:00
|
|
|
//
|
|
|
|
//r.Any("/database/*proxyPath", handler.CouchDBProxy)
|
|
|
|
//r.GET("/cors/*proxyPath", handler.CORSProxy)
|
|
|
|
//r.OPTIONS("/cors/*proxyPath", handler.CORSProxy)
|
2023-03-21 09:04:43 -06:00
|
|
|
api.GET("/glossary/code", handler.GlossarySearchByCode)
|
2022-10-09 10:34:57 -06:00
|
|
|
|
2022-12-02 20:40:58 -07:00
|
|
|
secure := api.Group("/secure").Use(middleware.RequireAuth())
|
|
|
|
{
|
|
|
|
secure.GET("/summary", handler.GetSummary)
|
|
|
|
|
|
|
|
secure.POST("/source", handler.CreateSource)
|
|
|
|
secure.POST("/source/manual", handler.CreateManualSource)
|
|
|
|
secure.GET("/source", handler.ListSource)
|
|
|
|
secure.GET("/source/:sourceId", handler.GetSource)
|
|
|
|
secure.POST("/source/:sourceId/sync", handler.SourceSync)
|
|
|
|
secure.GET("/source/:sourceId/summary", handler.GetSourceSummary)
|
2022-12-21 20:51:02 -07:00
|
|
|
secure.GET("/resource/fhir", handler.ListResourceFhir)
|
2023-04-22 23:08:58 -06:00
|
|
|
secure.GET("/resource/graph/:graphType", handler.GetResourceFhirGraph)
|
2022-12-02 20:40:58 -07:00
|
|
|
secure.GET("/resource/fhir/:sourceId/:resourceId", handler.GetResourceFhir)
|
2023-01-10 20:23:47 -07:00
|
|
|
secure.POST("/resource/composition", handler.CreateResourceComposition)
|
2023-03-21 09:04:43 -06:00
|
|
|
|
2023-07-08 08:43:30 -06:00
|
|
|
secure.GET("/dashboards", handler.GetDashboard)
|
2023-08-24 18:07:06 -06:00
|
|
|
secure.POST("/dashboards", handler.AddDashboardLocation)
|
2023-07-08 08:43:30 -06:00
|
|
|
//secure.GET("/dashboard/:dashboardId", handler.GetDashboard)
|
|
|
|
|
2023-10-08 17:29:26 -06:00
|
|
|
secure.GET("/jobs", handler.ListBackgroundJobs)
|
|
|
|
|
2023-07-08 08:43:30 -06:00
|
|
|
secure.POST("/query", handler.QueryResourceFhir)
|
|
|
|
|
2023-09-13 14:55:44 -06:00
|
|
|
//server-side-events handler (only supported on mac/linux)
|
|
|
|
// TODO: causes deadlock on Windows
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
secure.GET("/events/stream",
|
|
|
|
middleware.SSEHeaderMiddleware(),
|
2023-09-20 14:57:12 -06:00
|
|
|
handler.SSEEventBusServerHandler(ae.EventBus),
|
2023-09-13 14:55:44 -06:00
|
|
|
)
|
|
|
|
}
|
2022-12-02 20:40:58 -07:00
|
|
|
}
|
|
|
|
|
2022-12-21 20:51:02 -07:00
|
|
|
if ae.Config.GetBool("web.allow_unsafe_endpoints") {
|
|
|
|
//this endpoint lets us request data directly from the source api
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningf("\"web.allow_unsafe_endpoints\" mode enabled!! This enables developer functionality, including unauthenticated raw api requests")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
ae.Logger.Warningln("***UNSAFE***")
|
|
|
|
unsafe := api.Group("/unsafe")
|
|
|
|
{
|
2023-05-05 10:06:33 -06:00
|
|
|
//http://localhost:9090/api/unsafe/testuser1/3508f8cf-6eb9-4e4b-8174-dd69a493a2b4/Patient/smart-1288992
|
2022-12-21 20:51:02 -07:00
|
|
|
unsafe.GET("/:username/:sourceId/*path", handler.UnsafeRequestSource)
|
2023-04-22 23:08:58 -06:00
|
|
|
unsafe.GET("/:username/graph/:graphType", handler.UnsafeResourceGraph)
|
2022-12-21 20:51:02 -07:00
|
|
|
|
|
|
|
}
|
2022-12-02 20:40:58 -07:00
|
|
|
}
|
2022-08-22 18:51:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:23:29 -06:00
|
|
|
return base, r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ae *AppEngine) SetupFrontendRouting(base *gin.RouterGroup, router *gin.Engine) *gin.Engine {
|
2022-08-22 18:51:46 -06:00
|
|
|
//Static request routing
|
|
|
|
base.StaticFS("/web", http.Dir(ae.Config.GetString("web.src.frontend.path")))
|
|
|
|
|
|
|
|
//redirect base url to /web
|
|
|
|
base.GET("/", func(c *gin.Context) {
|
2023-08-28 17:23:29 -06:00
|
|
|
c.Redirect(http.StatusFound, ae.Config.GetString("web.listen.basepath")+"/web")
|
2022-08-22 18:51:46 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
//catch-all, serve index page.
|
2023-08-28 17:23:29 -06:00
|
|
|
router.NoRoute(func(c *gin.Context) {
|
2022-09-14 20:59:16 -06:00
|
|
|
path := c.Request.URL.Path
|
2023-08-28 17:23:29 -06:00
|
|
|
if strings.HasPrefix(path, "/api") {
|
2022-09-14 20:59:16 -06:00
|
|
|
c.JSON(http.StatusNotFound, gin.H{"success": false, "error": "404 endpoint not found"})
|
|
|
|
} else {
|
|
|
|
c.File(fmt.Sprintf("%s/index.html", ae.Config.GetString("web.src.frontend.path")))
|
|
|
|
}
|
2022-08-22 18:51:46 -06:00
|
|
|
})
|
2023-08-28 17:23:29 -06:00
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ae *AppEngine) SetupEmbeddedFrontendRouting(embeddedAssetsFS embed.FS, base *gin.RouterGroup, router *gin.Engine) *gin.Engine {
|
|
|
|
//Static request routing
|
|
|
|
base.StaticFS("/web", http.FS(embeddedAssetsFS))
|
|
|
|
|
|
|
|
//redirect base url to /web
|
|
|
|
base.GET("/", func(c *gin.Context) {
|
|
|
|
c.Redirect(http.StatusFound, ae.Config.GetString("web.listen.basepath")+"/web")
|
|
|
|
})
|
|
|
|
|
|
|
|
//catch-all, serve index page.
|
|
|
|
router.NoRoute(func(c *gin.Context) {
|
|
|
|
path := c.Request.URL.Path
|
|
|
|
if strings.HasPrefix(path, "/api") {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"success": false, "error": "404 endpoint not found"})
|
|
|
|
} else {
|
|
|
|
ae.Logger.Infof("could not find %s, fallback to index.html", path)
|
|
|
|
c.FileFromFS("index.html", http.FS(embeddedAssetsFS))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return router
|
2022-08-22 18:51:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ae *AppEngine) Start() error {
|
|
|
|
//set the gin mode
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
if strings.ToLower(ae.Config.GetString("log.level")) == "debug" {
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:23:29 -06:00
|
|
|
baseRouterGroup, ginRouter := ae.Setup()
|
|
|
|
r := ae.SetupFrontendRouting(baseRouterGroup, ginRouter)
|
2022-08-22 18:51:46 -06:00
|
|
|
|
|
|
|
return r.Run(fmt.Sprintf("%s:%s", ae.Config.GetString("web.listen.host"), ae.Config.GetString("web.listen.port")))
|
|
|
|
}
|