Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add personal line to README #1165

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
108 changes: 108 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: CI/CD Pipeline

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
tests:
name: Run 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: Run Tests with Coverage
run: go test ./... -cover

style:
name: Code Style & Security Checks
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 ./...)"

- name: Install Staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run Staticcheck
run: staticcheck ./...

- name: Install Gosec
run: go install github.com/securego/gosec/v2/cmd/gosec@latest

- name: Run Gosec Security Check
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:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}

- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v2
with:
project_id: notely-451808

- 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: |
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: 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
run: |
chmod +x scripts/migrateup.sh
./scripts/migrateup.sh

- 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
47 changes: 47 additions & 0 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
5 changes: 4 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/cors"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>

<body class="section">
<h1>Notely</h1>
<h1>Welcome to Notely</h1>

<div id="userCreationContainer" class="section">
<input id="nameField" type="text" placeholder="Enter your name">
Expand Down