Skip to content

Commit

Permalink
feat(router): Added gin
Browse files Browse the repository at this point in the history
Added gin to the application, still need to figure why the authenticator isn't working

Referred issue: #30
  • Loading branch information
FranCalveyra committed Jan 22, 2025
1 parent 55ae3bc commit f353c0f
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ AUTH0_CLIENT_ID=<AUTH0_CLIENT_ID>
AUTH0_CLIENT_SECRET=<AUTH0_CLIENT_SECRET>

# The Callback URL of our application. Customizable.
AUTH0_CALLBACK_URL=<AUTH0_CALLBACK_URL>
AUTH0_CALLBACK_URL=<AUTH0_CALLBACK_URL>
AUTH0_AUDIENCE=<CUSTOM_API_IDENTIFIER>
7 changes: 3 additions & 4 deletions config/database.go → config/databaseConfig.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database
package config

import (
"fmt"
Expand All @@ -11,10 +11,9 @@ import (
)

func StartDatabase() *gorm.DB {
// TODO: clean this commented code, catch error cases
// Retrieve environment variables
// err := godotenv.Load(".env")
// if err != nil {
//err := godotenv.Load(".env")

Check failure on line 15 in config/databaseConfig.go

View workflow job for this annotation

GitHub Actions / lint-and-test

commentFormatting: put a space between `//` and comment text (gocritic)
//if err != nil {
// log.Fatalf("Error loading .env file: %v", err)
// return nil
//}
Expand Down
34 changes: 34 additions & 0 deletions config/routerConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package config

import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"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,
}

if endpoint.IsProtected {
methods[endpoint.Method](endpoint.Path, middleware.IsAuthenticated, func(ctx *gin.Context) {
endpoint.HandlerFunction(ctx.Writer, ctx.Request, db)
})
} else {
methods[endpoint.Method](endpoint.Path, func(ctx *gin.Context) {
endpoint.HandlerFunction(ctx.Writer, ctx.Request, db)
})
}
}
13 changes: 13 additions & 0 deletions controllers/course/courseRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,41 @@ 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,
}
38 changes: 37 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,51 @@ go 1.23

require (
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.25.12
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
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/go-jose/go-jose/v3 v3.0.0 // indirect
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
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.2.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.2 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand All @@ -32,6 +64,10 @@ require (
)

require (
github.com/auth0/go-jwt-middleware/v2 v2.2.2
github.com/coreos/go-oidc/v3 v3.8.0
github.com/gin-contrib/sessions v1.0.2
github.com/gin-gonic/gin v1.10.0
github.com/joho/godotenv v1.5.1
golang.org/x/oauth2 v0.15.0
)
Loading

0 comments on commit f353c0f

Please sign in to comment.