diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..2a7a38a81b0 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,47 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + Deploy: + runs-on: ubuntu-latest + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.22' + + - name: Install goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Build + run: ./scripts/buildprod.sh + + - name: database migration + run: ./scripts/migrateup.sh + + - name: Authenticate to Google Cloud + uses: google-github-actions/auth@v2 + with: + credentials_json: ${{ secrets.GCP_CREDENTIALS }} + + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@v2 + + - name: Configure Docker for Artifact Registry + run: gcloud auth configure-docker us-central1-docker.pkg.dev + + - name: gcloud builds submit + run: gcloud builds submit --tag us-central1-docker.pkg.dev/notely-499416/notely-ar-repo/notely:latest + + - name: Deploy to Cloud Run + run: gcloud run deploy notely --image us-central1-docker.pkg.dev/notely-499416/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-499416 --max-instances=4 + + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..dfe61d61d17 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,47 @@ +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.26.0" + + - name: go test + 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.26.0" + + - name: Check for Formatting Issues + run: test -z $(go fmt ./...) + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest diff --git a/README.md b/README.md index c2bec0368b7..e79531a3f96 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # 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 +22,4 @@ 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! +Bartlomiej's version of Boot.dev's Notely app. diff --git a/internal/auth/get_api_key_test.go b/internal/auth/get_api_key_test.go new file mode 100644 index 00000000000..0dc2ed93871 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,57 @@ +package auth + +import ( + "errors" + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + type testCase struct { + name string + headers http.Header + expectedKey string + expectedErr error + } + + tests := []testCase{ + { + name: "no authorization header", + headers: http.Header{}, + expectedKey: "", + expectedErr: ErrNoAuthHeaderIncluded, + }, + { + name: "valid API key", + headers: http.Header{ + "Authorization": []string{"ApiKey my-secret-key"}, + }, + expectedKey: "my-secret-key", + expectedErr: nil, + }, + { + name: "malformed header - missing ApiKey prefix", + headers: http.Header{ + "Authorization": []string{"Bearer my-secret-key"}, + }, + expectedKey: "", + expectedErr: errors.New("malformed authorization header"), + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + key, err := GetAPIKey(tc.headers) + + if tc.expectedErr != nil && err == nil { + t.Errorf("expected error %q, got none", tc.expectedErr) + } + if tc.expectedErr == nil && err != nil { + t.Errorf("unexpected error: %v", err) + } + if key != tc.expectedKey { + t.Errorf("expected key %q, got %q", tc.expectedKey, key) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e18..5be70226eb8 100644 --- a/json.go +++ b/json.go @@ -30,5 +30,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { return } w.WriteHeader(code) - w.Write(dat) + + if _, err := w.Write(dat); err != nil { + log.Printf("error writing response: %v", err) + } } diff --git a/main.go b/main.go index 19d7366c5f7..b3f4da8f3e5 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "log" "net/http" "os" + "strings" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -89,10 +91,12 @@ 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) + log.Printf("Serving on port: %s\n", strings.ReplaceAll(port, "\n", "")) log.Fatal(srv.ListenAndServe()) + } diff --git a/static/index.html b/static/index.html index 72be101028c..2cde6bcd54b 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@
-