-
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 #32 from WillyTonkas/feature/back/auth0
Feature/back/auth0
- Loading branch information
Showing
17 changed files
with
560 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
DATABASE_URL=<CONTAINER_URL># This is for the .env to get the Docker container URL | ||
HOST=<DB_HOST> | ||
POSTGRES_DB=<DB_NAME> | ||
POSTGRES_USER=<DB_USER> | ||
POSTGRES_PASSWORD=<DB_PASSWORD> | ||
DATABASE_PORT=<DB_PORT> | ||
SERVER_PORT=<API_PORT> | ||
DATABASE_URL_LOCAL=<LOCAL_URL># This is for the .env to get the local URL | ||
|
||
# The URL of our Auth0 Tenant Domain. | ||
# If you're using a Custom Domain, be sure to set this to that value instead. | ||
AUTH0_DOMAIN=<AUTH0_DOMAIN> | ||
|
||
# Our Auth0 application's Client ID. | ||
AUTH0_CLIENT_ID=<AUTH0_CLIENT_ID> | ||
|
||
# Our Auth0 application's Client Secret. | ||
AUTH0_CLIENT_SECRET=<AUTH0_CLIENT_SECRET> | ||
|
||
# The Callback URL of our application. Customizable. | ||
AUTH0_CALLBACK_URL=<AUTH0_CALLBACK_URL> | ||
AUTH0_AUDIENCE=<CUSTOM_API_IDENTIFIER> | ||
|
||
# These two are different from the previous ones, from these ones we're getting the Management API JWT | ||
AUTH0_TEST_CLIENT_ID=<AUTH0_TEST_APP_CLIENT_ID> | ||
AUTH0_TEST_CLIENT_SECRET=<AUTH0_TEST_APP_CLIENT_SECRET> |
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,61 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"log" | ||
"os" | ||
"rpl-service/constants" | ||
"rpl-service/models" | ||
) | ||
|
||
func StartDatabase() *gorm.DB { | ||
// Uncomment these lines when running in local | ||
// // Retrieve environment variables | ||
// err := godotenv.Load(".env") | ||
// if err != nil { | ||
// log.Fatalf("Error loading .env file: %v", err) | ||
// 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") | ||
|
||
envVariables := []string{host, user, password, dbname, port} | ||
|
||
for _, envVar := range envVariables { | ||
if envVar == 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,50 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
"net/http" | ||
"rpl-service/controllers/course" | ||
"rpl-service/models" | ||
"rpl-service/platform/middleware" | ||
) | ||
|
||
func InitializeRoutes(router *gin.Engine, db *gorm.DB) { | ||
for _, endpoint := range course.Endpoints { | ||
mapToGinRoute(router, endpoint, db) | ||
} | ||
} | ||
|
||
func mapToGinRoute(router *gin.Engine, endpoint models.Endpoint, db *gorm.DB) { | ||
methods := map[string]func(string, ...gin.HandlerFunc) gin.IRoutes{ | ||
"GET": router.GET, | ||
"POST": router.POST, | ||
"PUT": router.PUT, | ||
"DELETE": router.DELETE, | ||
} | ||
|
||
// Validate method exists | ||
method, exists := methods[endpoint.Method] | ||
if !exists { | ||
panic(fmt.Sprintf("Unsupported method: %s", endpoint.Method)) | ||
} | ||
|
||
// Create the base handler | ||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
endpoint.HandlerFunction(w, r, db) | ||
} | ||
|
||
// Apply middleware if the route is protected | ||
if endpoint.IsProtected { | ||
method(endpoint.Path, gin.WrapH(middleware.EnsureValidToken()( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
handler(w, r) | ||
})))) | ||
} else { | ||
// Unprotected route | ||
method(endpoint.Path, gin.WrapF(func(w http.ResponseWriter, r *http.Request) { | ||
handler(w, r) | ||
})) | ||
} | ||
} |
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,3 +1,6 @@ | ||
package constants | ||
|
||
import "time" | ||
|
||
const EmptyString = "" | ||
const ProviderDuration = 5 * time.Minute |
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 @@ | ||
package course_test |
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,50 @@ | ||
package course | ||
|
||
import ( | ||
"rpl-service/models" | ||
) | ||
|
||
const BaseURL = "/courses" | ||
|
||
var ExistsEndpoint = models.Endpoint{ | ||
Method: models.GET, | ||
Path: BaseURL + "/course/{id}", | ||
HandlerFunction: Exists, | ||
IsProtected: true, | ||
} | ||
|
||
var CreateCourseEndpoint = models.Endpoint{ | ||
Method: models.POST, | ||
Path: BaseURL + "/course", | ||
HandlerFunction: Create, | ||
IsProtected: true, | ||
} | ||
|
||
var EnrollToCourseEndpoint = models.Endpoint{ | ||
Method: models.POST, | ||
Path: BaseURL + "/enroll", | ||
HandlerFunction: EnrollToCourse, | ||
IsProtected: true, | ||
} | ||
|
||
var StudentExistsEndPoint = models.Endpoint{ | ||
Method: models.GET, | ||
Path: BaseURL + "/course/{id}/is-enrolled", | ||
HandlerFunction: StudentExists, | ||
IsProtected: true, | ||
} | ||
|
||
var DeleteStudentEndpoint = models.Endpoint{ | ||
Method: models.DELETE, | ||
Path: BaseURL + "/course/{id}/student", | ||
HandlerFunction: DeleteStudent, | ||
IsProtected: true, | ||
} | ||
|
||
var Endpoints = []models.Endpoint{ | ||
ExistsEndpoint, | ||
CreateCourseEndpoint, | ||
EnrollToCourseEndpoint, | ||
StudentExistsEndPoint, | ||
DeleteStudentEndpoint, | ||
} |
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
Oops, something went wrong.