Skip to content

Commit f2cc844

Browse files
authored
Merge pull request #31 from CS3219-AY2425S1/frontend-docker-vol-binding
Add Docker support
2 parents 4596f44 + 93659bb commit f2cc844

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+5446
-421
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/.env

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/bzPrOe11)
22
# CS3219 Project (PeerPrep) - AY2425S1
3-
## Group: Gxx
3+
## Group: G14
44

55
### Note:
66
- You can choose to develop individual microservices within separate folders within this repository **OR** use individual repositories (all public) for each microservice.

backend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
MONGODB_URI=mongodb+srv://<user>:<password>@<collection>.<name>.mongodb.net/?retryWrites=true&w=majority&appName=<appname>
2+
PORT=:9090
3+
CORS_ORIGIN=http://localhost:3000

backend/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
questionDB.env
1+
.env
22
log

backend/Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM golang:1.23
4+
5+
# Set destination for COPY
6+
WORKDIR /backend
7+
8+
# Download Go modules
9+
# TODO: don't include the .env file in the COPY
10+
# TODO: multistage build
11+
COPY go.mod go.sum ./
12+
RUN go mod download
13+
14+
# Copy the source code. Note the slash at the end, as explained in
15+
# https://docs.docker.com/reference/dockerfile/#copy
16+
COPY . .
17+
18+
# Build
19+
RUN CGO_ENABLED=0 GOOS=linux go build -o /backend/app
20+
21+
# Optional:
22+
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
23+
# But we can document in the Dockerfile what ports
24+
# the application is going to listen on by default.
25+
# https://docs.docker.com/reference/dockerfile/#expose
26+
EXPOSE 9090
27+
28+
# Run
29+
CMD ["/backend/app"]

backend/common/logger_struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/sirupsen/logrus"
55
)
66

7-
//contains the logger
7+
// contains the logger
88
type Logger struct {
99
Log *logrus.Logger
1010
}

backend/common/question_struct.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
// defines the JSON format of quesitons.
22
package common
33

4-
type Difficulty int
5-
6-
const (
7-
Easy Difficulty = 1
8-
Medium Difficulty = 2
9-
Hard Difficulty = 3
10-
)
11-
12-
// Question struct
134
type Question struct {
14-
ID int `json:"id"`
15-
Difficulty Difficulty `json:"difficulty"`
16-
Title string `json:"title"`
17-
Description string `json:"description"`
18-
Categories []string `json:"categories"`
19-
TestCases map[string]string `json:"test_cases"`
20-
//Images []string `json:"images"` // for future uses
5+
Title string `json:"title"`
6+
TitleSlug string `json:"titleSlug"`
7+
Difficulty string `json:"difficulty"`
8+
TopicTags []string `json:"topicTags"`
9+
Content string `json:"content"`
10+
Schemas []string `json:"schemas"`
11+
Id int `json:"id"`
2112
}
2213

14+
type FrontendQuestion struct {
15+
Title string `json:"title"`
16+
Difficulty string `json:"difficulty"`
17+
TopicTags []string `json:"topicTags"`
18+
Content string `json:"content"`
19+
}

backend/database/database_interactions.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ package database
44
import (
55
"context"
66
"errors"
7-
"net/http"
87
"fmt"
8+
"net/http"
9+
10+
"peerprep/common"
911

1012
"go.mongodb.org/mongo-driver/bson"
1113
"go.mongodb.org/mongo-driver/mongo/options"
12-
"peerprep/common"
1314
)
1415

15-
func (db *QuestionDB) GetAllQuestionsWithQuery(logger *common.Logger, filter bson.D) ([]common.Question, error) {
16+
func (db *QuestionDB) GetAllQuestionsWithQuery(
17+
logger *common.Logger,
18+
filter bson.D,
19+
) ([]common.Question, error) {
1620
questionCursor, err := db.questions.Find(context.Background(), filter)
1721

1822
if err != nil {
@@ -36,28 +40,23 @@ func (db *QuestionDB) AddQuestion(logger *common.Logger, question *common.Questi
3640
return http.StatusConflict, errors.New("question already exists")
3741
}
3842

39-
question.ID = db.FindNextQuestionId()
40-
41-
if question.ID == -1 {
42-
logger.Log.Error("Could not find next question ID")
43-
return http.StatusBadGateway, errors.New("could not find the next question ID")
44-
}
45-
4643
if _, err := db.questions.InsertOne(context.Background(), question); err != nil {
4744
logger.Log.Error("Error adding question", err.Error())
4845
return http.StatusBadGateway, err
4946
}
5047

51-
db.IncrementNextQuestionId(question.ID + 1, logger)
5248
return http.StatusOK, nil
5349
}
5450

55-
func (db *QuestionDB) UpsertQuestion(logger *common.Logger, question *common.Question) (int, error) {
51+
func (db *QuestionDB) UpsertQuestion(
52+
logger *common.Logger,
53+
question *common.Question,
54+
) (int, error) {
5655

57-
filter := bson.D{bson.E{Key: "id", Value: question.ID}}
56+
filter := bson.D{bson.E{Key: "id", Value: question.Id}}
5857
setter := bson.M{"$set": question}
5958
upsert := options.Update().SetUpsert(true)
60-
59+
6160
_, err := db.questions.UpdateOne(context.Background(), filter, setter, upsert)
6261

6362
if err != nil {
@@ -69,7 +68,10 @@ func (db *QuestionDB) UpsertQuestion(logger *common.Logger, question *common.Que
6968
}
7069

7170
func (db *QuestionDB) DeleteQuestion(logger *common.Logger, id int) (int, error) {
72-
deleteStatus, err := db.questions.DeleteOne(context.Background(), bson.D{bson.E{Key: "id", Value: id}})
71+
deleteStatus, err := db.questions.DeleteOne(
72+
context.Background(),
73+
bson.D{bson.E{Key: "id", Value: id}},
74+
)
7375

7476
if err != nil {
7577
logger.Log.Error("Error deleting question", err.Error())
@@ -81,4 +83,4 @@ func (db *QuestionDB) DeleteQuestion(logger *common.Logger, id int) (int, error)
8183
}
8284

8385
return http.StatusNoContent, nil
84-
}
86+
}

backend/database/database_util.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package database
33

44
import (
55
"context"
6-
"fmt"
6+
"peerprep/common"
77

88
"go.mongodb.org/mongo-driver/bson"
9-
"peerprep/common"
109
)
1110

1211
// used to check if a question already exists in the database.
1312
func (db *QuestionDB) QuestionExists(question *common.Question) bool {
1413
filter := bson.D{bson.E{Key: "title", Value: question.Title}}
15-
err := db.questions.FindOne(context.Background(), filter).Decode(&common.Question{}) //FindOne() returns error if no document is found
14+
err := db.questions.FindOne(context.Background(), filter).
15+
Decode(&common.Question{})
16+
//FindOne() returns error if no document is found
1617
return err == nil
1718
}
1819

@@ -30,22 +31,15 @@ func (db *QuestionDB) FindNextQuestionId() int {
3031
return id.Next
3132
}
3233

33-
// used to increment the next question ID to be used.
34-
// since the collection is capped at one document, inserting a new document will replace the old one.
35-
func (db *QuestionDB) IncrementNextQuestionId(nextId int, logger *common.Logger) {
36-
var err error
37-
if _, err = db.nextId.InsertOne(context.Background(), bson.D{bson.E{Key: "next", Value: nextId}}); err != nil {
38-
logger.Log.Error("Error incrementing next question ID: ", err)
39-
return
40-
}
41-
42-
logger.Log.Info(fmt.Sprintf("Next question ID incremented to %d successfully", nextId))
43-
}
44-
4534
// used to check if a question being replaced will cause duplicates in the database
4635

4736
func (db *QuestionDB) QuestionExistsExceptId(question *common.Question) bool {
48-
filter := bson.D{bson.E{Key: "title", Value: question.Title}, bson.E{Key: "id", Value: bson.D{bson.E{Key: "$ne", Value: question.ID}}}}
49-
err := db.questions.FindOne(context.Background(), filter).Decode(&common.Question{}) //FindOne() returns error if no document is found
37+
filter := bson.D{
38+
bson.E{Key: "title", Value: question.Title},
39+
bson.E{Key: "id", Value: bson.D{bson.E{Key: "$ne", Value: question.Id}}},
40+
}
41+
err := db.questions.FindOne(context.Background(), filter).
42+
Decode(&common.Question{})
43+
//FindOne() returns error if no document is found
5044
return err == nil
5145
}

backend/database/initialise_database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func InitialiseDB() (*mongo.Client, error) {
1515
// Load environment variables
16-
err := godotenv.Load("questionDB.env")
16+
err := godotenv.Load(".env")
1717

1818
if err != nil {
1919
log.Fatal("Error loading environment variables: " + err.Error())

0 commit comments

Comments
 (0)