Skip to content

Commit

Permalink
restructing the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Atoo35 committed Oct 3, 2024
1 parent 4a64a42 commit 53ebdca
Show file tree
Hide file tree
Showing 10 changed files with 1,116 additions and 1,020 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stockbackend
36 changes: 36 additions & 0 deletions clients/mongo/mongo_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package mongo_client

import (
"context"
"os"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
"gopkg.in/mgo.v2/bson"
)

var (
Client *mongo.Client
)

func init() {
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
mongoURI := os.Getenv("MONGO_URI")
// zap.L().Info("Mongo URI", zap.String("uri", mongoURI))
opts := options.Client().ApplyURI(mongoURI).SetServerAPIOptions(serverAPI)
// Create a new client and connect to the server
var err error
Client, err = mongo.Connect(context.TODO(), opts)
if err != nil {
panic(err)
}

// Send a ping to confirm a successful connection
pingCmd := bson.M{"ping": 1}
if err := Client.Database("admin").RunCommand(context.TODO(), pingCmd).Err(); err != nil {
panic(err)
}

zap.L().Info("Connected to MongoDB")
}
45 changes: 45 additions & 0 deletions controllers/file_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package controllers

import (
"stockbackend/services"

"github.com/gin-gonic/gin"
)

type FileControllerI interface {
ParseXLSXFile(ctx *gin.Context)
}

type fileController struct{}

var FileController FileControllerI = &fileController{}

func (f *fileController) ParseXLSXFile(ctx *gin.Context) {
// Parse the form and retrieve the uploaded files
form, err := ctx.MultipartForm()
if err != nil {
ctx.JSON(400, gin.H{"error": "Error parsing form data"})
return
}

// Retrieve the files from the form
files := form.File["files"]
if len(files) == 0 {
ctx.JSON(400, gin.H{"error": "No files found"})
return
}

// Set headers for chunked transfer (if needed)
ctx.Writer.Header().Set("Content-Type", "text/plain")
ctx.Writer.Header().Set("Cache-Control", "no-cache")
ctx.Writer.Header().Set("Connection", "keep-alive")

err = services.FileService.ParseXLSXFile(ctx, files)
if err != nil {
ctx.JSON(500, gin.H{"error": err.Error()})
return
}

ctx.Writer.Write([]byte("\nStream complete.\n"))
ctx.Writer.Flush() // Ensure the final response is sent
}
15 changes: 15 additions & 0 deletions controllers/health_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package controllers

import "github.com/gin-gonic/gin"

type HealthControllerI interface {
IsRunning(ctx *gin.Context)
}

type healthController struct{}

var HealthController HealthControllerI = &healthController{}

func (h *healthController) IsRunning(ctx *gin.Context) {
ctx.JSON(200, gin.H{"message": "Server is running"})
}
Loading

0 comments on commit 53ebdca

Please sign in to comment.