refactor(backend): changes to convert the backend to strictly the app
This commit is contained in:
@@ -4,39 +4,89 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
// Load .env only in dev (not Docker/production)
|
||||
if os.Getenv("RUNNING_IN_DOCKER") != "true" {
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
log.Println("Warning: .env file not found (ok in Docker/production)")
|
||||
}
|
||||
}
|
||||
|
||||
// name := os.Getenv("LSTV2")
|
||||
// Set basePath dynamically
|
||||
basePath := "/"
|
||||
if os.Getenv("APP_ENV") != "production" {
|
||||
basePath = "/lst" // Dev only
|
||||
}
|
||||
|
||||
// fmt.Println(name)
|
||||
fmt.Println("Welcome to lst backend where all the fun happens.")
|
||||
r := gin.Default()
|
||||
|
||||
// Serve Docusaurus static files
|
||||
r.StaticFS("/lst/docs", http.Dir("docs"))
|
||||
// // --- Add Redirects Here ---
|
||||
// // Redirect root ("/") to "/app" or "/lst/app"
|
||||
// r.GET("/", func(c *gin.Context) {
|
||||
// c.Redirect(http.StatusMovedPermanently, basePath+"/app")
|
||||
// })
|
||||
|
||||
r.GET("/api/ping", func(c *gin.Context) {
|
||||
// // Redirect "/lst" (if applicable) to "/lst/app"
|
||||
// if basePath == "/lst" {
|
||||
// r.GET("/lst", func(c *gin.Context) {
|
||||
// c.Redirect(http.StatusMovedPermanently, basePath+"/app")
|
||||
// })
|
||||
// }
|
||||
|
||||
// Serve Docusaurus static files
|
||||
r.StaticFS(basePath+"/docs", http.Dir("docs"))
|
||||
r.StaticFS(basePath+"/app", http.Dir("frontend"))
|
||||
|
||||
r.GET(basePath+"/api/ping", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.Any("/api", errorApiLoc)
|
||||
r.Any("/", errorLoc)
|
||||
r.Any(basePath+"/api", errorApiLoc)
|
||||
|
||||
// // Serve static assets for Vite app
|
||||
// r.Static("/lst/app/assets", "./dist/app/assets")
|
||||
|
||||
// // Catch-all for Vite app routes
|
||||
// r.NoRoute(func(c *gin.Context) {
|
||||
// path := c.Request.URL.Path
|
||||
|
||||
// // Don't handle API, assets, or docs
|
||||
// if strings.HasPrefix(path, "/lst/api") ||
|
||||
// strings.HasPrefix(path, "/lst/app/assets") ||
|
||||
// strings.HasPrefix(path, "/lst/docs") {
|
||||
// c.JSON(404, gin.H{"error": "Not found"})
|
||||
// return
|
||||
// }
|
||||
|
||||
// // Serve index.html for all /lst/app routes
|
||||
// if strings.HasPrefix(path, "/lst/app") {
|
||||
// c.File("./dist/app/index.html")
|
||||
// return
|
||||
// }
|
||||
|
||||
// c.JSON(404, gin.H{"error": "Not found"})
|
||||
// })
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
func errorLoc(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "welcome to lst system you might have just encountered an incorrect area of the app"})
|
||||
}
|
||||
// func serveViteApp(c *gin.Context) {
|
||||
// // Set proper Content-Type for HTML
|
||||
// c.Header("Content-Type", "text/html")
|
||||
// c.File("./dist/index.html")
|
||||
// }
|
||||
|
||||
// func errorLoc(c *gin.Context) {
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"message": "welcome to lst system you might have just encountered an incorrect area of the app"})
|
||||
// }
|
||||
func errorApiLoc(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user