Skip to content

Commit

Permalink
Merge pull request #31 from CS3219-AY2425S1/frontend-docker-vol-binding
Browse files Browse the repository at this point in the history
Add Docker support
  • Loading branch information
WZWren authored Oct 6, 2024
2 parents 4596f44 + 93659bb commit f2cc844
Show file tree
Hide file tree
Showing 83 changed files with 5,446 additions and 421 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/bzPrOe11)
# CS3219 Project (PeerPrep) - AY2425S1
## Group: Gxx
## Group: G14

### Note:
- You can choose to develop individual microservices within separate folders within this repository **OR** use individual repositories (all public) for each microservice.
Expand Down
3 changes: 3 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URI=mongodb+srv://<user>:<password>@<collection>.<name>.mongodb.net/?retryWrites=true&w=majority&appName=<appname>
PORT=:9090
CORS_ORIGIN=http://localhost:3000
2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
questionDB.env
.env
log
29 changes: 29 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# syntax=docker/dockerfile:1

FROM golang:1.23

# Set destination for COPY
WORKDIR /backend

# Download Go modules
# TODO: don't include the .env file in the COPY
# TODO: multistage build
COPY go.mod go.sum ./
RUN go mod download

# Copy the source code. Note the slash at the end, as explained in
# https://docs.docker.com/reference/dockerfile/#copy
COPY . .

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /backend/app

# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
# But we can document in the Dockerfile what ports
# the application is going to listen on by default.
# https://docs.docker.com/reference/dockerfile/#expose
EXPOSE 9090

# Run
CMD ["/backend/app"]
2 changes: 1 addition & 1 deletion backend/common/logger_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/sirupsen/logrus"
)

//contains the logger
// contains the logger
type Logger struct {
Log *logrus.Logger
}
Expand Down
29 changes: 13 additions & 16 deletions backend/common/question_struct.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
// defines the JSON format of quesitons.
package common

type Difficulty int

const (
Easy Difficulty = 1
Medium Difficulty = 2
Hard Difficulty = 3
)

// Question struct
type Question struct {
ID int `json:"id"`
Difficulty Difficulty `json:"difficulty"`
Title string `json:"title"`
Description string `json:"description"`
Categories []string `json:"categories"`
TestCases map[string]string `json:"test_cases"`
//Images []string `json:"images"` // for future uses
Title string `json:"title"`
TitleSlug string `json:"titleSlug"`
Difficulty string `json:"difficulty"`
TopicTags []string `json:"topicTags"`
Content string `json:"content"`
Schemas []string `json:"schemas"`
Id int `json:"id"`
}

type FrontendQuestion struct {
Title string `json:"title"`
Difficulty string `json:"difficulty"`
TopicTags []string `json:"topicTags"`
Content string `json:"content"`
}
34 changes: 18 additions & 16 deletions backend/database/database_interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ package database
import (
"context"
"errors"
"net/http"
"fmt"
"net/http"

"peerprep/common"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"peerprep/common"
)

func (db *QuestionDB) GetAllQuestionsWithQuery(logger *common.Logger, filter bson.D) ([]common.Question, error) {
func (db *QuestionDB) GetAllQuestionsWithQuery(
logger *common.Logger,
filter bson.D,
) ([]common.Question, error) {
questionCursor, err := db.questions.Find(context.Background(), filter)

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

question.ID = db.FindNextQuestionId()

if question.ID == -1 {
logger.Log.Error("Could not find next question ID")
return http.StatusBadGateway, errors.New("could not find the next question ID")
}

if _, err := db.questions.InsertOne(context.Background(), question); err != nil {
logger.Log.Error("Error adding question", err.Error())
return http.StatusBadGateway, err
}

db.IncrementNextQuestionId(question.ID + 1, logger)
return http.StatusOK, nil
}

func (db *QuestionDB) UpsertQuestion(logger *common.Logger, question *common.Question) (int, error) {
func (db *QuestionDB) UpsertQuestion(
logger *common.Logger,
question *common.Question,
) (int, error) {

filter := bson.D{bson.E{Key: "id", Value: question.ID}}
filter := bson.D{bson.E{Key: "id", Value: question.Id}}
setter := bson.M{"$set": question}
upsert := options.Update().SetUpsert(true)

_, err := db.questions.UpdateOne(context.Background(), filter, setter, upsert)

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

func (db *QuestionDB) DeleteQuestion(logger *common.Logger, id int) (int, error) {
deleteStatus, err := db.questions.DeleteOne(context.Background(), bson.D{bson.E{Key: "id", Value: id}})
deleteStatus, err := db.questions.DeleteOne(
context.Background(),
bson.D{bson.E{Key: "id", Value: id}},
)

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

return http.StatusNoContent, nil
}
}
28 changes: 11 additions & 17 deletions backend/database/database_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package database

import (
"context"
"fmt"
"peerprep/common"

"go.mongodb.org/mongo-driver/bson"
"peerprep/common"
)

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

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

// used to increment the next question ID to be used.
// since the collection is capped at one document, inserting a new document will replace the old one.
func (db *QuestionDB) IncrementNextQuestionId(nextId int, logger *common.Logger) {
var err error
if _, err = db.nextId.InsertOne(context.Background(), bson.D{bson.E{Key: "next", Value: nextId}}); err != nil {
logger.Log.Error("Error incrementing next question ID: ", err)
return
}

logger.Log.Info(fmt.Sprintf("Next question ID incremented to %d successfully", nextId))
}

// used to check if a question being replaced will cause duplicates in the database

func (db *QuestionDB) QuestionExistsExceptId(question *common.Question) bool {
filter := bson.D{bson.E{Key: "title", Value: question.Title}, bson.E{Key: "id", Value: bson.D{bson.E{Key: "$ne", Value: question.ID}}}}
err := db.questions.FindOne(context.Background(), filter).Decode(&common.Question{}) //FindOne() returns error if no document is found
filter := bson.D{
bson.E{Key: "title", Value: question.Title},
bson.E{Key: "id", Value: bson.D{bson.E{Key: "$ne", Value: question.Id}}},
}
err := db.questions.FindOne(context.Background(), filter).
Decode(&common.Question{})
//FindOne() returns error if no document is found
return err == nil
}
2 changes: 1 addition & 1 deletion backend/database/initialise_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func InitialiseDB() (*mongo.Client, error) {
// Load environment variables
err := godotenv.Load("questionDB.env")
err := godotenv.Load(".env")

if err != nil {
log.Fatal("Error loading environment variables: " + err.Error())
Expand Down
6 changes: 3 additions & 3 deletions backend/database/questionDB_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
// questions is the collection with all the questions, nextId is a single-entry collection that stores the next ID to be used.
type QuestionDB struct {
questions *mongo.Collection
nextId *mongo.Collection
nextId *mongo.Collection
}

// returns a pointer to an instance of the Question collection
func NewQuestionDB(client *mongo.Client) *QuestionDB {

questionCollection := client.Database("questions").Collection("questions")
idCollection := client.Database("questions").Collection("id")
idCollection := client.Database("questions").Collection("counter")
return &QuestionDB{questions: questionCollection, nextId: idCollection}
}
}
25 changes: 17 additions & 8 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module peerprep

go 1.20
go 1.23

require github.com/joho/godotenv v1.5.1 // indirect -allows to load environment variables from a .env file instead of hardcoding them in the code

Expand All @@ -11,7 +11,7 @@ require (
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/gin-gonic/gin v1.10.0
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
Expand All @@ -33,15 +33,24 @@ require (
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-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.16.1 // indirect
go.mongodb.org/mongo-driver v1.16.1
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require github.com/gin-contrib/cors v1.7.2 // indirect
require (
github.com/gin-contrib/cors v1.7.2
github.com/microcosm-cc/bluemonday v1.0.27
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/kr/text v0.2.0 // indirect
)
Loading

0 comments on commit f2cc844

Please sign in to comment.