diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000000..331e9f7dea9 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,25 @@ +name: cd + +on: + push: + branches: [main] + +jobs: + build-and-deploy: + name: Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: "1.26.0" + + - name: Make script executable + run: chmod +x ./scripts/buildprod.sh + + - 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..16f4d799a47 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,57 @@ +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: Run tests + run: go test ./... -cover + + security-scans: + name: Secuirty Tests + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v6 + + - name: Run Gosec Security Scanner + uses: securego/gosec@master + with: + args: ./... + + lint-and-format: + 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: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@v0.7.0 + + - name: Format code + run: test -z "$(go fmt ./...)" + + - name: Check linting + run: staticcheck ./... diff --git a/README.md b/README.md index c2bec0368b7..2770c01ffb1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![test badge](https://github.com/AmanT776/learn-cicd-starter/actions/workflows/ci.yml/badge.svg)](https://github.com/AmanT776/learn-cicd-starter/actions/workflows/ci.yml) + # 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). @@ -18,6 +20,8 @@ Run the server: go build -o notely && ./notely ``` -*This starts the server in non-database mode.* It will serve a simple webpage at `http://localhost:8080`. +_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! -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! +aman'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..e2892aa7497 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,64 @@ +package auth_test + +import ( + "errors" + "net/http" + "testing" + + "github.com/bootdotdev/learn-cicd-starter/internal/auth" +) + +func TestApiKey(t *testing.T) { + tests := []struct { + name string + headers http.Header + expectedKey string + expectedError error + }{ + { + name: "successfull extraction with ApiKey scheme", + headers: http.Header{ + "Authorization": []string{"ApiKey secret-token-123"}, + }, + expectedKey: "secret-token-123", + expectedError: nil, + }, + { + name: "malformed header - wrong scheme prefix", + headers: http.Header{ + "Authorization": []string{"Bearer secret-token-123"}, + }, + expectedKey: "", + expectedError: errors.New("malformed authorization header"), + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + key, err := auth.GetAPIKey(tc.headers) + + if tc.expectedError != nil { + if err == nil { + t.Fatalf("expected error %v, got nil", tc.expectedError) + } + + // Use errors.Is for sentinel errors, or string comparison for generic errors + if errors.Is(tc.expectedError, auth.ErrNoAuthHeaderIncluded) { + if !errors.Is(err, auth.ErrNoAuthHeaderIncluded) { + t.Errorf("expected sentinel error %v, got %v", tc.expectedError, err) + } + } else if err.Error() != tc.expectedError.Error() { + t.Errorf("expected error message %q, got %q", tc.expectedError.Error(), err.Error()) + } + } else { + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + } + + // 2. Handle key assertions + if key != tc.expectedKey { + t.Errorf("expected key %q, got %q", tc.expectedKey, key) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e18..f9d0b6e1eb5 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.Println(err) + } } diff --git a/main.go b/main.go index 19d7366c5f7..8af883330af 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,13 @@ package main import ( "database/sql" "embed" + "fmt" "io" "log" + "log/slog" "net/http" "os" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -25,6 +28,9 @@ type apiConfig struct { var staticFiles embed.FS func main() { + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ + Level: slog.LevelInfo, + })) err := godotenv.Load(".env") if err != nil { log.Printf("warning: assuming default configuration. .env unreadable: %v", err) @@ -89,10 +95,11 @@ func main() { router.Mount("/v1", v1Router) srv := &http.Server{ - Addr: ":" + port, - Handler: router, + Addr: ":" + port, + Handler: router, + ReadHeaderTimeout: 3 * time.Second, } - log.Printf("Serving on port: %s\n", port) + logger.Info(fmt.Sprintf("Serving on port: %s\n", port)) log.Fatal(srv.ListenAndServe()) }