feat(logging): added in db and logging with websocket
This commit is contained in:
@@ -1,24 +1,63 @@
|
||||
// @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"
|
||||
"log"
|
||||
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
"lst.net/cmd/services/logging"
|
||||
_ "lst.net/docs"
|
||||
"lst.net/utils/db"
|
||||
)
|
||||
|
||||
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.Println("Warning: .env file not found (ok in Docker/production)")
|
||||
log.Info("Warning: .env file not found (ok in Docker/production)", "system", map[string]interface{}{})
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize DB
|
||||
if err := db.InitDB(); err != nil {
|
||||
log.Panic("Database intialize failed", "db", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
"casue": errors.Unwrap(err),
|
||||
"timeout": "30s",
|
||||
"details": fmt.Sprintf("%+v", err), // Full stack trace if available
|
||||
})
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
sqlDB, _ := db.DB.DB()
|
||||
sqlDB.Close()
|
||||
log.Error("Recovered from panic during DB shutdown", "db", map[string]interface{}{
|
||||
"panic": r,
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
// Set basePath dynamically
|
||||
basePath := "/"
|
||||
|
||||
@@ -34,6 +73,16 @@ func main() {
|
||||
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) {
|
||||
@@ -52,34 +101,18 @@ func main() {
|
||||
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)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -93,5 +126,11 @@ func main() {
|
||||
// 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.Info("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"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user