Skip to content

Commit

Permalink
Merge branch 'hngprojects:main' into feat-certbot-config-scrpt
Browse files Browse the repository at this point in the history
  • Loading branch information
Babbexx-22 authored Jul 21, 2024
2 parents 7534d58 + c7e4f56 commit 4e26b66
Show file tree
Hide file tree
Showing 40 changed files with 1,719 additions and 111 deletions.
37 changes: 9 additions & 28 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- dev
pull_request:
branches:
- dev

pull_request:
branches:
Expand Down Expand Up @@ -58,7 +61,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
Expand All @@ -69,30 +76,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 }}
37 changes: 9 additions & 28 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

pull_request:
branches:
Expand Down Expand Up @@ -58,7 +61,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
Expand All @@ -69,30 +76,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 }}
37 changes: 9 additions & 28 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- staging
pull_request:
branches:
- staging

pull_request:
branches:
Expand Down Expand Up @@ -58,7 +61,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
Expand All @@ -69,30 +76,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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ _ignore/
app.env
tmp/
.idea/

app.env
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/hngprojects/hng_boilerplate_golang_web

go 1.19

toolchain go1.22.2

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/didip/tollbooth v4.0.2+incompatible
Expand All @@ -12,10 +14,13 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.22.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/jeanphorn/log4go v0.0.0-20231225120528-d93eb9001e51
github.com/mitchellh/mapstructure v1.5.0
github.com/nyaruka/phonenumbers v1.3.6
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.19.0
golang.org/x/crypto v0.23.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
)
Expand All @@ -41,7 +46,6 @@ require (
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
Expand All @@ -59,7 +63,6 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
37 changes: 33 additions & 4 deletions internal/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package config
import (
"log"

"github.com/hngprojects/hng_boilerplate_golang_web/utility"

"github.com/mitchellh/mapstructure"

"github.com/spf13/viper"

"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

// Setup initialize configuration
Expand All @@ -22,10 +26,20 @@ func Setup(logger *utility.Logger, name string) *Configuration {
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
// remove from fatal to Printf to check env
log.Printf("Error reading config file, %s", err)
log.Printf("Reading from environment variable")

viper.AutomaticEnv()

viper.AutomaticEnv()
var config BaseConfig

// bind config keys to viper
err := BindKeys(viper.GetViper(), config)
if err != nil {
log.Fatalf("Unable to bindkeys in struct, %v", err)
}
}

err := viper.Unmarshal(&baseConfiguration)
if err != nil {
Expand All @@ -44,3 +58,18 @@ func Setup(logger *utility.Logger, name string) *Configuration {
func GetConfig() *Configuration {
return Config
}

func BindKeys(v *viper.Viper, input interface{}) error {

envKeysMap := &map[string]interface{}{}
if err := mapstructure.Decode(input, &envKeysMap); err != nil {
return err
}
for k := range *envKeysMap {
if bindErr := viper.BindEnv(k); bindErr != nil {
return bindErr
}
}

return nil
}
1 change: 1 addition & 0 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (config *BaseConfig) SetupConfigurationn() *Configuration {
exemptFromThrottle := []string{}
json.Unmarshal([]byte(config.TRUSTED_PROXIES), &trustedProxies)
json.Unmarshal([]byte(config.EXEMPT_FROM_THROTTLE), &exemptFromThrottle)

if config.SERVER_PORT == "" {
config.SERVER_PORT = os.Getenv("PORT")
}
Expand Down
16 changes: 16 additions & 0 deletions internal/models/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package migrations

import "github.com/hngprojects/hng_boilerplate_golang_web/internal/models"

func AuthMigrationModels() []interface{} {
return []interface{}{
models.Organisation{},
models.Profile{},
models.Product{},
models.User{},
} // an array of db models, example: User{}
}

func AlterColumnModels() []AlterColumn {
return []AlterColumn{}
}
10 changes: 0 additions & 10 deletions internal/models/migrations/transactions_migrations.go

This file was deleted.

47 changes: 47 additions & 0 deletions internal/models/organisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package models

import (
"time"

"gorm.io/gorm"

"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql"
)

type Organisation struct {
ID string `gorm:"type:uuid;primaryKey;unique;not null" json:"id"`
Name string `gorm:"type:varchar(255);not null" json:"name"`
Description string `gorm:"type:text" json:"description"`
Email string `gorm:"type:varchar(255);unique" json:"email"`
State string `gorm:"type:varchar(255)" json:"state"`
Industry string `gorm:"type:varchar(255)" json:"industry"`
Type string `gorm:"type:varchar(255)" json:"type"`
Address string `gorm:"type:varchar(255)" json:"address"`
Country string `gorm:"type:varchar(255)" json:"country"`
OwnerID string `gorm:"type:uuid;" json:"owner_id"`
Users []User `gorm:"many2many:user_organisations;foreignKey:ID;joinForeignKey:org_id;References:ID;joinReferences:user_id"`
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"`
}

type CreateOrgRequestModel struct {
Name string `json:"name" validate:"required,min=2,max=255"`
Description string `json:"description" `
Email string `json:"email" validate:"required"`
State string `json:"state" validate:"required"`
Industry string `json:"industry" validate:"required"`
Type string `json:"type" validate:"required"`
Address string `json:"address" validate:"required"`
Country string `json:"country" validate:"required"`
}

func (c *Organisation) CreateOrganisation(db *gorm.DB) error {

err := postgresql.CreateOneRecord(db, &c)

if err != nil {
return err
}

return nil
}
12 changes: 12 additions & 0 deletions internal/models/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

import "time"

type Product struct {
ID string `gorm:"type:uuid;primaryKey" json:"product_id"`
Name string `gorm:"column:name; type:varchar(255); not null" json:"name"`
Description string `gorm:"column:description;type:text;" json:"description"`
OwnerID string `gorm:"type:uuid;" json:"owner_id"`
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"`
}
14 changes: 14 additions & 0 deletions internal/models/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package models

import "time"

type Profile struct {
ID string `gorm:"type:uuid;primary_key" json:"profile_id"`
FirstName string `gorm:"column:first_name; type:text; not null" json:"first_name"`
LastName string `gorm:"column:last_name; type:text;not null" json:"last_name"`
Phone string `gorm:"type:varchar(255)" json:"phone"`
AvatarURL string `gorm:"type:varchar(255)" json:"avatar_url"`
Userid string `gorm:"type:uuid;" json:"user_id"`
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"`
}
Loading

0 comments on commit 4e26b66

Please sign in to comment.