diff --git a/.github/.DS_Store b/.github/.DS_Store new file mode 100644 index 0000000000..a7c529c749 Binary files /dev/null and b/.github/.DS_Store differ diff --git a/.github/workflows/.DS_Store b/.github/workflows/.DS_Store new file mode 100644 index 0000000000..df20869dcb Binary files /dev/null and b/.github/workflows/.DS_Store differ diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000000..0fc87e4df6 --- /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@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Install goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Build production binary + run: ./scripts/buildprod.sh + + - id: "auth" + 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@v3" + + - name: "Build and push Docker image" + run: "gcloud builds submit --tag northamerica-northeast1-docker.pkg.dev/notely-504618/notely-ar-repo/notely:latest ." + + - name: Run database migrations + run: ./scripts/migrateup.sh + + - name: Deploy to Cloud Run + run: gcloud run deploy notely --image northamerica-northeast1-docker.pkg.dev/notely-504618/notely-ar-repo/notely:latest --region us-central1 --allow-unauthenticated --project notely-504618 --max-instances=4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..fdd6239458 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,51 @@ +name: CI + +on: + pull_request: + branches: [main] + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Testing + 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@v6 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.26.0" + + - name: Formatting + run: test -z $(go fmt ./...) + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Staticcheck + run: staticcheck ./... + diff --git a/.gitignore b/.gitignore index 2092f54e78..ae28bb60c3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ out .env learn-cicd-starter notely +key.json diff --git a/FILE_NAME b/FILE_NAME new file mode 100644 index 0000000000..7d99693937 --- /dev/null +++ b/FILE_NAME @@ -0,0 +1,12 @@ + + + + + +
404. That’s an error. +
That’s all we know. diff --git a/README.md b/README.md index c2bec0368b..ac0dc1db27 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,6 @@ 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! + + + Aycoi's version of Boot.dev's Notely app \ No newline at end of file diff --git a/Untitled b/Untitled new file mode 160000 index 0000000000..a759e37288 --- /dev/null +++ b/Untitled @@ -0,0 +1 @@ +Subproject commit a759e37288a16648b2d73e96b0a07ba52d85ad90 diff --git a/internal/auth/get_api_key_test.go b/internal/auth/get_api_key_test.go new file mode 100644 index 0000000000..fbfaef09b4 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,53 @@ +package auth + +import ( + "errors" + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + tests := map[string]struct { + headers http.Header + wantKey string + wantErr error + }{ + "valid api key": { + headers: http.Header{"Authorization": []string{"ApiKey my-secret-key"}}, + wantKey: "my-secret-key", + wantErr: nil, + }, + "no auth header": { + headers: http.Header{}, + wantKey: "", + wantErr: ErrNoAuthHeaderIncluded, + }, + "malformed - wrong prefix": { + headers: http.Header{"Authorization": []string{"Bearer my-secret-key"}}, + wantKey: "", + wantErr: errors.New("malformed authorization header"), + }, + "malformed - missing key": { + headers: http.Header{"Authorization": []string{"ApiKey"}}, + wantKey: "", + wantErr: errors.New("malformed authorization header"), + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + gotKey, gotErr := GetAPIKey(tc.headers) + + if gotKey != tc.wantKey { + t.Errorf("expected key %q, got %q", tc.wantKey, gotKey) + } + if (gotErr == nil) != (tc.wantErr == nil) { + t.Errorf("expected error %v, got %v", tc.wantErr, gotErr) + return + } + if gotErr != nil && gotErr.Error() != tc.wantErr.Error() { + t.Errorf("expected error %q, got %q", tc.wantErr.Error(), gotErr.Error()) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e1..c72d9a4f36 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) + _, err = w.Write(dat) + if err != nil { + log.Printf("Error writing response: %s", err) + } } diff --git a/macOS b/macOS new file mode 100644 index 0000000000..7d99693937 --- /dev/null +++ b/macOS @@ -0,0 +1,12 @@ + + + + + +
404. That’s an error. +
That’s all we know. diff --git a/macOS64-bitmacOS64-bit b/macOS64-bitmacOS64-bit new file mode 100644 index 0000000000..7d99693937 --- /dev/null +++ b/macOS64-bitmacOS64-bit @@ -0,0 +1,12 @@ + + + + + +
404. That’s an error. +
That’s all we know. diff --git a/main.go b/main.go index 19d7366c5f..a8c0c6efeb 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" @@ -31,6 +33,8 @@ func main() { } port := os.Getenv("PORT") + port = strings.ReplaceAll(port, "\n", "") + port = strings.ReplaceAll(port, "\r", "") if port == "" { log.Fatal("PORT environment variable is not set") } @@ -89,10 +93,12 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + ReadHeaderTimeout: 2 * time.Second, // Time allowed to read request headers + Addr: ":" + port, + Handler: router, } log.Printf("Serving on port: %s\n", port) log.Fatal(srv.ListenAndServe()) + } 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 @@
-