refactor(backend): changes to convert the backend to strictly the app

This commit is contained in:
2025-07-19 11:37:13 -05:00
parent 5cc1fbe919
commit 1e649d1f23
3 changed files with 73 additions and 15 deletions

2
backend/.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
lst_backend.exe
lst.net.exe

View File

@@ -3,19 +3,25 @@ FROM golang:1.24.4-alpine3.22 AS builder
WORKDIR /app WORKDIR /app
COPY go.mod go.sum ./ COPY go.mod go.sum ./
# COPY VERSION ./VERSION COPY docs /app/docs/
COPY frontend /app/frontend/
RUN go mod download RUN go mod download
COPY . . COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o lst_go ./main.go RUN CGO_ENABLED=0 GOOS=linux go build -o lst_go ./main.go
FROM alpine:latest FROM alpine:latest
WORKDIR /root/ WORKDIR /root/
# Copy only the binary (no need for source files)
RUN mkdir -p ./docs ./frontend
COPY --from=builder /app/lst_go . COPY --from=builder /app/lst_go .
# COPY --from=builder /app/VERSION ./ COPY --from=builder /app/docs ./docs/
COPY --from=builder /app/frontend ./frontend/
# create the volume paths # create the volume paths
RUN mkdir -p /data RUN mkdir -p /data

View File

@@ -4,39 +4,89 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
func main() { func main() {
// Load .env only in dev (not Docker/production)
if os.Getenv("RUNNING_IN_DOCKER") != "true" {
err := godotenv.Load("../.env") err := godotenv.Load("../.env")
if err != nil { if err != nil {
log.Fatal("Error loading .env file") 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(name)
fmt.Println("Welcome to lst backend where all the fun happens.") fmt.Println("Welcome to lst backend where all the fun happens.")
r := gin.Default() r := gin.Default()
// Serve Docusaurus static files // // --- Add Redirects Here ---
r.StaticFS("/lst/docs", http.Dir("docs")) // // 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"}) c.JSON(200, gin.H{"message": "pong"})
}) })
r.Any("/api", errorApiLoc) r.Any(basePath+"/api", errorApiLoc)
r.Any("/", errorLoc)
// // 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") r.Run(":8080")
} }
func errorLoc(c *gin.Context) { // func serveViteApp(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"}) // // 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) { func errorApiLoc(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"}) c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"})
} }