From 0b310eeb064ba494854381509b87b210a2b7b7c5 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sat, 22 Feb 2025 17:35:39 +0000 Subject: [PATCH 01/20] Add personal line to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c2bec0368b..d9e20d4fa7 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,5 @@ go build -o notely && ./notely *This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`. You do *not* need to set up a database or any interactivity on the webpage yet. Instructions for that will come later in the course! + +VORTEX's version of Boot.dev's Notely app. \ No newline at end of file From 29b8d16c36d1b3af4b39647467d32b062c7cba04 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sat, 22 Feb 2025 18:27:58 +0000 Subject: [PATCH 02/20] Added CI Tests --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..99ceb2f807 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,22 @@ +name: ci + +on: + pull_request: + branches: [main] + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Force Failure + run: (exit 1) From 6b92ae30f684cd191c8edc27938f76be7215946d Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sat, 22 Feb 2025 18:39:28 +0000 Subject: [PATCH 03/20] Updated ci.yml --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99ceb2f807..776c8444e2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,5 +18,5 @@ jobs: with: go-version: "1.23.0" - - name: Force Failure - run: (exit 1) + - name: Verify Go Version + run: go version From 472ace8fd97893a62575f17372fc107843424e92 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 05:34:02 +0000 Subject: [PATCH 04/20] Update ci.yml --- .github/workflows/ci.yml | 2 +- internal/auth/auth_test.go | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 internal/auth/auth_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 776c8444e2..902d88b315 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.23.0" - name: Verify Go Version - run: go version + run: go test ./... diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..034dcfaeed --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,47 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + tests := []struct { + name string + headers http.Header + wantKey string + wantErr error + }{ + { + name: "Valid API key", + headers: http.Header{ + "Authorization": []string{"ApiKey testkey123"}, + }, + wantKey: "testkey123", + wantErr: nil, + }, + { + name: "No authorization header", + headers: http.Header{}, + wantKey: "", + wantErr: ErrNoAuthHeaderIncluded, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotKey, err := GetAPIKey(tt.headers) + + // Check error + if err != tt.wantErr { + t.Errorf("GetAPIKey() error = %v, wantErr %v", err, tt.wantErr) + return + } + + // Check key + if gotKey != tt.wantKey { + t.Errorf("GetAPIKey() gotKey = %v, want %v", gotKey, tt.wantKey) + } + }) + } +} From cad4fa58e6c90aa337c29514dd6f93c351ee1955 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 05:41:29 +0000 Subject: [PATCH 05/20] Test's Coverage Added to ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 902d88b315..fe199f3b32 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,4 @@ jobs: go-version: "1.23.0" - name: Verify Go Version - run: go test ./... + run: go test ./... -cover From 73a762edf84df03ff75d979623005ee85c7f820b Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 06:50:23 +0000 Subject: [PATCH 06/20] Update Style Tests ci.yml --- .github/workflows/ci.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fe199f3b32..f0108bb8ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,3 +20,19 @@ jobs: - name: Verify Go Version run: go test ./... -cover + + style: + name: Style + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Run formatting check + run: test -z "$(go fmt ./...)" \ No newline at end of file From 34825cbbe46e450ec7354cccdfd2bb1e1860ec00 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 07:02:10 +0000 Subject: [PATCH 07/20] Linting Check --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0108bb8ad..c5e3c3020b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,4 +35,7 @@ jobs: go-version: "1.23.0" - name: Run formatting check - run: test -z "$(go fmt ./...)" \ No newline at end of file + run: test -z "$(go fmt ./...)" + + - name: Install staticcheck + run: go run honnef.co/go/tools/cmd/staticcheck@latest ./... \ No newline at end of file From 69cfd79aa00ddab5bc7601a04421d02cac2aa1cc Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 07:41:33 +0000 Subject: [PATCH 08/20] Sec Check --- .github/workflows/ci.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c5e3c3020b..a80ba1cab3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,4 +38,10 @@ jobs: run: test -z "$(go fmt ./...)" - name: Install staticcheck - run: go run honnef.co/go/tools/cmd/staticcheck@latest ./... \ No newline at end of file + run: go run honnef.co/go/tools/cmd/staticcheck@latest ./... + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run gosec + run: gosec ./... \ No newline at end of file From 1de1e2e57cfccfa47519b621e73cdf3aefc2a5a2 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 07:51:26 +0000 Subject: [PATCH 09/20] Fixing Sec Checks --- json.go | 5 ++++- main.go | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/json.go b/json.go index e346ef4093..fd41eb7cbd 100644 --- a/json.go +++ b/json.go @@ -27,5 +27,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { return } w.WriteHeader(code) - w.Write(dat) + _, err = w.Write(dat) + if err != nil { + log.Printf("Error writing JSON response: %v", err) + } } diff --git a/main.go b/main.go index 19d7366c5f..72873fee93 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -89,8 +90,9 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 10 * time.Second, } log.Printf("Serving on port: %s\n", port) From 260d1c3c66a317cb573f0f87e05f7cfd5e59f674 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 10:19:48 +0000 Subject: [PATCH 10/20] Update CD with GCP --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a80ba1cab3..750314d5e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,4 +44,7 @@ jobs: run: go install github.com/securego/gosec/v2/cmd/gosec@latest - name: Run gosec - run: gosec ./... \ No newline at end of file + run: gosec ./... + + - name: Deploy to Cloud Run + run: gcloud run deploy notely --image us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-451808 --max-instances=4 \ No newline at end of file From 41267b505321250c5d82a8b5449e47a5830cb508 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 10:30:23 +0000 Subject: [PATCH 11/20] Update Title --- static/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/index.html b/static/index.html index 72be101028..5d4ad73c09 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -

Notely

+

Welcome to Notely

From a63f8f321045e3204a8a729ed170f07f05fcda0a Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 10:35:23 +0000 Subject: [PATCH 12/20] Update CD --- .github/workflows/ci.yml | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 750314d5e3..2868bfcdba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,6 +45,22 @@ jobs: - name: Run gosec run: gosec ./... - + + - name: Authenticate with Google Cloud + uses: google-github-actions/auth@v2 + with: + credentials_json: ${{ secrets.GCP_CREDENTIALS }} + + - name: Set up gcloud CLI + uses: google-github-actions/setup-gcloud@v2 + with: + project_id: notely-451808 + - name: Deploy to Cloud Run - run: gcloud run deploy notely --image us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-451808 --max-instances=4 \ No newline at end of file + run: | + gcloud run deploy notely \ + --image us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest \ + --region us-central1 \ + --allow-unauthenticated \ + --project notely-451808 \ + --max-instances=4 From 6f5d2436167b4f2477859e1a61be453589566e0c Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 10:49:40 +0000 Subject: [PATCH 13/20] update h1 --- static/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/index.html b/static/index.html index 5d4ad73c09..6770d39220 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -

Welcome to Notely

+

Welcome to Notely!

From 5819a17871fa5eb1023d02a271a1d387ec9b3dbc Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 10:54:47 +0000 Subject: [PATCH 14/20] Update CD --- .github/workflows/ci.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2868bfcdba..e70e3e4d48 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,8 @@ name: ci on: + push: + branches: [main] pull_request: branches: [main] @@ -46,6 +48,15 @@ jobs: - name: Run gosec run: gosec ./... + deploy: + name: Deploy to Cloud Run + runs-on: ubuntu-latest + needs: [tests, style] + + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Authenticate with Google Cloud uses: google-github-actions/auth@v2 with: @@ -56,6 +67,10 @@ jobs: with: project_id: notely-451808 + - name: Build and Push Docker Image + run: | + gcloud builds submit --tag us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest . + - name: Deploy to Cloud Run run: | gcloud run deploy notely \ From b17f85d020e8a8608c523732fc4311bdbbc27c66 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 11:07:35 +0000 Subject: [PATCH 15/20] update cd1 --- .github/workflows/ci.yml | 42 ++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e70e3e4d48..f180fe04c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: ci +name: CI/CD Pipeline on: push: @@ -8,7 +8,7 @@ on: jobs: tests: - name: Tests + name: Run Tests runs-on: ubuntu-latest steps: @@ -20,11 +20,19 @@ jobs: with: go-version: "1.23.0" - - name: Verify Go Version + - name: Cache Go modules + uses: actions/cache@v4 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Run Tests with Coverage run: go test ./... -cover style: - name: Style + name: Lint & Security Checks runs-on: ubuntu-latest steps: @@ -36,23 +44,20 @@ jobs: with: go-version: "1.23.0" - - name: Run formatting check + - name: Run Formatting Check run: test -z "$(go fmt ./...)" - - name: Install staticcheck - run: go run honnef.co/go/tools/cmd/staticcheck@latest ./... + - name: Install & Run Staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck ./... - - name: Install gosec - run: go install github.com/securego/gosec/v2/cmd/gosec@latest - - - name: Run gosec - run: gosec ./... + - name: Install & Run gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest && gosec ./... deploy: - name: Deploy to Cloud Run + name: Build & Deploy to Cloud Run runs-on: ubuntu-latest needs: [tests, style] - + steps: - name: Check out code uses: actions/checkout@v4 @@ -67,9 +72,12 @@ jobs: with: project_id: notely-451808 - - name: Build and Push Docker Image + - name: Build & Push Docker Image + env: + IMAGE_TAG: us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:${{ github.sha }} run: | - gcloud builds submit --tag us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest . + gcloud builds submit --tag $IMAGE_TAG . + gcloud container images add-tag $IMAGE_TAG us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest - name: Deploy to Cloud Run run: | @@ -78,4 +86,4 @@ jobs: --region us-central1 \ --allow-unauthenticated \ --project notely-451808 \ - --max-instances=4 + --max-instances=4 \ No newline at end of file From 8ecb77d1eae3a0aad80f89050f92ed0a083695b2 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 11:12:05 +0000 Subject: [PATCH 16/20] update cd2 --- .github/workflows/ci.yml | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f180fe04c6..a9fce7d4b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,19 +20,11 @@ jobs: with: go-version: "1.23.0" - - name: Cache Go modules - uses: actions/cache@v4 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - name: Run Tests with Coverage run: go test ./... -cover style: - name: Lint & Security Checks + name: Code Style & Security Checks runs-on: ubuntu-latest steps: @@ -47,14 +39,20 @@ jobs: - name: Run Formatting Check run: test -z "$(go fmt ./...)" - - name: Install & Run Staticcheck - run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck ./... + - name: Install Staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run Staticcheck + run: staticcheck ./... - - name: Install & Run gosec - run: go install github.com/securego/gosec/v2/cmd/gosec@latest && gosec ./... + - name: Install Gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run Gosec Security Check + run: gosec ./... deploy: - name: Build & Deploy to Cloud Run + name: Deploy to Cloud Run runs-on: ubuntu-latest needs: [tests, style] @@ -67,12 +65,20 @@ jobs: with: credentials_json: ${{ secrets.GCP_CREDENTIALS }} - - name: Set up gcloud CLI + - name: Set up Google Cloud SDK uses: google-github-actions/setup-gcloud@v2 with: project_id: notely-451808 - - name: Build & Push Docker Image + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.23.0" + + - name: Build Go Binary + run: go build -o notely . + + - name: Build and Push Docker Image env: IMAGE_TAG: us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:${{ github.sha }} run: | From 5222a5377256b7e89df9ebbddba92a16d78447b7 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 11:15:11 +0000 Subject: [PATCH 17/20] ! ? --- static/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/index.html b/static/index.html index 6770d39220..5d4ad73c09 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -

Welcome to Notely!

+

Welcome to Notely

From f909ae2c3faec8a7b05ed806687022bb15efe6f0 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 11:42:54 +0000 Subject: [PATCH 18/20] Database Integration --- .github/workflows/ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9fce7d4b2..48d1f44b6b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,9 @@ jobs: runs-on: ubuntu-latest needs: [tests, style] + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + steps: - name: Check out code uses: actions/checkout@v4 @@ -85,6 +88,13 @@ jobs: gcloud builds submit --tag $IMAGE_TAG . gcloud container images add-tag $IMAGE_TAG us-central1-docker.pkg.dev/notely-451808/notely-ar-repo/notely:latest + - name: Install Goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Run Database Migrations + run: | + goose -dir migrations sqlite3 "$DATABASE_URL" up + - name: Deploy to Cloud Run run: | gcloud run deploy notely \ From 53df0a042838ac06639d7fec8dc24620bc11b4ce Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 11:55:37 +0000 Subject: [PATCH 19/20] update dbmigration --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 48d1f44b6b..19ab5bf3fb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,9 +56,6 @@ jobs: runs-on: ubuntu-latest needs: [tests, style] - env: - DATABASE_URL: ${{ secrets.DATABASE_URL }} - steps: - name: Check out code uses: actions/checkout@v4 @@ -92,8 +89,11 @@ jobs: run: go install github.com/pressly/goose/v3/cmd/goose@latest - name: Run Database Migrations + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} run: | - goose -dir migrations sqlite3 "$DATABASE_URL" up + chmod +x scripts/migrateup.sh + ./scripts/migrateup.sh - name: Deploy to Cloud Run run: | From 3375f221e11a99f8f0a67d2cdcaf720f89e44fb7 Mon Sep 17 00:00:00 2001 From: Lonwwolf14 Date: Sun, 23 Feb 2025 12:11:29 +0000 Subject: [PATCH 20/20] secretManager --- .github/workflows/ci.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19ab5bf3fb..4cdd40114b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,9 +88,12 @@ jobs: - name: Install Goose run: go install github.com/pressly/goose/v3/cmd/goose@latest + - name: Fetch Database URL from Secret Manager + run: | + gcloud secrets versions access latest --secret=notely_db_password > db_url.txt + echo "DATABASE_URL=$(cat db_url.txt)" >> $GITHUB_ENV + - name: Run Database Migrations - env: - DATABASE_URL: ${{ secrets.DATABASE_URL }} run: | chmod +x scripts/migrateup.sh ./scripts/migrateup.sh