refactor(correction to folder sturcture): before we got to deep resturctures to best pactice folder
This commit is contained in:
110
backend/main.go
110
backend/main.go
@@ -1,50 +1,29 @@
|
||||
// @title My Awesome API
|
||||
// @version 1.0
|
||||
// @description This is a sample server for a pet store.
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
|
||||
// @contact.name API Support
|
||||
// @contact.url http://www.swagger.io/support
|
||||
// @contact.email support@swagger.io
|
||||
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
// @host localhost:8080
|
||||
// @BasePath /api/v1
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
"lst.net/cmd/services/system/settings"
|
||||
"lst.net/cmd/services/websocket"
|
||||
|
||||
// _ "lst.net/docs"
|
||||
|
||||
"lst.net/utils/db"
|
||||
logging "lst.net/utils/logger"
|
||||
"lst.net/internal/db"
|
||||
"lst.net/internal/router"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := logging.New()
|
||||
// Load .env only in dev (not Docker/production)
|
||||
if os.Getenv("RUNNING_IN_DOCKER") != "true" {
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
log := logger.New()
|
||||
log.Info("Warning: .env file not found (ok in Docker/production)", "system", map[string]interface{}{})
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize DB
|
||||
if _, err := db.InitDB(); err != nil {
|
||||
log := logger.New()
|
||||
log.Panic("Database intialize failed", "db", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"casue": errors.Unwrap(err),
|
||||
@@ -54,6 +33,7 @@ func main() {
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log := logger.New()
|
||||
sqlDB, _ := db.DB.DB()
|
||||
sqlDB.Close()
|
||||
log.Error("Recovered from panic during DB shutdown", "db", map[string]interface{}{
|
||||
@@ -62,6 +42,10 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// long lived process like ocp running all the time should go here and base the db struct over.
|
||||
// go ocp.MonitorPrinters
|
||||
// go notifcations.Processor
|
||||
|
||||
// Set basePath dynamically
|
||||
basePath := "/"
|
||||
|
||||
@@ -69,79 +53,21 @@ func main() {
|
||||
basePath = "/lst" // Dev only
|
||||
}
|
||||
|
||||
// fmt.Println(name)
|
||||
fmt.Println("Welcome to lst backend where all the fun happens.")
|
||||
r := gin.Default()
|
||||
|
||||
if os.Getenv("APP_ENV") == "production" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
// Enable CORS (adjust origins as needed)
|
||||
r.Use(cors.New(cors.Config{
|
||||
AllowOrigins: []string{"*"}, // Allow all origins (change in production)
|
||||
AllowMethods: []string{"GET", "OPTIONS", "POST", "DELETE", "PATCH", "CONNECT"},
|
||||
AllowHeaders: []string{"Origin", "Cache-Control", "Content-Type"},
|
||||
ExposeHeaders: []string{"Content-Length"},
|
||||
AllowCredentials: true,
|
||||
AllowWebSockets: true,
|
||||
}))
|
||||
|
||||
// // --- 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) {
|
||||
log.Info("Checking if the server is up", "system", map[string]interface{}{
|
||||
"endpoint": "/api/ping",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
})
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
//logging.RegisterLoggerRoutes(r, basePath)
|
||||
websocket.RegisterSocketRoutes(r, basePath)
|
||||
settings.RegisterSettingsRoutes(r, basePath)
|
||||
|
||||
r.Any(basePath+"/api", errorApiLoc)
|
||||
// Init Gin router and pass DB to services
|
||||
r := router.Setup(db.DB, basePath)
|
||||
|
||||
// get the server port
|
||||
port := "8080"
|
||||
if os.Getenv("VITE_SERVER_PORT") != "" {
|
||||
port = os.Getenv("VITE_SERVER_PORT")
|
||||
}
|
||||
r.Run(":" + port)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
log := logging.New()
|
||||
log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
|
||||
"endpoint": "/api",
|
||||
"client_ip": c.ClientIP(),
|
||||
"user_agent": c.Request.UserAgent(),
|
||||
})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"})
|
||||
if err := r.Run(":" + port); err != nil {
|
||||
log := logger.New()
|
||||
log.Panic("Server failed to start", "system", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user