diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..14f4b2be7d8 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,45 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.25.1" + + - name: Install goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Run build script + run: ./scripts/buildprod.sh + + - name: Run migration + run: ./scripts/migrateup.sh + + - id: auth + uses: google-github-actions/auth@v2 + with: + credentials_json: ${{ secrets.GCP_CREDENTIALS }} + + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@v3 + + - name: Use gcloud CLI + run: gcloud builds submit --tag europe-west6-docker.pkg.dev/notely-484713/notely-ar-repo/notely:latest . + + - name: Deploy to Cloud Run + run: gcloud run deploy notely --image europe-west6-docker.pkg.dev/notely-484713/notely-ar-repo/notely:latest --region europe-west6 --allow-unauthenticated --project notely-484713 --max-instances=4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..eedf25276fc --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,50 @@ +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.25.1" + + - name: Run Go tests + run: go test -cover ./... + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run gosec + run: gosec ./... + + 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.25.1" + + - name: Format Go code + run: test -z $(go fmt ./...) + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Run staticcheck + run: staticcheck ./... diff --git a/README.md b/README.md index c2bec0368b7..666a5a63e3c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![](https://github.com/gabyrod7/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) + # learn-cicd-starter (Notely) This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). @@ -21,3 +23,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! + +Gabriel's version of Boot.dev's Notely app. diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 00000000000..b14b13a6fc3 --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,48 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey_ValidHeader(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "ApiKey abc123") + + key, err := GetAPIKey(h) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if key != "abc123" { + t.Fatalf("expected key %q, got %q", "abc123", key) + } +} + +func TestGetAPIKey_MissingHeader(t *testing.T) { + h := http.Header{} + + _, err := GetAPIKey(h) + if err != ErrNoAuthHeaderIncluded { + t.Fatalf("expected ErrNoAuthHeaderIncluded, got %v", err) + } +} + +func TestGetAPIKey_WrongScheme(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "Bearer abc123") + + _, err := GetAPIKey(h) + if err == nil { + t.Fatal("expected error, got nil") + } +} + +func TestGetAPIKey_MalformedHeader(t *testing.T) { + h := http.Header{} + h.Set("Authorization", "ApiKey") + + _, err := GetAPIKey(h) + if err == nil { + t.Fatal("expected error, got nil") + } +} diff --git a/json.go b/json.go index 1e6e7985e18..9825dee3720 100644 --- a/json.go +++ b/json.go @@ -30,5 +30,9 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { return } w.WriteHeader(code) - w.Write(dat) + if _, err := w.Write(dat); err != nil { + // At this point headers are already sent, so we can only log. + log.Printf("failed to write response: %v", err) + } + } diff --git a/main.go b/main.go index 19d7366c5f7..0a4a9eb7a48 100644 --- a/main.go +++ b/main.go @@ -89,8 +89,9 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 5_000_000_000, // nanoseconds } log.Printf("Serving on port: %s\n", port) diff --git a/static/index.html b/static/index.html index 72be101028c..5d4ad73c095 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -

Notely

+

Welcome to Notely