Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
9b49a79
update events hello payload
AnatoleAM Jul 28, 2023
3324b8c
terraform
AnatoleAM Jul 28, 2023
3240cd8
validation
AnatoleAM Jul 28, 2023
3d96924
template file
AnatoleAM Jul 28, 2023
f27b529
missing website url
AnatoleAM Jul 28, 2023
9a0ed9c
fix ci cond
AnatoleAM Jul 28, 2023
1aa0f04
formatting errors
AnatoleAM Jul 29, 2023
f65e504
health check optional s3
AnatoleAM Jul 29, 2023
277b742
pass db credentials
AnatoleAM Jul 29, 2023
4b2dffd
fix bad endpoints
AnatoleAM Jul 29, 2023
5fd6efc
stage scaling
AnatoleAM Aug 4, 2023
0efffc0
s3 / rmq
AnatoleAM Aug 10, 2023
db7952b
custom s3
AnatoleAM Aug 12, 2023
09dff99
use production state from infra
AnatoleAM Aug 15, 2023
96243aa
use new tunnel hostname
AnatoleAM Aug 17, 2023
c4dddda
Added NATS to config
broadeditz Aug 24, 2023
45a9d00
Updated modules
broadeditz Aug 24, 2023
76cd24b
Replaced Redis with NATS for events
broadeditz Aug 24, 2023
cd6bf15
Added TODO's
broadeditz Sep 1, 2023
e986e9f
no force ingress target
AnatoleAM Oct 8, 2023
6a4b7ca
Update modules
broadeditz Oct 30, 2023
2d1113d
Set MaxBatch for emote sets
broadeditz Oct 30, 2023
c6dcfe6
Optimize emote sets query
broadeditz Oct 30, 2023
157ffa7
add NATS to terraform
broadeditz Oct 30, 2023
4bcef1b
Merge remote-tracking branch 'origin/chore/terraform' into feat/bread…
broadeditz Oct 30, 2023
b7cd173
comply with lint
broadeditz Oct 30, 2023
0e1b557
comply with lint
broadeditz Oct 30, 2023
ce775eb
bump common repo to newer version
broadeditz Nov 9, 2023
b0e62b3
add caching to emote loader
broadeditz Nov 14, 2023
95673f6
cache emotes as JSON
broadeditz Nov 14, 2023
104b661
comply with lint
broadeditz Nov 14, 2023
81f18a4
fix cache marshaling
broadeditz Nov 14, 2023
05389e3
change emote loader cache to in memory cache
broadeditz Nov 15, 2023
6090dce
comply with lint
broadeditz Nov 15, 2023
996567f
shorten emote cache period
broadeditz Nov 23, 2023
3d616ad
change events dispatch key
broadeditz Nov 23, 2023
7423270
lower resources requested
broadeditz Nov 23, 2023
01fea7c
move jwt secret to terraform state
broadeditz Nov 28, 2023
5c14ca2
remove autoscaling for memory use
broadeditz Nov 28, 2023
63f4823
temporarily disable emote list & search
broadeditz Nov 28, 2023
b7cf8af
temporarily disable lint
broadeditz Nov 28, 2023
d37c7b4
switch to cf tunnel
AnatoleAM Nov 29, 2023
22d2f5a
reduce autoscaler threshold
broadeditz Nov 29, 2023
e7c9770
Merge remote-tracking branch 'origin/feat/bread/nats' into feat/bread…
broadeditz Nov 29, 2023
295adde
Emote search through Meilisearch
broadeditz Nov 29, 2023
eac3b31
add meilisearch to terraform config
broadeditz Nov 29, 2023
8db4cb4
fix import cycle
broadeditz Nov 29, 2023
0546ca0
fix type interface
broadeditz Nov 29, 2023
1cb45bc
fix missing import
broadeditz Nov 29, 2023
3e4af76
add melisearch key terraform variable
broadeditz Nov 29, 2023
ba1c2f4
fix typo
broadeditz Nov 29, 2023
b6e21d3
add temporary logging
broadeditz Nov 29, 2023
bdc3131
fix lifecycle filter
broadeditz Nov 29, 2023
874504f
fix emote id type
broadeditz Nov 29, 2023
b88ad43
change meilisearch limit to hitsperpage
broadeditz Nov 29, 2023
1f1a085
optimize aggregation order
broadeditz Nov 29, 2023
7470f68
log redis error
AnatoleAM Nov 29, 2023
ca5612f
disable emote channels
broadeditz Nov 29, 2023
4a25f5f
change redis config to array of addresses
broadeditz Nov 29, 2023
e345b33
change redis config to array of addresses
broadeditz Nov 29, 2023
1960d1d
disable emote channel re-counting
broadeditz Nov 29, 2023
322deec
fix emote search paging
broadeditz Nov 30, 2023
08b75d4
fix emote search paging
broadeditz Nov 30, 2023
e60ede5
sort emote search results
broadeditz Nov 30, 2023
03fc5f3
api resources limit
AnatoleAM Dec 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 136 additions & 44 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ on:
workflow_dispatch:
inputs:
deploy:
description: "Deploy location"
description: "Which environment to deploy to"
required: true
default: "none"
type: choice
options:
- production
- staging
- prod
- test
- none

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
DEPLOY: ${{ (inputs.deploy != 'none' && inputs.deploy) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'prod') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') && 'prod') || ((github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'test') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) && 'test') || 'none' }}

jobs:
ci:
name: API Lint, Build, Test, Deploy
runs-on: aws-runner
env:
DEPLOY_PROD: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'production') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'master') }}
DEPLOY_STAGE: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy == 'staging') || (github.event_name == 'push' && github.ref_type == 'branch' && github.ref_name == 'dev') || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'staged')) }}
GOLANGCI_LINT_CACHE: /home/runner/.cache/golangci-lint
concurrency:
group: ${{ github.workflow }}-ci-${{ github.ref }}
Expand Down Expand Up @@ -99,7 +100,7 @@ jobs:
run: make generate

- name: Build Dev Portal Frontend
run: make ${{ (env.DEPLOY_PROD == 'true' && 'portal') || 'portal_stage' }}
run: make ${{ (env.DEPLOY == 'prod' && 'portal') || 'portal_stage' }}

- name: Run Linter
run: make lint
Expand All @@ -110,64 +111,155 @@ jobs:
- name: Build App
run: make build

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

- name: Login to Amazon ECR
id: login-ecr
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
uses: aws-actions/amazon-ecr-login@v1

- name: Make build context
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
if: env.DEPLOY != 'none'
run: |
docker context create builders

- name: Setup buildx
uses: docker/setup-buildx-action@v2
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
if: env.DEPLOY != 'none'
with:
install: true
endpoint: builders

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Build docker image
uses: docker/build-push-action@v4
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
if: env.DEPLOY != 'none'
with:
context: .
file: docker/partial.Dockerfile
# cache-from: |
# type=registry,ref=gha
# cache-to: |
# type=registry,ref=gha,mode=max
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/api:latest
${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/api:${{ github.sha }}
ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}
ghcr.io/seventv/api:${{ env.DEPLOY }}-latest
push: true

- name: Update deployment template
uses: danielr1996/envsubst-action@1.1.0
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
env:
IMAGE: ${{ steps.login-ecr.outputs.registry }}/${{ (env.DEPLOY_PROD == 'true' && '7tv') || '7tv-stage' }}/api:${{ github.sha }}
with:
input: k8s/${{ (env.DEPLOY_PROD == 'true' && 'production') || 'staging' }}.template.yaml
output: k8s/deploy.yaml
validate:
name: API Deploy Validation
needs: ci
runs-on: ubuntu-latest
permissions:
pull-requests: write
defaults:
run:
working-directory: ./terraform

- name: Setup Kubectl
uses: azure/setup-kubectl@v3.0
steps:
- name: Checkout code
id: ok
if: env.DEPLOY != 'none'
uses: actions/checkout@v3

- name: "Setup Terraform"
if: steps.ok.outcome == 'success'
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

- name: Deploy to k8s
if: ${{ env.DEPLOY_PROD == 'true' || env.DEPLOY_STAGE == 'true' }}
- name: "Terraform Init"
if: steps.ok.outcome == 'success'
id: init
env:
KUBE_CONFIG_DATA: ${{ (env.DEPLOY_PROD == 'true' && secrets.KUBECONFIG) || secrets.KUBECONFIG_STAGE }}
TF_WORKSPACE: ${{ env.DEPLOY }}
run: terraform init
continue-on-error: true

- name: "Terraform Workspace"
if: steps.ok.outcome == 'success'
run: terraform workspace select -or-create=true ${{ env.DEPLOY }}

- name: Terraform fmt
if: steps.ok.outcome == 'success'
id: fmt
run: terraform fmt -check
continue-on-error: true

- name: Terraform Validate
if: steps.ok.outcome == 'success'
id: validate
run: terraform validate -no-color

- name: Terraform Variables
if: steps.ok.outcome == 'success'
run: |
mkdir -p ~/.kube
(echo $KUBE_CONFIG_DATA | base64 -d) >> ~/.kube/config
cat <<EOF > *.auto.tfvars
image_url="ghcr.io/seventv/api:${{ env.DEPLOY }}-${{ github.sha }}"
image_pull_policy="IfNotPresent"

EOF

- name: "Terraform Plan"
if: steps.ok.outcome == 'success'
id: plan
run: terraform plan -no-color

- uses: actions/github-script@v6
if: steps.ok.outcome == 'success' && github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
})

// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>

\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`

</details>

#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`

<details><summary>Show Plan</summary>

\`\`\`\n
${process.env.PLAN}
\`\`\`

</details>

*Actor: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;

// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}

kubectl apply -f k8s/deploy.yaml
- name: "Terraform Apply"
if: steps.ok.outcome == 'success'
id: apply
run: terraform apply -no-color -auto-approve
continue-on-error: true
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ out/
tmp/
*.yaml
!example.config.yaml
!terraform/config.template.yaml
!docker-compose.yaml
!.github/**/*.yaml
*-gqlgen.go
Expand All @@ -16,3 +17,17 @@ internal/rest/v*/docs
go.work
go.work.sum
!.golangci.yaml

# Terraform local state files
**/.terraform/*
*.tfstate
*.tfstate.*
*.tfplan
crash.log
*.tfvars
override.tf
override.tf.json
*_override.tf
*_override.tf.json
.terraformrc
terraform.rc
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ build:
GOOS=linux GOARCH=amd64 go build -v -ldflags "-X 'main.Version=${VERSION}' -X 'main.Unix=$(shell date +%s)' -X 'main.User=${BUILDER}'" -o out/api cmd/*.go

lint:
golangci-lint run --go=1.18
yarn prettier --check .
# golangci-lint run --go=1.18
# yarn prettier --check .

format:
gofmt -s -w .
Expand Down Expand Up @@ -70,3 +70,9 @@ work:

dev:
go run cmd/main.go

terraform:
terraform -chdir=./terraform init

deploy:
terraform -chdir=./terraform apply -auto-approve
49 changes: 36 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/bugsnag/panicwrap"
"github.com/nats-io/nats.go"
"github.com/seventv/common/mongo"
"github.com/seventv/common/mongo/indexing"
"github.com/seventv/common/redis"
"github.com/seventv/common/svc"
"github.com/seventv/common/svc/s3"
"github.com/seventv/compactdisc"
messagequeue "github.com/seventv/message-queue/go"
"go.uber.org/zap"

"github.com/seventv/api/data/events"
"github.com/seventv/api/data/model"
"github.com/seventv/api/data/mutate"
Expand All @@ -22,6 +32,7 @@ import (
"github.com/seventv/api/internal/configure"
"github.com/seventv/api/internal/global"
"github.com/seventv/api/internal/loaders"
"github.com/seventv/api/internal/search"
"github.com/seventv/api/internal/svc/auth"
"github.com/seventv/api/internal/svc/health"
"github.com/seventv/api/internal/svc/limiter"
Expand All @@ -30,14 +41,6 @@ import (
"github.com/seventv/api/internal/svc/presences"
"github.com/seventv/api/internal/svc/prometheus"
"github.com/seventv/api/internal/svc/youtube"
"github.com/seventv/common/mongo"
"github.com/seventv/common/mongo/indexing"
"github.com/seventv/common/redis"
"github.com/seventv/common/svc"
"github.com/seventv/common/svc/s3"
"github.com/seventv/compactdisc"
messagequeue "github.com/seventv/message-queue/go"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -103,11 +106,16 @@ func main() {
}
}

// INITIALIZE MEILISEARCH
gctx.Inst().Meilisearch = search.New(gctx.Config())

{
gctx.Inst().Mongo, err = mongo.Setup(gctx, mongo.SetupOptions{
URI: config.Mongo.URI,
DB: config.Mongo.DB,
Direct: config.Mongo.Direct,
URI: config.Mongo.URI,
DB: config.Mongo.DB,
Direct: config.Mongo.Direct,
Username: config.Mongo.Username,
Password: config.Mongo.Password,
})
if err != nil {
zap.S().Fatalw("failed to setup mongo handler",
Expand Down Expand Up @@ -182,13 +190,28 @@ func main() {
})
}

nc, err := nats.Connect(config.Nats.Url)
if err != nil {
zap.S().Fatalw("failed to connect to nats",
"error", err,
)
}

defer func() {
err = nc.Drain()
zap.S().Fatalw("failed to drain nats, is connection failing?",
"error", err,
)
}()

gctx.Inst().Events = events.NewPublisher(nc, config.Nats.Subject)

{
id := svc.AppIdentity{
Name: "API",
Web: config.WebsiteURL,
CDN: config.CdnURL,
}
gctx.Inst().Events = events.NewPublisher(gctx, gctx.Inst().Redis)

gctx.Inst().Limiter, err = limiter.New(gctx, gctx.Inst().Redis)
if err != nil {
Expand All @@ -201,7 +224,7 @@ func main() {
CDN: config.CdnURL,
Website: config.WebsiteURL,
})
gctx.Inst().Query = query.New(gctx.Inst().Mongo, gctx.Inst().Redis)
gctx.Inst().Query = query.New(gctx.Inst().Mongo, gctx.Inst().Redis, gctx.Inst().Meilisearch)
gctx.Inst().Loaders = loaders.New(gctx, gctx.Inst().Mongo, gctx.Inst().Redis, gctx.Inst().Query)

gctx.Inst().Mutate = mutate.New(mutate.InstanceOptions{
Expand Down
2 changes: 1 addition & 1 deletion data/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (op Opcode) String() string {
}

func (op Opcode) PublishKey() string {
return fmt.Sprintf("events:%s:%s", "op", strings.ToLower(op.String()))
return fmt.Sprintf("events.%s.%s", "op", strings.ToLower(op.String()))
}

type CloseCode uint16
Expand Down
Loading