diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..41056f51c95 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,22 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + deploy: + name: Deploy + 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: Build app + run: ./scripts/buildprod.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..d9189805871 --- /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@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.26.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 + 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 formatting + 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..2ec4c51c7ae 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # learn-cicd-starter (Notely) +![Tests](https://github.com/nonso7/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) + This repo contains the starter code for the "Notely" application for the "Learn CICD" course on [Boot.dev](https://boot.dev). ## Local Development @@ -21,3 +23,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! +"Chinonso Nwajagu 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..38988a15da4 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,75 @@ +package auth + +import ( + "errors" + "net/http" + "testing" +) + +func TestGetAPIKey(t *testing.T) { + tests := []struct { + name string + headers http.Header + expectedKey string + expectedErr error + }{ + { + 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 key", + headers: http.Header{ + "Authorization": []string{"ApiKey"}, + }, + expectedKey: "", + expectedErr: errors.New("malformed authorization header"), + }, + { + name: "malformed header wrong scheme", + headers: http.Header{ + "Authorization": []string{"Bearer my-secret-key"}, + }, + expectedKey: "", + expectedErr: errors.New("malformed authorization header"), + }, + { + name: "empty authorization header value", + headers: http.Header{ + "Authorization": []string{""}, + }, + expectedKey: "", + expectedErr: ErrNoAuthHeaderIncluded, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + key, err := GetAPIKey(tc.headers) + + if key != tc.expectedKey { + t.Errorf("expected key %q, got %q", tc.expectedKey, key) + } + + if tc.expectedErr == nil && err != nil { + t.Errorf("expected no error, got %v", err) + } + if tc.expectedErr != nil && err == nil { + t.Errorf("expected error %v, got nil", tc.expectedErr) + } + if tc.expectedErr != nil && err != nil && err.Error() != tc.expectedErr.Error() { + t.Errorf("expected error %q, got %q", tc.expectedErr.Error(), err.Error()) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e18..0f8075d808a 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: %s", err) + } } diff --git a/main.go b/main.go index 19d7366c5f7..9045915e748 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,13 @@ package main import ( "database/sql" "embed" + "fmt" "io" "log" "net/http" "os" + "strconv" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -30,10 +33,14 @@ func main() { log.Printf("warning: assuming default configuration. .env unreadable: %v", err) } - port := os.Getenv("PORT") - if port == "" { + portStr := os.Getenv("PORT") + if portStr == "" { log.Fatal("PORT environment variable is not set") } + port, err := strconv.Atoi(portStr) + if err != nil { + log.Fatalf("PORT environment variable is not a valid number: %v", err) + } apiCfg := apiConfig{} @@ -89,10 +96,11 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: fmt.Sprintf(":%d", port), + Handler: router, + ReadHeaderTimeout: 10 * time.Second, } - log.Printf("Serving on port: %s\n", port) + log.Printf("Serving on port: %d\n", port) log.Fatal(srv.ListenAndServe()) }