Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3f26386
Update README.md
Aradhya2708 Jun 25, 2025
b569f69
create tasks/crud/ folder structure
Anarghya1610 Jun 26, 2025
dd9fac1
Add files via upload
Anarghya1610 Jun 26, 2025
7841a44
Create .gitkeep
Anarghya1610 Jun 26, 2025
0f52a26
Add files via upload
Anarghya1610 Jun 26, 2025
54ea921
Create .gitkeep
Anarghya1610 Jun 26, 2025
d429b02
Add files via upload
Anarghya1610 Jun 26, 2025
7bf160c
Create .gitkeep
Anarghya1610 Jun 26, 2025
9d02733
Add files via upload
Anarghya1610 Jun 26, 2025
93105c5
Create README.md
Anarghya1610 Jun 26, 2025
715c12b
Update README.md
Anarghya1610 Jun 26, 2025
83d8e2f
Create .gitkeep
Anarghya1610 Jun 26, 2025
5d195d9
Add files via upload
Anarghya1610 Jun 26, 2025
e3580fe
Delete tasks/postman_screenshots directory
Anarghya1610 Jun 26, 2025
ca6573e
Create .gitkeep
Anarghya1610 Jun 26, 2025
7e21b83
Add files via upload
Anarghya1610 Jun 26, 2025
19b108e
Create .gitkeep
Anarghya1610 Jun 26, 2025
b52593b
Add localindexing Go tool
Anarghya1610 Jun 26, 2025
830ceec
Create README.md
Anarghya1610 Jun 26, 2025
e2bdb74
Create .gitkeep
Anarghya1610 Jun 28, 2025
1edbf52
Add MNIST classifier
Anarghya1610 Jun 28, 2025
171169b
Delete tasks/mnist_classifier/FileNest directory
Anarghya1610 Jun 28, 2025
da2eb10
Create README.md
Anarghya1610 Jun 28, 2025
267ad3a
Create data
Anarghya1610 Jun 28, 2025
7d21eed
Create models
Anarghya1610 Jun 28, 2025
e71f8d0
Delete tasks/mnist_classifier/data
Anarghya1610 Jun 28, 2025
0d2c936
Delete tasks/mnist_classifier/models
Anarghya1610 Jun 28, 2025
53330bf
Create .gitkeep
Anarghya1610 Jun 28, 2025
174c9ab
Create .gitkeep
Anarghya1610 Jun 28, 2025
32bb880
Add files via upload
Anarghya1610 Jun 28, 2025
8b5d5c4
Add files via upload
Anarghya1610 Jun 28, 2025
9f99854
Create .gitkeep
Anarghya1610 Jun 30, 2025
5666513
Add files via upload
Anarghya1610 Jun 30, 2025
3167ecc
Update README.md
Anarghya1610 Jun 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ FileNest/

```bash
# Clone the repository
git clone https://github.com/lakshyajain-0291/FileNest.git
git clone https://github.com/AISocietyIITJ/FileNest.git
cd FileNest

# Set up backend dependencies
Expand Down
1 change: 1 addition & 0 deletions tasks/crud/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

136 changes: 136 additions & 0 deletions tasks/crud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# πŸ“‚ FileNest - File Metadata CRUD API

This project is a basic **CRUD API in Go** for managing **file metadata** (such as filename, size, type, and upload time).
It is a foundational backend component for the upcoming **FileNest P2P File Sharing System**.

---

## πŸ“¦ Features

- βœ… REST API with Go and Gorilla Mux
- βœ… MongoDB Atlas integration
- βœ… CRUD operations for file metadata
- βœ… Input validation and error handling
- βœ… Manual testing via Postman

---

## πŸ“ Folder Structure

```
tasks/
└── crud/
β”œβ”€β”€ main.go
β”œβ”€β”€ model/
β”‚ └── models.go
β”œβ”€β”€ controller/
β”‚ └── controller.go
β”œβ”€β”€ router/
β”‚ └── router.go
β”œβ”€β”€ README.md
└── postman_screenshots/
```

## βš™οΈ Tech Stack

| Layer | Tech |
|----------------|-------------------------------|
| Language | Golang 1.21+ |
| Router | Gorilla Mux |
| Database | MongoDB Atlas (or local) |
| Driver | Mongo Go Driver |
| Testing Tool | Postman, Curl |

---

## πŸš€ Quick Start

### πŸ“‹ Prerequisites

- Go 1.21 or higher
- MongoDB Atlas or local MongoDB instance
- Git

### πŸ“₯ Installation

```bash
# Download Go dependencies
go mod tidy
```

### βš™οΈ Environment Setup

Create a `.env` file in the root with the following:

```
MONGO_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority
```

### ▢️ Run the Server

```bash
go run main.go
```

> Server starts on `http://localhost:4000`

---

## πŸ“‘ API Endpoints

| Method | Endpoint | Description |
|--------|---------------------|------------------------------|
| GET | `/files` | Get all file records |
| GET | `/files/{id}` | Get file by ID |
| POST | `/files` | Create new file record |
| PUT | `/files/{id}` | Update file by ID |
| DELETE | `/files/{id}` | Delete file by ID |

---

## πŸ“„ Data Model (FileMetadata)

```go
type FileMetadata struct {
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
Filename string `json:"filename" bson:"filename"`
Size int64 `json:"filesize" bson:"filesize"`
ContentType string `json:"content_type" bson:"content_type"`
UploadedAt primitive.DateTime `json:"uploaded_at,omitempty" bson:"uploaded_at,omitempty"
}
```

---

## πŸ§ͺ Testing

Use [Postman](https://www.postman.com/) or Curl:

### Create File (POST)

```bash
curl -X POST http://localhost:4000/files \
-H "Content-Type: application/json" \
-d '{"filename":"test.pdf","filepath":"/docs/test.pdf","filesize":1024,"content_type":"application/pdf"}'
```

### Get All Files (GET)

```bash
curl http://localhost:4000/files
```

### Update File (PUT)

```bash
curl -X PUT http://localhost:4000/files/<id> \
-H "Content-Type: application/json" \
-d '{"filename":"updated.pdf"}'
```

### Delete File (DELETE)

```bash
curl -X DELETE http://localhost:4000/files/<id>
```

1 change: 1 addition & 0 deletions tasks/crud/controller/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

117 changes: 117 additions & 0 deletions tasks/crud/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package controller

import (
"context"
"encoding/json"
"fmt"
"log"
"mongodbgo/model"
"net/http"
"time"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

const connectionString = "mongodb+srv://Anarghya:hellomonkey@cluster0.aprqfub.mongodb.net/"
const dbName = "filenest"
const colName = "metadata"

var collection *mongo.Collection

func init() {
clientOption := options.Client().ApplyURI(connectionString)
client, err := mongo.Connect(context.TODO(), clientOption)

if err != nil {
log.Fatal(err)
}
fmt.Println(("MongoDB Connection Successful"))

collection = client.Database(dbName).Collection(colName)
fmt.Println("Collection instance is ready")
}

func CreateFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var file model.FileMeta
err := json.NewDecoder(r.Body).Decode(&file)
if err != nil || file.FileName == "" || file.Size <= 0 {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

file.UploadedAt = primitive.NewDateTimeFromTime(time.Now())
result, err := collection.InsertOne(context.TODO(), file)
if err != nil {
http.Error(w, "Insert failed", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(result)
}

func GetAllFiles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
cur, err := collection.Find(context.TODO(), bson.D{{}})
if err != nil {
http.Error(w, "Error fetching", http.StatusInternalServerError)
return
}
var files []bson.M
if err := cur.All(context.TODO(), &files); err != nil {
http.Error(w, "Decode error", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(files)
}

func GetFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id, _ := primitive.ObjectIDFromHex(mux.Vars(r)["id"])
var file bson.M
err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(&file)
if err != nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(file)
}

func UpdateFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id, _ := primitive.ObjectIDFromHex(mux.Vars(r)["id"])

var updated model.FileMeta
if err := json.NewDecoder(r.Body).Decode(&updated); err != nil {
http.Error(w, "Invalid input", http.StatusBadRequest)
return
}

update := bson.M{"$set": bson.M{
"filename": updated.FileName,
"size": updated.Size,
"content_type": updated.ContentType,
}}

result, err := collection.UpdateOne(context.TODO(), bson.M{"_id": id}, update)
if err != nil {
http.Error(w, "Update failed", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(result)
}

func DeleteFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id, _ := primitive.ObjectIDFromHex(mux.Vars(r)["id"])

result, err := collection.DeleteOne(context.TODO(), bson.M{"_id": id})
if err != nil {
http.Error(w, "Delete failed", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(result)
}
18 changes: 18 additions & 0 deletions tasks/crud/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module mongodbgo

go 1.24.4

require (
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.mongodb.org/mongo-driver v1.17.4 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.17.0 // indirect
)
48 changes: 48 additions & 0 deletions tasks/crud/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw=
go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
16 changes: 16 additions & 0 deletions tasks/crud/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"log"
"mongodbgo/router"
"net/http"
)

func main() {
fmt.Println("MongoDB API")
r := router.Router()
fmt.Println("Server is getting started...")
log.Fatal(http.ListenAndServe(":4000", r))
fmt.Println("Listening at port 4000...")
}
1 change: 1 addition & 0 deletions tasks/crud/model/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 13 additions & 0 deletions tasks/crud/model/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model

import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

type FileMeta struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
FileName string `json:"filename"`
Size int64 `json:"size"`
ContentType string `json:"content_type"`
UploadedAt primitive.DateTime `json:"uploaded_at"`
}
1 change: 1 addition & 0 deletions tasks/crud/postman_screenshots/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tasks/crud/router/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

19 changes: 19 additions & 0 deletions tasks/crud/router/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package router

import (
"mongodbgo/controller"

"github.com/gorilla/mux"
)

func Router() *mux.Router {
router := mux.NewRouter()

router.HandleFunc("/files", controller.CreateFile).Methods("POST")
router.HandleFunc("/files", controller.GetAllFiles).Methods("GET")
router.HandleFunc("/files/{id}", controller.GetFile).Methods("GET")
router.HandleFunc("/files/{id}", controller.UpdateFile).Methods("PUT")
router.HandleFunc("/files/{id}", controller.DeleteFile).Methods("DELETE")

return router
}
1 change: 1 addition & 0 deletions tasks/localindexing/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading