diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000000..47075e07f9 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,60 @@ +name: CD + +on: + push: + branches: + - main + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + + env: + DATABASE_URL: ${{ secrets.DATABASE_URL }} + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + + - name: Build production binary + run: | + chmod +x scripts/buildprod.sh + ./scripts/buildprod.sh + + - name: Authenticate to GCP + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GCP_CREDENTIALS }} + + - name: Set up gcloud CLI + uses: google-github-actions/setup-gcloud@v2 + with: + project_id: notely-469212 + version: 'latest' + + - name: Build and Push Docker image + run: | + gcloud builds submit \ + --tag us-central1-docker.pkg.dev/notely-469212/notely-ar-repo/my-app:latest + + + - name: Install goose + run: go install github.com/pressly/goose/v3/cmd/goose@latest + + - name: Run migrations + run: ./scripts/migrateup.sh + + - name: Deploy to Cloud Run + run: | + gcloud run deploy notely \ + --image us-central1-docker.pkg.dev/notely-469212/notely-ar-repo/my-app:latest \ + --region us-central1 \ + --allow-unauthenticated \ + --project notely-469212 \ + --max-instances=4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000000..5ef19b1351 --- /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.23.0" + + - name: Run tests + run: go test -cover ./... + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Run gosec security scan + 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.23.0" + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Lint with Staticcheck + run: staticcheck ./... diff --git a/Dockerfile b/Dockerfile index 2be3d18b81..09521c4ceb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,10 @@ FROM --platform=linux/amd64 debian:stable-slim RUN apt-get update && apt-get install -y ca-certificates -ADD notely /usr/bin/notely +WORKDIR /app + +COPY notely ./notely +COPY static ./static + +ENTRYPOINT ["./notely"] -CMD ["notely"] diff --git a/README.md b/README.md index c2bec0368b..22d4aa222b 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,10 @@ 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! + + +wewe's version of Boot.dev's Notely app. +عملت ميرج قبل ما اعمل تيست جد اني وردة + + +![CI Tests](https://github.com/lamakhaledd/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) \ No newline at end of file diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 0000000000..336dc9dc7e --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,38 @@ +package auth + +import ( + "net/http" + "testing" +) + +func TestGetAPIKey_Valid(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "ApiKey 12345") + + apiKey, err := GetAPIKey(headers) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if apiKey != "12345" { + t.Errorf("expected apiKey '12345', got %s", apiKey) + } +} + +func TestGetAPIKey_MissingHeader(t *testing.T) { + headers := http.Header{} + + _, err := GetAPIKey(headers) + if err != ErrNoAuthHeaderIncluded { + t.Errorf("expected ErrNoAuthHeaderIncluded, got %v", err) + } +} + +func TestGetAPIKey_InvalidFormat(t *testing.T) { + headers := http.Header{} + headers.Set("Authorization", "Bearer token123") + + _, err := GetAPIKey(headers) + if err == nil || err.Error() != "malformed authorization header" { + t.Errorf("expected malformed authorization header error, got %v", err) + } +} diff --git a/json.go b/json.go index 1e6e7985e1..527d6d010e 100644 --- a/json.go +++ b/json.go @@ -30,5 +30,7 @@ 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 19d7366c5f..e15f377234 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" @@ -24,6 +25,7 @@ type apiConfig struct { //go:embed static/* var staticFiles embed.FS + func main() { err := godotenv.Load(".env") if err != nil { @@ -89,10 +91,12 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 5 * time.Second, } + log.Printf("Serving on port: %s\n", port) log.Fatal(srv.ListenAndServe()) } diff --git a/static/index.html b/static/index.html index 72be101028..15f0ecb997 100644 --- a/static/index.html +++ b/static/index.html @@ -3,11 +3,11 @@ - Notely + Welcome to Notely -

Notely

+

Welcome to Notely

diff --git a/test.tar b/test.tar new file mode 100644 index 0000000000..dd3d734076 Binary files /dev/null and b/test.tar differ