Files
logistics_support_tool/backend/main.go

93 lines
2.4 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
// 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)")
}
}
// 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()
// // --- Add Redirects Here ---
// // Redirect root ("/") to "/app" or "/lst/app"
// r.GET("/", func(c *gin.Context) {
// c.Redirect(http.StatusMovedPermanently, basePath+"/app")
// })
// // 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(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 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"})
}