Skip to content

Commit

Permalink
Create a common config to read env values & logger package (#39)
Browse files Browse the repository at this point in the history
* add logger package

* creat a config package to read all env variables from

* add cases for tests in core config

* fix integrations tests not running on mac

* add command to display env

* print current dir

* add a test.env file

* add env to go test command

* add env and verbose to test workflow

* remove whitespace

* add postgres to test workflow (#40)

* add postgres to test workflow

* revert workflow name

* break unit and integration test into separate commands (#41)

* createa a release docker file and update the listener port to 0.0.0.0

* create a release docker file (#42)

* createa a release docker file and update the listener port to 0.0.0.0

* add comments to release docker file

* take build args in the release docker file

* remove build args from release docker & create a health check route

* change the route group prefix from v1 to wisee/v1 (#43)

* change the route group prefix from v1 to wisee/v1

* update the workflow to deploy to ecs

* add new branch

* reduce docker image size

* chore: add platform

* update go arch and goos

* remove unused comment

* set wait for service stability to false

* add error log in health check route

* add workflow dispatch

* remove revision file

* change trigger from dev to develop

* make a get call to google.com

* remove get call to google

* clean up verify token function and add a issued at time (#45)

* clean up verify token function and add a issued at time

* change jwt validity to days from hours

* make a get call to google.com

* remove ping to google in health check

---------

Co-authored-by: Prakash <om.pcprakash@gmail.com>

---------

Co-authored-by: Prakash <om.pcprakash@gmail.com>

* log jwt validity in unit tests

* update dev and test env

* uncommment integration tests

---------

Co-authored-by: Prakash <om.pcprakash@gmail.com>
  • Loading branch information
yesyash and prakashchoudhary07 authored Apr 7, 2024
1 parent f706bb5 commit f88cc70
Show file tree
Hide file tree
Showing 23 changed files with 550 additions and 176 deletions.
32 changes: 32 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
67 changes: 67 additions & 0 deletions .github/workflows/release-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Deploy to Staging

on:
workflow_dispatch:
push:
branches:
- develop
- yash/group

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: staging

env:
AWS_REGION: ${{ vars.AWS_REGION }}
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }}
ECS_SERVICE: ${{ vars.ECS_SERVICE }}
ECS_CLUSTER: ${{ vars.ECS_CLUSTER }}
ECS_TASK_DEFINITION: ${{ vars.ECS_TASK_DEFINITION }}
CONTAINER_NAME: ${{ vars.CONTAINER_NAME }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition wisee-backend --query taskDefinition > task-definition.json
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -f docker/release.dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: false
26 changes: 22 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
name: Test
name: Tests

on:
pull_request:
branches: ["*"]

jobs:
unit-tests:
tests:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: wisee_core_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
name: Run tests
steps:
Expand All @@ -23,7 +38,10 @@ jobs:
- run: go version

- name: Copy Env
run: cp ./environments/dev.env .env
run: cp ./environments/test.env .env

- name: Run Unit tests
run: go test ./tests/unit
run: make test_unit

- name: Run Integration tests
run: make test_integration
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ ifeq ($(ARCH),x86_64)
ARCH := amd64
else ifeq ($(ARCH),i386)
ARCH := 386
else ifeq ($(ARCH),aarch64)
ARCH := arm64
endif

build:
@echo "Building $(OS) $(ARCH) binary..."
@GOOS=$(OS) GOARCH=$(ARCH) go build $(ARGS) -o "bin/$(BINARY_NAME)" src/main.go
@GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(ARGS) -o "bin/$(BINARY_NAME)" ./src

test:
test_unit:
@echo "Running tests..."
@@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -race -covermode=atomic -v -coverpkg=./src/... ./tests/... ./src/...
@@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -race -covermode=atomic -v -coverpkg=./src/... ./tests/unit/... ./src/...

test_integration:
@echo "Running tests..."
@@GOOS=$(OS) GOARCH=$(ARCH) ENV=test go test -race -covermode=atomic -v -coverpkg=./src/... ./tests/integration/... ./src/...

test: test_unit test_integration

clean:
@echo "Cleaning..."
Expand All @@ -51,7 +59,7 @@ watch:
# Up all migrations
migrate-all-up:
@if command -v migrate > /dev/null; then \
migrate -path ./migrations -database $(DEV_DATABASE_URL) -path ./database/migrations up; \
migrate -database $(DEV_DATABASE_URL) -path ./database/migrations up; \
else \
echo "Golang Migrate cli is not installed on your machine. Exiting..."; \
exit 1; \
Expand All @@ -60,7 +68,7 @@ migrate-all-up:
# Drop all migrations when in development
migrate-all-down:
@if command -v migrate > /dev/null; then \
migrate -path ./migrations -database $(DEV_DATABASE_URL) -path ./database/migrations down; \
migrate -database $(DEV_DATABASE_URL) -path ./database/migrations down; \
else \
echo "Golang Migrate cli is not installed on your machine. Exiting..."; \
exit 1; \
Expand Down
57 changes: 57 additions & 0 deletions docker/release.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Create a stage for building the application.
ARG GO_VERSION=1.21.0
FROM golang:${GO_VERSION} AS build
WORKDIR /src

# Download dependencies as a separate step to take advantage of Docker's caching.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x

# This is the architecture you’re building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETARCH

# Build the application.
# Leverage a cache mount to /go/pkg/mod/ to speed up subsequent builds.
# Leverage a bind mount to the current directory to avoid having to copy the
# source code into the container.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,target=. \
GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o /bin/server ./src

################################################################################
# Create a new stage for running the application that contains the minimal
FROM alpine:latest AS final

# Install any runtime dependencies that are needed to run your application.
# Leverage a cache mount to /var/cache/apk/ to speed up subsequent builds.
RUN --mount=type=cache,target=/var/cache/apk \
apk --update add \
ca-certificates \
tzdata \
&& \
update-ca-certificates

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
ENTRYPOINT [ "/bin/server" ]
3 changes: 2 additions & 1 deletion environments/dev.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ENV="dev"
JWT_SECRET="secret"
JWT_VALIDITY_IN_HOURS=24
JWT_VALIDITY_IN_DAYS=1
JWT_ISSUER="wisee-backend"

DOMAIN="localhost"
Expand Down
15 changes: 15 additions & 0 deletions environments/test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ENV="test"
JWT_SECRET="secret"
JWT_VALIDITY_IN_DAYS=1
JWT_ISSUER="wisee-backend"

DOMAIN="localhost"
AUTH_REDIRECT_URL="http://localhost:3000/dashboard"

DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core?sslmode=disable"
TEST_DB_URL="postgresql://postgres:postgres@localhost:5432/wisee_core_test?sslmode=disable"
DB_MAX_OPEN_CONNECTIONS=10

GOOGLE_CLIENT_ID="google-client-id"
GOOGLE_CLIENT_SECRET="google-client-secret"
GOOGLE_REDIRECT_URL="http://localhost:8080/v1/auth/google/callback"
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/uptrace/bun v1.1.14
github.com/uptrace/bun/dialect/pgdialect v1.1.14
github.com/uptrace/bun/driver/pgdriver v1.1.14
Expand All @@ -16,11 +17,8 @@ require (
)

require (
github.com/google/go-github/v39 v39.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/lib/pq v1.10.9 // indirect
go.uber.org/atomic v1.7.0 // indirect
)

Expand Down Expand Up @@ -55,7 +53,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/stretchr/testify v1.8.4
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
Loading

0 comments on commit f88cc70

Please sign in to comment.