2022-08-22 18:51:46 -06:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-22 19:11:00 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/config"
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/web/handler"
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/web/middleware"
|
2022-08-22 18:51:46 -06:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AppEngine struct {
|
|
|
|
Config config.Interface
|
|
|
|
Logger *logrus.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine {
|
|
|
|
r := gin.New()
|
|
|
|
|
|
|
|
r.Use(middleware.LoggerMiddleware(logger))
|
2022-08-25 19:26:29 -06:00
|
|
|
r.Use(middleware.RepositoryMiddleware(ae.Config, logger))
|
2022-08-22 18:51:46 -06:00
|
|
|
r.Use(middleware.ConfigMiddleware(ae.Config))
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
|
|
basePath := ae.Config.GetString("web.listen.basepath")
|
|
|
|
logger.Debugf("basepath: %s", basePath)
|
|
|
|
|
|
|
|
base := r.Group(basePath)
|
|
|
|
{
|
|
|
|
api := base.Group("/api")
|
|
|
|
{
|
|
|
|
api.GET("/health", func(c *gin.Context) {
|
|
|
|
//TODO:
|
|
|
|
// check if the /web folder is populated.
|
|
|
|
// check if access to database
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"success": true,
|
|
|
|
})
|
|
|
|
})
|
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-10-09 10:34:57 -06:00
|
|
|
|
|
|
|
r.Any("/database/*proxyPath", handler.CouchDBProxy)
|
2022-10-12 19:47:12 -06:00
|
|
|
r.GET("/cors/*proxyPath", handler.CORSProxy)
|
|
|
|
r.OPTIONS("/cors/*proxyPath", handler.CORSProxy)
|
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) {
|
|
|
|
c.Redirect(http.StatusFound, basePath+"/web")
|
|
|
|
})
|
|
|
|
|
|
|
|
//catch-all, serve index page.
|
|
|
|
r.NoRoute(func(c *gin.Context) {
|
2022-09-14 20:59:16 -06:00
|
|
|
path := c.Request.URL.Path
|
2022-09-18 20:46:57 -06:00
|
|
|
if strings.HasPrefix(path, "/api") || 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
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := ae.Setup(ae.Logger)
|
|
|
|
|
|
|
|
return r.Run(fmt.Sprintf("%s:%s", ae.Config.GetString("web.listen.host"), ae.Config.GetString("web.listen.port")))
|
|
|
|
}
|