init project
This commit is contained in:
42
pkg/handlers/mongo/mongo.go
Normal file
42
pkg/handlers/mongo/mongo.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"template_blueprint/internal/database"
|
||||
"template_blueprint/pkg/database/mongo"
|
||||
"template_blueprint/pkg/models/local"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetDataLog(c *gin.Context) {
|
||||
locals := os.Getenv("BLUEPRINT_DB_LOCAL")
|
||||
db := database.New(locals).GetDB()
|
||||
if db == nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
||||
return
|
||||
}
|
||||
mongoDB := mongo.NewDatabaseService(db)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cursor, err := mongoDB.DB.Collection("startup_log").Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database query failed"})
|
||||
return
|
||||
}
|
||||
|
||||
var dataLog []*local.StartUpLog
|
||||
errDecode := cursor.All(ctx, &dataLog)
|
||||
if errDecode != nil {
|
||||
log.Println(errDecode)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database query failed"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dataLog)
|
||||
}
|
||||
Reference in New Issue
Block a user