From 91c73b197e71ca70621ea61e345a53d8c731fb4b Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 16:41:13 +0100 Subject: [PATCH 1/9] "Updated GitHub workflows for development, production, and staging environments to use a script for deployment and added pull request triggers." --- .github/workflows/development.yml | 39 +++++------------ .github/workflows/production.yml | 37 ++++------------ .github/workflows/staging.yml | 39 +++++------------ scripts/deploy_app.sh | 70 +++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 86 deletions(-) create mode 100644 scripts/deploy_app.sh diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 25658b14..0aeb7057 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -1,9 +1,12 @@ -name: Build, Test, and Deploy for Development +name: Build, Test, and Deploy for Production on: push: branches: - dev + pull_request: + branches: + - dev jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_development_app + SERVER_PORT: 7000 + DB_NAME: "development_db" + USERNAME: "development_user" + APP_NAME: "development" + APP_URL: "http://localhost:7000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/development - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin dev || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b dev http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/development/development_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh development SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 06325392..42447b24 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -4,6 +4,9 @@ on: push: branches: - main + pull_request: + branches: + - main jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_production_app + SERVER_PORT: 9000 + DB_NAME: "production_db" + USERNAME: "production_user" + APP_NAME: "production" + APP_URL: "http://localhost:9000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/production - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin main || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b main http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/production/production_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh production SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index f4b49264..ee3b6ef1 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,9 +1,12 @@ -name: Build, Test, and Deploy for Staging +name: Build, Test, and Deploy for Production on: push: branches: - staging + pull_request: + branches: + - staging jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_staging_app + SERVER_PORT: 8000 + DB_NAME: "staging_db" + USERNAME: "staging_user" + APP_NAME: "staging" + APP_URL: "http://localhost:8000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/staging - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin staging || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b staging http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/staging/staging_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh staging SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/scripts/deploy_app.sh b/scripts/deploy_app.sh new file mode 100644 index 00000000..06a8c9e6 --- /dev/null +++ b/scripts/deploy_app.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 [env_vars...]" + exit 1 +fi + +ENV=$1 +GITHUB_REPOSITORY=$2 +shift 2 + +case $ENV in + development) + BRANCH="dev" + APPROOT=~/deployments/development + APP_NAME="development_app" + ;; + staging) + BRANCH="staging" + APPROOT=~/deployments/staging + APP_NAME="staging_app" + ;; + production) + BRANCH="main" + APPROOT=~/deployments/production + APP_NAME="production_app" + ;; + *) + echo "Invalid environment: $ENV" + exit 1 + ;; +esac + +export APPROOT +export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin +export PATH=$PATH:/usr/local/go/bin + +mkdir -p $APPROOT +cd $APPROOT + +if [ -d "$APPROOT/.git" ]; then + # Navigate to the repository directory and pull changes + cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } + git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } + git pull origin $BRANCH || { echo "Failed to pull latest changes"; exit 1; } +else + git clone -b $BRANCH https://github.com/$GITHUB_REPOSITORY . || { echo "Failed to clone repository"; exit 1; } +fi + +# Replace environment variables in app.env +cp app-sample.env app.env +for VAR in "$@" +do + KEY=$(echo $VAR | cut -d '=' -f 1) + VALUE=$(echo $VAR | cut -d '=' -f 2) + sed -i "s|\${env.$KEY}|$VALUE|g" app.env +done + +go build -o $APPROOT/$APP_NAME + +# Start or restart the built binary with pm2 +if pm2 list | grep -qw "$APP_NAME"; then + echo "Process $APP_NAME is running. Restarting..." + pm2 restart $APPROOT/$APP_NAME +else + echo "Process $APP_NAME is not running. Starting..." + pm2 start $APPROOT/$APP_NAME --name $APP_NAME +fi \ No newline at end of file From 29cc52b06ad5a6967888fdd563b53a4011191cd4 Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 17:20:15 +0100 Subject: [PATCH 2/9] "Updated workflow names in development.yml and staging.yml" --- .github/workflows/development.yml | 2 +- .github/workflows/staging.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 0aeb7057..db3ad1d6 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Production +name: Build, Test, and Deploy for Development on: push: diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index ee3b6ef1..e13eb1db 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Production +name: Build, Test, and Deploy for Staging on: push: From 7698b33fecb084a92c562206b49739e084064677 Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 19:06:05 +0100 Subject: [PATCH 3/9] "Updated deployment scripts in development, production, and staging workflows to change directory to ~/deployments/ before running deploy_app.sh" --- .github/workflows/development.yml | 3 ++- .github/workflows/production.yml | 3 ++- .github/workflows/staging.yml | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index db3ad1d6..cca31641 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -72,4 +72,5 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - bash ~/scripts/deploy_app.sh development SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + cd ~/deployments/development + bash ./scripts/deploy_app.sh development SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 42447b24..4635d516 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -72,4 +72,5 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - bash ~/scripts/deploy_app.sh production SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + cd ~/deployments/production + bash ./scripts/deploy_app.sh production SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index e13eb1db..94bc0c8c 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -72,4 +72,5 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - bash ~/scripts/deploy_app.sh staging SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + cd ~/deployments/staging + bash ./scripts/deploy_app.sh staging SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} From f19ef7bfc4a03e9931269969a7f16216383d1101 Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 19:25:17 +0100 Subject: [PATCH 4/9] Add copy env to test job --- .github/workflows/development.yml | 2 ++ .github/workflows/production.yml | 2 ++ .github/workflows/staging.yml | 2 ++ 3 files changed, 6 insertions(+) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index cca31641..66f1cddc 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -46,6 +46,8 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v4 + - name: Create app config file + run: cp app-sample.env app.env - name: Run All Tests run: go test ./... -timeout 99999s diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 4635d516..621b0a3c 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -46,6 +46,8 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v4 + - name: Create app config file + run: cp app-sample.env app.env - name: Run All Tests run: go test ./... -timeout 99999s diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 94bc0c8c..49aea6d3 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -46,6 +46,8 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v4 + - name: Create app config file + run: cp app-sample.env app.env - name: Run All Tests run: go test ./... -timeout 99999s From 0039dfbde477205b3bd64b1f76d5d7c52a30d1be Mon Sep 17 00:00:00 2001 From: Nwokoye Chigozie <24975761+NwokoyeChigozie@users.noreply.github.com> Date: Sun, 21 Jul 2024 20:14:03 +0100 Subject: [PATCH 5/9] Revert "feat: add fetch customers list" --- .gitignore | 2 -- internal/models/seed/seed.go | 20 ++++++++-------- internal/models/user.go | 12 ---------- pkg/controller/user/user.go | 14 ----------- pkg/middleware/authorize.go | 23 +------------------ pkg/middleware/jwttoken.go | 2 -- .../storage/postgresql/connection.go | 2 +- pkg/router/organisation.go | 2 +- pkg/router/user.go | 2 -- services/user/user.go | 19 --------------- 10 files changed, 12 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index df7969d6..42092c91 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,3 @@ _ignore/ app.env tmp/ .idea/ - -app.env \ No newline at end of file diff --git a/internal/models/seed/seed.go b/internal/models/seed/seed.go index c2497e75..728904a4 100644 --- a/internal/models/seed/seed.go +++ b/internal/models/seed/seed.go @@ -15,11 +15,10 @@ func SeedDatabase(db *gorm.DB) { Userid1 := utility.GenerateUUID() user1 := models.User{ - ID: Userid1, - Name: "John Doe", - Email: "john@example.com", + ID: Userid1, + Name: "John Doe", + Email: "john@example.com", Password: utility.RandomString(20), - Role: "customer", Profile: models.Profile{ ID: utility.GenerateUUID(), FirstName: "John", @@ -35,11 +34,10 @@ func SeedDatabase(db *gorm.DB) { Userid2 := utility.GenerateUUID() user2 := models.User{ - ID: Userid2, - Name: "Jane Doe", + ID: Userid2, + Name: "Jane Doe", Password: utility.RandomString(20), - Email: "jane@example.com", - Role: "customer", + Email: "jane@example.com", Profile: models.Profile{ ID: utility.GenerateUUID(), FirstName: "Jane", @@ -54,9 +52,9 @@ func SeedDatabase(db *gorm.DB) { } organisations := []models.Organisation{ - {ID: utility.GenerateUUID(), Name: "Org1", Email: fmt.Sprintf(utility.RandomString(4) + "@email.com"), Description: "Description1", OwnerID: Userid1}, - {ID: utility.GenerateUUID(), Name: "Org2", Email: fmt.Sprintf(utility.RandomString(4) + "@email.com"), Description: "Description2", OwnerID: Userid1}, - {ID: utility.GenerateUUID(), Name: "Org3", Email: fmt.Sprintf(utility.RandomString(4) + "@email.com"), Description: "Description3", OwnerID: Userid2}, + {ID: utility.GenerateUUID(), Name: "Org1", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description1", OwnerID: Userid1}, + {ID: utility.GenerateUUID(), Name: "Org2", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description2", OwnerID: Userid1}, + {ID: utility.GenerateUUID(), Name: "Org3", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description3", OwnerID: Userid2}, } var existingUser models.User diff --git a/internal/models/user.go b/internal/models/user.go index cf63b5d6..b9f26a1a 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -13,8 +13,6 @@ type User struct { Name string `gorm:"column:name; type:varchar(255)" json:"name"` Email string `gorm:"column:email; type:varchar(255)" json:"email"` Password string `gorm:"column:password; type:text; not null" json:"-"` - Role string `gorm:"column:role; type:varchar(255)" json:"role"` - IsActive bool `gorm:"column:is_active; type:boolean" json:"is_active"` Profile Profile `gorm:"foreignKey:Userid;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"profile"` Organisations []Organisation `gorm:"many2many:user_organisations;;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"organisations" ` // many to many relationship Products []Product `gorm:"foreignKey:OwnerID" json:"products"` @@ -29,7 +27,6 @@ type CreateUserRequestModel struct { LastName string `json:"last_name" validate:"required"` UserName string `json:"username" validate:"required"` PhoneNumber string `json:"phone_number"` - Role string `json:"role"` } type LoginRequestModel struct { @@ -68,12 +65,3 @@ func (u *User) CreateUser(db *gorm.DB) error { return nil } - -func (u *User) GetAllCustomers(db *gorm.DB) ([]User, error) { - var users []User - if err := db.Preload("Profile").Preload("Products").Preload("Organisations").Where("role = ?", "customer").Find(&users).Error; err != nil { - return users, err - } - - return users, nil -} diff --git a/pkg/controller/user/user.go b/pkg/controller/user/user.go index fc7239fa..5cefd51f 100644 --- a/pkg/controller/user/user.go +++ b/pkg/controller/user/user.go @@ -92,17 +92,3 @@ func (base *Controller) LoginUser(c *gin.Context) { rd := utility.BuildSuccessResponse(http.StatusOK, "user login successfully", respData) c.JSON(http.StatusOK, rd) } - -func (base *Controller) GetAllCustomers(c *gin.Context) { - respData, err := user.GetAllCustomers(base.Db.Postgresql) - if err != nil { - rd := utility.BuildErrorResponse(400, "error", err.Error(), err, nil) - c.JSON(http.StatusBadRequest, rd) - return - } - - base.Logger.Info("All Customers fetched successfully") - - rd := utility.BuildSuccessResponse(http.StatusOK, "All Customers fetched successfully", respData) - c.JSON(http.StatusOK, rd) -} diff --git a/pkg/middleware/authorize.go b/pkg/middleware/authorize.go index 7acfee75..f4fa4ce7 100644 --- a/pkg/middleware/authorize.go +++ b/pkg/middleware/authorize.go @@ -10,7 +10,7 @@ import ( "github.com/hngprojects/hng_boilerplate_golang_web/utility" ) -func Authorize(requiredRoles ...string) gin.HandlerFunc { +func Authorize() gin.HandlerFunc { return func(c *gin.Context) { var tokenStr string bearerToken := c.GetHeader("Authorization") @@ -49,27 +49,6 @@ func Authorize(requiredRoles ...string) gin.HandlerFunc { return } - // Check user role - userRole, ok := claims["user_role"].(string) - if !ok { - c.AbortWithStatusJSON(http.StatusForbidden, utility.BuildErrorResponse(http.StatusForbidden, "error", "Forbidden", "Unauthorized", nil)) - return - } - - // Check if user role is in the list of required roles - roleAuthorized := false - for _, role := range requiredRoles { - if userRole == role { - roleAuthorized = true - break - } - } - - if !roleAuthorized { - c.AbortWithStatusJSON(http.StatusForbidden, utility.BuildErrorResponse(http.StatusForbidden, "error", "Forbidden", "Unauthorized", nil)) - return - } - // store user claims in Context // for accesiblity in controller diff --git a/pkg/middleware/jwttoken.go b/pkg/middleware/jwttoken.go index 9c5bb9e0..9a9269d4 100644 --- a/pkg/middleware/jwttoken.go +++ b/pkg/middleware/jwttoken.go @@ -20,12 +20,10 @@ func CreateToken(user models.User) (string, time.Time, error) { //create token userid := user.ID - userRole := user.Role userClaims := jwt.MapClaims{} // specify user claims userClaims["user_id"] = userid - userClaims["user_role"] = userRole userClaims["exp"] = UnixExp userClaims["authorised"] = true diff --git a/pkg/repository/storage/postgresql/connection.go b/pkg/repository/storage/postgresql/connection.go index e37ae8c5..feeb5f5e 100644 --- a/pkg/repository/storage/postgresql/connection.go +++ b/pkg/repository/storage/postgresql/connection.go @@ -44,7 +44,7 @@ func connectToDb(host, user, password, dbname, port, sslmode, timezone string, l port = detectedPort } - dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=%v TimeZone=%v prefer_simple_protocol=true", host, user, password, dbname, port, sslmode, timezone) + dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=%v TimeZone=%v", host, user, password, dbname, port, sslmode, timezone) newLogger := lg.New( log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer diff --git a/pkg/router/organisation.go b/pkg/router/organisation.go index 4c5a80de..0a25a984 100644 --- a/pkg/router/organisation.go +++ b/pkg/router/organisation.go @@ -17,7 +17,7 @@ func Organisation(r *gin.Engine, ApiVersion string, validator *validator.Validat extReq := request.ExternalRequest{Logger: logger, Test: false} organisation := organisation.Controller{Db: db, Validator: validator, Logger: logger, ExtReq: extReq} - organisationUrl := r.Group(fmt.Sprintf("%v", ApiVersion), middleware.Authorize("customer", "admin")) + organisationUrl := r.Group(fmt.Sprintf("%v", ApiVersion), middleware.Authorize()) { organisationUrl.POST("/organisations", organisation.CreateOrganisation) diff --git a/pkg/router/user.go b/pkg/router/user.go index fd016c55..208030e0 100644 --- a/pkg/router/user.go +++ b/pkg/router/user.go @@ -8,7 +8,6 @@ import ( "github.com/hngprojects/hng_boilerplate_golang_web/external/request" "github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/user" - "github.com/hngprojects/hng_boilerplate_golang_web/pkg/middleware" "github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" "github.com/hngprojects/hng_boilerplate_golang_web/utility" ) @@ -21,7 +20,6 @@ func User(r *gin.Engine, ApiVersion string, validator *validator.Validate, db *s { userUrl.POST("/users/signup", user.CreateUser) userUrl.POST("/users/login", user.LoginUser) - userUrl.GET("/customers", middleware.Authorize("admin"), user.GetAllCustomers) } return r } diff --git a/services/user/user.go b/services/user/user.go index 0051ff64..40ba39c9 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -71,7 +71,6 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err username = strings.ToLower(req.UserName) phoneNumber = req.PhoneNumber password = req.Password - role = req.Role responseData gin.H ) @@ -80,16 +79,11 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err return nil, http.StatusInternalServerError, err } - if role == "" { - role = "customer" - } - user := models.User{ ID: utility.GenerateUUID(), Name: username, Email: email, Password: password, - Role: role, Profile: models.Profile{ ID: utility.GenerateUUID(), FirstName: firstName, @@ -114,7 +108,6 @@ func CreateUser(req models.CreateUserRequestModel, db *gorm.DB) (gin.H, int, err "first_name": user.Profile.FirstName, "last_name": user.Profile.LastName, "phone": user.Profile.Phone, - "role": user.Role, "expires_in": expiry, "access_token": token, } @@ -156,21 +149,9 @@ func LoginUser(req models.LoginRequestModel, db *gorm.DB) (gin.H, int, error) { "first_name": userData.Profile.FirstName, "last_name": userData.Profile.LastName, "phone": userData.Profile.Phone, - "role": userData.Role, "expires_in": expiry, "access_token": token, } return responseData, http.StatusCreated, nil } - -func GetAllCustomers(db *gorm.DB) ([]models.User, error) { - var users models.User - - userResp, err := users.GetAllCustomers(db) - if err != nil { - return userResp, err - } - - return userResp, err -} From 73a9dd71af8f9453738a0e6ac54d861eafdf2312 Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 21 Jul 2024 20:45:48 +0100 Subject: [PATCH 6/9] bugfix: deployment script not editing script --- .github/workflows/development.yml | 4 ++-- .github/workflows/production.yml | 4 ++-- .github/workflows/staging.yml | 4 ++-- scripts/deploy_app.sh | 17 +++++++++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 66f1cddc..227fea0c 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -6,7 +6,7 @@ on: - dev pull_request: branches: - - dev + - dev jobs: build: @@ -75,4 +75,4 @@ jobs: port: ${{ env.SSH_PORT }} script: | cd ~/deployments/development - bash ./scripts/deploy_app.sh development SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + bash ./scripts/deploy_app.sh development https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 621b0a3c..ade9d536 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -6,7 +6,7 @@ on: - main pull_request: branches: - - main + - main jobs: build: @@ -75,4 +75,4 @@ jobs: port: ${{ env.SSH_PORT }} script: | cd ~/deployments/production - bash ./scripts/deploy_app.sh production SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + bash ./scripts/deploy_app.sh production https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 49aea6d3..2bd91228 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -6,7 +6,7 @@ on: - staging pull_request: branches: - - staging + - staging jobs: build: @@ -75,4 +75,4 @@ jobs: port: ${{ env.SSH_PORT }} script: | cd ~/deployments/staging - bash ./scripts/deploy_app.sh staging SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} + bash ./scripts/deploy_app.sh staging https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/scripts/deploy_app.sh b/scripts/deploy_app.sh index 06a8c9e6..19edaf36 100644 --- a/scripts/deploy_app.sh +++ b/scripts/deploy_app.sh @@ -51,11 +51,20 @@ fi # Replace environment variables in app.env cp app-sample.env app.env -for VAR in "$@" +for var in "$@" do - KEY=$(echo $VAR | cut -d '=' -f 1) - VALUE=$(echo $VAR | cut -d '=' -f 2) - sed -i "s|\${env.$KEY}|$VALUE|g" app.env + # Split the variable into key and value + KEY=${var%%=*} + VALUE=${var#*=} + + # Check if the key already exists in the file + if grep -q "^$KEY=" app.env; then + # Update the existing key with the new value + sed -i "s|^$KEY=.*|$KEY=$VALUE|" app.env + else + # Add the new key-value pair + echo "$KEY=$VALUE" >> app.env + fi done go build -o $APPROOT/$APP_NAME From 6b469c56b8648dfc90f1d38ee210d6bffdf014a4 Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 21 Jul 2024 21:02:13 +0100 Subject: [PATCH 7/9] bugfix: update workflow to use deploy script --- .github/workflows/development.yml | 14 +++++++++++++- .github/workflows/production.yml | 14 +++++++++++++- .github/workflows/staging.yml | 14 +++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 227fea0c..7c13bff8 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -74,5 +74,17 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - cd ~/deployments/development + export APPROOT=~/deployments/development + mkdir -p $APPROOT + cd $APPROOT + + if [ -d "$APPROOT/.git" ]; then + # Navigate to the repository directory and pull changes + cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } + git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } + git pull origin development || { echo "Failed to pull latest changes"; exit 1; } + else + git clone -b development https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } + fi + bash ./scripts/deploy_app.sh development https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index ade9d536..0815ffad 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -74,5 +74,17 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - cd ~/deployments/production + export APPROOT=~/deployments/production + mkdir -p $APPROOT + cd $APPROOT + + if [ -d "$APPROOT/.git" ]; then + # Navigate to the repository directory and pull changes + cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } + git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } + git pull origin production || { echo "Failed to pull latest changes"; exit 1; } + else + git clone -b production https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } + fi + bash ./scripts/deploy_app.sh production https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 2bd91228..672235f5 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -74,5 +74,17 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - cd ~/deployments/staging + export APPROOT=~/deployments/staging + mkdir -p $APPROOT + cd $APPROOT + + if [ -d "$APPROOT/.git" ]; then + # Navigate to the repository directory and pull changes + cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } + git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } + git pull origin staging || { echo "Failed to pull latest changes"; exit 1; } + else + git clone -b staging https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } + fi + bash ./scripts/deploy_app.sh staging https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} From 03043ba8a841e3740a9e7cfc64859e89c2a9da52 Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 21 Jul 2024 21:07:28 +0100 Subject: [PATCH 8/9] bugfix: update workflow to use the right branch names --- .github/workflows/development.yml | 4 ++-- .github/workflows/production.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 7c13bff8..95ea822f 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -82,9 +82,9 @@ jobs: # Navigate to the repository directory and pull changes cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin development || { echo "Failed to pull latest changes"; exit 1; } + git pull origin dev || { echo "Failed to pull latest changes"; exit 1; } else - git clone -b development https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } + git clone -b dev https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } fi bash ./scripts/deploy_app.sh development https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 0815ffad..000964d1 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -82,9 +82,9 @@ jobs: # Navigate to the repository directory and pull changes cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin production || { echo "Failed to pull latest changes"; exit 1; } + git pull origin main || { echo "Failed to pull latest changes"; exit 1; } else - git clone -b production https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } + git clone -b main https://github.com/${{ github.repository}} . || { echo "Failed to clone repository"; exit 1; } fi bash ./scripts/deploy_app.sh production https://github.com/${{ github.repository}} SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} From 0c510e7ecc36eac0d15e1e41a5e4fa30deabf501 Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 21 Jul 2024 21:42:06 +0100 Subject: [PATCH 9/9] chore: set migrate to trueon the app-sample.env --- app-sample.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-sample.env b/app-sample.env index c0f0fa28..f0383469 100644 --- a/app-sample.env +++ b/app-sample.env @@ -19,7 +19,7 @@ SSLMODE=disable USERNAME=postgres PASSWORD=password DB_NAME=db_name -MIGRATE=false +MIGRATE=true # Test # TEST_DB_HOST=localhost