Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoApp committed Nov 11, 2024
1 parent e601a36 commit 8591365
Show file tree
Hide file tree
Showing 140 changed files with 27,026 additions and 64 deletions.
2 changes: 0 additions & 2 deletions .devcontainer/Dockerfile

This file was deleted.

19 changes: 2 additions & 17 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
{
"name": "GCP playground",
"image": "mcr.microsoft.com/vscode/devcontainers/base:0-ubuntu-20.04",
"features": {
"ghcr.io/devcontainers/features/sshd:1": {
"version": "latest"
}
},
"settings": {
"extensions.ignoreRecommendations": true,
"workbench.startupEditor": "none",
"workbench.colorTheme": "Visual Studio Dark",
"workbench.colorCustomizations": {},
"workbench.welcomePage.walkthroughs.openOnInstall": false,
"workbench.welcomePage.experimental.videoTutorials": "off",
"github.codespaces.defaultExtensions": []
},
"postStartCommand": "bash -c .devcontainer/setup.sh"
"name": "Anythink Development Container",
"image": "public.ecr.aws/v0a2l7y2/wilco/anythink-devcontainer:latest"
}
20 changes: 16 additions & 4 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ export ENGINE_EVENT_ENDPOINT="${ENGINE_BASE_URL}/users/${WILCO_ID}/event"
# Update engine that codespace started for user
curl -L -X POST "${ENGINE_EVENT_ENDPOINT}" -H "Content-Type: application/json" --data-raw "{ \"event\": \"github_codespace_started\" }"

echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update
sudo apt-get install -y google-cloud-sdk
# Export backend envs when in codespaces
echo "export CODESPACE_BACKEND_HOST=\"${CODESPACE_BACKEND_HOST}\"" >> ~/.bashrc
echo "export CODESPACE_BACKEND_URL=\"${CODESPACE_BACKEND_URL}\"" >> ~/.bashrc
echo "export CODESPACE_WDS_SOCKET_PORT=443" >> ~/.bashrc

# Export welcome prompt in bash:
echo "printf \"\n\n☁️☁️☁️️ Anythink: Develop in the Cloud ☁️☁️☁️\n\"" >> ~/.bashrc
echo "printf \"\n\x1b[31m \x1b[1m👉 Type: \\\`docker compose up\\\` to run the project. 👈\n\n\"" >> ~/.bashrc

nohup bash -c "cd /wilco-agent && node agent.js &" >> /tmp/agent.log 2>&1

# Check if docker is installed
if command -v docker &> /dev/null
then
docker compose pull
fi
3 changes: 3 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
157 changes: 157 additions & 0 deletions .github/workflows/k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Build and deploy to Kubernetes
on:
push:
branches:
- main

concurrency:
group: k8s
cancel-in-progress: true

jobs:
check-kubernetes-enabled:
runs-on: ubuntu-20.04
outputs:
kubernetes-enabled: ${{ steps.kubernetes-flag-defined.outputs.DEFINED }}
steps:
- id: kubernetes-flag-defined
if: "${{ env.ENABLE_KUBERNETES != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
ENABLE_KUBERNETES: ${{ secrets.ENABLE_KUBERNETES }}

check-secret:
runs-on: ubuntu-20.04
needs: [check-kubernetes-enabled]
outputs:
aws-creds-defined: ${{ steps.aws-creds-defined.outputs.DEFINED }}
kubeconfig-defined: ${{ steps.kubeconfig-defined.outputs.DEFINED }}
if: needs.check-kubernetes-enabled.outputs.kubernetes-enabled == 'true'
steps:
- id: aws-creds-defined
if: "${{ env.AWS_ACCESS_KEY_ID != '' && env.AWS_SECRET_ACCESS_KEY != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

- id: kubeconfig-defined
if: "${{ env.KUBECONFIG != '' }}"
run: echo "DEFINED=true" >> $GITHUB_OUTPUT
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}

build-backend:
name: Build backend image
runs-on: ubuntu-20.04
needs: [check-secret]
if: needs.check-secret.outputs.aws-creds-defined == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3

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

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV

- name: Set repository name
run: |
if [ ${{ secrets.CLUSTER_ENV }} == 'staging' ]; then
echo "REPO_NAME=staging-anythink-backend" >> $GITHUB_ENV
else
echo "REPO_NAME=anythink-backend" >> $GITHUB_ENV
fi
- name: Build, tag, and push backend image to Amazon ECR
id: build-image-backend
run: |
docker build \
-t ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \
-f backend/Dockerfile.aws \
.
docker push ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
build-frontend:
name: Build frontend images
runs-on: ubuntu-20.04
needs: [check-secret]
if: needs.check-secret.outputs.aws-creds-defined == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3

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

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV

- name: Set repository name
run: |
if [ ${{ secrets.CLUSTER_ENV }} == 'staging' ]; then
echo "REPO_NAME=staging-anythink-frontend" >> $GITHUB_ENV
else
echo "REPO_NAME=anythink-frontend" >> $GITHUB_ENV
fi
- name: Build, tag, and push frontend image to Amazon ECR
id: build-image-frontend
run: |
docker build \
-t ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }} \
-f frontend/Dockerfile.aws \
.
docker push ${{ steps.login-ecr.outputs.registry }}/${{ env.REPO_NAME }}:${{ env.IMAGE_TAG }}
deploy:
name: Deploy latest tag using helm
runs-on: ubuntu-20.04
if: needs.check-secret.outputs.kubeconfig-defined == 'true'
needs:
- build-frontend
- build-backend
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Create kube config
run: |
mkdir -p $HOME/.kube/
echo "${{ secrets.KUBECONFIG }}" > $HOME/.kube/config
chmod 600 $HOME/.kube/config
- name: Install helm
run: |
curl -LO https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz
tar -zxvf helm-v3.8.0-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
helm version
- name: Lint helm charts
run: helm lint ./charts/

- name: Set the image tag
run: echo IMAGE_TAG=${GITHUB_REPOSITORY/\//-}-latest >> $GITHUB_ENV

- name: Deploy
run: |
helm upgrade --install --timeout 10m anythink-market ./charts/ \
--set clusterEnv=${{ secrets.CLUSTER_ENV }} \
--set frontend.image.tag=${{ env.IMAGE_TAG }} \
--set backend.image.tag=${{ env.IMAGE_TAG }}
10 changes: 10 additions & 0 deletions .github/workflows/wilco-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ jobs:
- name: Check out project
uses: actions/checkout@v2

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: "16"

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.6.0
with:
mongodb-version: "4.4"

- uses: oNaiPs/secrets-to-env-action@v1
with:
secrets: ${{ toJSON(secrets) }}
Expand Down
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
/.idea
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/backend/node_modules
/frontend/node_modules
/.wilco-helpers/node_modules
/tests/e2e/node_modules
/tests/frontend/node_modules/
/tests/frontend/test-results/
/tests/frontend/playwright-report/
/tests/frontend/playwright/.cache/

/.pnp
.pnp.js

# testing
/coverage

# production
/backend/build
/frontend/build

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

#IDEs
/.idea/
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"workbench.startupEditor": "none"
}
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

37 changes: 37 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Logs
logs
*.log
.DS_Store

npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

.idea
10 changes: 10 additions & 0 deletions backend/Dockerfile.aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:16

WORKDIR /usr/src
COPY backend ./backend
COPY .wilco ./.wilco

# Pre-install npm packages
WORKDIR /usr/src/backend
RUN yarn install

22 changes: 22 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Anythink Market Backend

The Anythink Market backend is Node web app written with [Express](https://expressjs.com/)

## Dependencies

- [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) - For generating JWTs used by authentication
- [mongoose](https://github.com/Automattic/mongoose) - For modeling and mapping MongoDB data to javascript
- [mongoose-unique-validator](https://github.com/blakehaswell/mongoose-unique-validator) - For handling unique validation errors in Mongoose. Mongoose only handles validation at the document level, so a unique index across a collection will throw an exception at the driver level. The `mongoose-unique-validator` plugin helps us by formatting the error like a normal mongoose `ValidationError`.
- [passport](https://github.com/jaredhanson/passport) - For handling user authentication
- [slug](https://github.com/dodo/node-slug) - For encoding titles into a URL-friendly format

## Application Structure

- `app.js` - The entry point to our application. This file defines our express server and connects it to MongoDB using mongoose. It also requires the routes and models we'll be using in the application.
- `config/` - This folder contains configuration for passport as well as a central location for configuration/environment variables.
- `routes/` - This folder contains the route definitions for our API.
- `models/` - This folder contains the schema definitions for our Mongoose models.

## Error Handling

In `routes/api/index.js`, we define a error-handling middleware for handling Mongoose's `ValidationError`. This middleware will respond with a 422 status code and format the response to have [error messages the clients can understand](https://github.com/gothinkster/realworld/blob/master/API.md#errors-and-status-codes)
Loading

0 comments on commit 8591365

Please sign in to comment.