-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from WillyTonkas/feature/back/setup-application
Feature/back/setup application
- Loading branch information
Showing
18 changed files
with
356 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea | ||
*.iml | ||
*.iml | ||
.env | ||
.gitmessage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package constants | ||
|
||
const EmptyString = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
"net/http" | ||
"rpl-service/constants" | ||
"rpl-service/models" | ||
"rpl-service/services/users" | ||
) | ||
|
||
const BaseURL = "/courses" | ||
|
||
func CourseExists(w http.ResponseWriter, r *http.Request, db *gorm.DB) { | ||
courseID := r.PathValue("id") // Get the course ID from the URL | ||
if courseID == constants.EmptyString { | ||
http.Error(w, "Course ID is required", http.StatusBadRequest) | ||
return | ||
} | ||
courseUUID, err := uuid.Parse(courseID) | ||
if err != nil { | ||
http.Error(w, "Invalid course ID format", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Should return whether a user with that ID exists | ||
if !users.CourseExists(db, courseUUID) { // TODO: change package name | ||
http.Error(w, "Course not found", http.StatusNotFound) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write([]byte("Course exists")) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
|
||
func CreateCourse(w http.ResponseWriter, r *http.Request, db *gorm.DB) { | ||
// Should create a new course | ||
var body models.Course | ||
err := json.NewDecoder(r.Body).Decode(&body) | ||
if err != nil { | ||
http.Error(w, "Invalid request body", http.StatusBadRequest) | ||
return | ||
} | ||
userID := uuid.New() // TODO: get the actual userID | ||
|
||
currentCourse, creatingCourseErr := users.CreateCourse(db, userID, body.Name, body.Description) | ||
if creatingCourseErr != nil { | ||
http.Error(w, "Failed to create course", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
response, err := json.Marshal(currentCourse) | ||
if err != nil { | ||
http.Error(w, "Failed to marshal response", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusCreated) | ||
_, err = w.Write(response) | ||
if err != nil { | ||
http.Error(w, "Failed to write response", http.StatusInternalServerError) | ||
return | ||
} | ||
} | ||
|
||
var CourseExistsEndpoint = models.Endpoint{ | ||
Method: models.GET, | ||
Path: BaseURL + "/course/exists/{id}", | ||
HandlerFunction: CourseExists, | ||
} | ||
|
||
var CreateCourseEndpoint = models.Endpoint{ | ||
Method: models.POST, | ||
Path: BaseURL + "/course", | ||
HandlerFunction: CreateCourse, | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= | ||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= | ||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= | ||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= | ||
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= | ||
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= | ||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= | ||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= | ||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= | ||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= | ||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= | ||
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= | ||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= | ||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,112 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/joho/godotenv" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"log" | ||
"net/http" | ||
"os" | ||
"rpl-service/constants" | ||
"rpl-service/controllers" | ||
"rpl-service/models" | ||
) | ||
|
||
// Should run the main web application | ||
// This is a mock version of the main application, this is the binary to compile on CD. | ||
func main() { | ||
// Should run the main web application | ||
fmt.Println("Hello World!") | ||
// Start the database | ||
startServer() | ||
} | ||
|
||
func startServer() { | ||
db := startDatabase() | ||
if db == nil { | ||
fmt.Println("Error starting the database") | ||
return | ||
} | ||
|
||
s, serverError := db.DB() | ||
if serverError != nil { | ||
return | ||
} | ||
|
||
// Defer its closing | ||
defer func(db *sql.DB) { | ||
err := db.Close() | ||
if err != nil { | ||
return | ||
} | ||
}(s) | ||
|
||
// Here should go the functions for each endpoint | ||
|
||
http.HandleFunc(controllers.CourseExistsEndpoint.Path, func(writer http.ResponseWriter, request *http.Request) { | ||
controllers.CourseExistsEndpoint.HandlerFunction(writer, request, db) | ||
}) | ||
|
||
http.HandleFunc(controllers.CreateCourseEndpoint.Path, func(writer http.ResponseWriter, request *http.Request) { | ||
controllers.CreateCourseEndpoint.HandlerFunction(writer, request, db) | ||
}) | ||
|
||
serverPort := os.Getenv("SERVER_PORT") | ||
|
||
if serverPort == constants.EmptyString { | ||
log.Panic("serverPort environment variable is not set") | ||
} | ||
|
||
serverError = http.ListenAndServe(":"+serverPort, nil) | ||
if serverError != nil { | ||
return | ||
} | ||
} | ||
|
||
func startDatabase() *gorm.DB { | ||
// Retrieve environment variables | ||
err := godotenv.Load(".env") | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
host := os.Getenv("HOST") | ||
user := os.Getenv("POSTGRES_USER") | ||
password := os.Getenv("POSTGRES_PASSWORD") | ||
dbname := os.Getenv("POSTGRES_DB") | ||
port := os.Getenv("DATABASE_PORT") | ||
|
||
if host == constants.EmptyString || | ||
user == constants.EmptyString || | ||
password == constants.EmptyString || | ||
dbname == constants.EmptyString || | ||
port == constants.EmptyString { | ||
log.Fatal("One or more database environment variables are not set") | ||
} | ||
|
||
// Database connection string | ||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable", host, user, password, dbname, port) | ||
|
||
// Open the database connection | ||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatalf("failed to connect to the database: %v", err) | ||
} | ||
|
||
// Enable uuid-ossp extension | ||
err = db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"").Error | ||
if err != nil { | ||
log.Fatalf("failed to enable uuid-ossp extension: %v", err) | ||
} | ||
|
||
migrateSchemas(db) | ||
|
||
return db | ||
} | ||
|
||
func migrateSchemas(db *gorm.DB) { | ||
err := db.AutoMigrate(&models.Course{}, &models.Exercise{}, &models.Test{}, &models.IsEnrolled{}) | ||
if err != nil { | ||
log.Fatalf("failed to migrate database: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
GET = "GET" | ||
POST = "POST" | ||
PUT = "PUT" | ||
DELETE = "DELETE" | ||
) | ||
|
||
type Endpoint struct { | ||
Method string | ||
Path string | ||
HandlerFunction func(w http.ResponseWriter, r *http.Request, db *gorm.DB) | ||
} |
Oops, something went wrong.