Skip to content

Commit

Permalink
Merge pull request #105 from COS301-SE-2024/fix/backend/reorganizing-…
Browse files Browse the repository at this point in the history
…env-environments

Fix/backend/reorganizing env environments
  • Loading branch information
waveyboym authored Jun 27, 2024
2 parents 206c3ea + ace60cb commit 7471b8e
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 100 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/deploy-golang-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ jobs:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Decrypt env variables
- name: Decrypt default variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .dev.env.gpg > .dev.env
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/config.yaml.gpg > configs/config.yaml
- name: Decrypt test variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/dev.deployed.yaml.gpg > configs/dev.deployed.yaml
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/deploy-golang-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ jobs:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Decrypt env variables
- name: Decrypt default variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .prod.env.gpg > .prod.env
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/config.yaml.gpg > configs/config.yaml
- name: Decrypt test variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/prod.yaml.gpg > configs/prod.yaml
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/lint-test-build-golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ jobs:
with:
go-version: '1.21' # Specify the Go version you are using

- name: Decrypt env variables
- name: Decrypt default variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .test.env.gpg > .test.env
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/config.yaml.gpg > configs/config.yaml
- name: Decrypt test variables
run: |
echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 configs/test.yaml.gpg > configs/test.yaml
- name: Run tests
run: |
Expand Down
2 changes: 0 additions & 2 deletions occupi-backend/.dev.env.gpg

This file was deleted.

Binary file removed occupi-backend/.env.gpg
Binary file not shown.
Binary file removed occupi-backend/.prod.env.gpg
Binary file not shown.
Binary file removed occupi-backend/.test.env.gpg
Binary file not shown.
5 changes: 1 addition & 4 deletions occupi-backend/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download && go mod verify

# copy .dev.env file
COPY .dev.env .env

# Copy the source code into the container
COPY . .

# Build the Go application
RUN go build -o occupi-backend ./cmd/occupi-backend
RUN go build -o occupi-backend ./cmd/occupi-backend -env=dev.deployed

# Expose the port the app runs on
EXPOSE 8081
Expand Down
5 changes: 1 addition & 4 deletions occupi-backend/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download && go mod verify

# copy .prod.env file
COPY .prod.env .env

# Copy the source code into the container
COPY . .

# Build the Go application
RUN go build -o occupi-backend ./cmd/occupi-backend
RUN go build -o occupi-backend ./cmd/occupi-backend -env=prod

# Expose the port the app runs on
EXPOSE 8080
Expand Down
Binary file removed occupi-backend/cert.pem.gpg
Binary file not shown.
31 changes: 16 additions & 15 deletions occupi-backend/cmd/occupi-backend/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package main

import (
"fmt"
"log"
"flag"

"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"

"github.com/COS301-SE-2024/occupi/occupi-backend/configs"
Expand All @@ -17,10 +15,12 @@ import (

// occupi backend entry point
func main() {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
log.Fatal(fmt.Printf("Error loading .env file with error as %s", err))
}
// Define the environment flag
env := flag.String("env", "dev.localhost", "Environment to use (dev.localhost, dev.deployed, prod)")
flag.Parse()

// init viper
configs.InitViper(env)

// setup logger to log all server interactions
utils.SetupLogger()
Expand Down Expand Up @@ -49,19 +49,20 @@ func main() {
certFile := configs.GetCertFileName()
keyFile := configs.GetKeyFileName()

// fatal error if the cert or key file is not found
if certFile == "CERTIFICATE_FILE_PATH" || keyFile == "KEY_FILE_PATH" {
logrus.Fatal("Cert or Key file not found")
}

// logrus all env variables
logrus.Infof("Server running on port: %s", configs.GetPort())
logrus.Infof("Server running in %s mode", configs.GetGinRunMode())
logrus.Infof("Server running with cert file: %s", certFile)
logrus.Infof("Server running with key file: %s", keyFile)

// Listening on the port with TLS
if err := ginRouter.RunTLS(":"+configs.GetPort(), certFile, keyFile); err != nil {
logrus.Fatal("Failed to run server: ", err)
// Listening on the port with TLS if env is prod or dev.deployed
if configs.GetEnv() == "prod" || configs.GetEnv() == "dev.deployed" {
if err := ginRouter.RunTLS(":"+configs.GetPort(), certFile, keyFile); err != nil {
logrus.Fatal("Failed to run server: ", err)
}
} else {
if err := ginRouter.Run(":" + configs.GetPort()); err != nil {
logrus.Fatal("Failed to run server: ", err)
}
}
}
1 change: 1 addition & 0 deletions occupi-backend/configs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yaml
Loading

0 comments on commit 7471b8e

Please sign in to comment.