diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000000..b1daf7e3ce9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,50 @@ +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.25.1" + + - name: Run unit 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.25.1" + + - 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..902590ed3b5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ + +![CI](https://github.com/ArchAngel77/learn-cicd-starter/actions/workflows/ci.yml/badge.svg) + # 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 +24,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! + +Jordan's version of Boot.dev's Notely app +# trigger diff --git a/internal/auth/auth.go b/internal/auth/auth.go index f969aacf638..834779b283d 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -15,7 +15,7 @@ func GetAPIKey(headers http.Header) (string, error) { return "", ErrNoAuthHeaderIncluded } splitAuth := strings.Split(authHeader, " ") - if len(splitAuth) < 2 || splitAuth[0] != "ApiKey" { + if len(splitAuth) < 2 || splitAuth[0] != "apiKey" { return "", errors.New("malformed authorization header") } diff --git a/internal/auth/get_api_key_test.go b/internal/auth/get_api_key_test.go new file mode 100644 index 00000000000..155a1587083 --- /dev/null +++ b/internal/auth/get_api_key_test.go @@ -0,0 +1,41 @@ +package auth + +import ( + "net/http" + "strings" + "testing" +) + +func TestAPIKey(t *testing.T) { + // 1. Define the 'table' of test cases + tests := []struct { + name string + inputHeader http.Header + expectedKey string + expectedError string + }{ + {name: "valid api key", + inputHeader: http.Header{ + "Authorization": []string{"ApiKey secret-sauce"}, + }, + expectedKey: "secret-sauce", + expectedError: "", + }, + } + + // 2. Iterate over the table + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + apiKey, err := GetAPIKey(tt.inputHeader) + if err != nil { + if !strings.Contains(err.Error(), tt.expectedError) { + t.Errorf("expected error containing %s, got %v", tt.expectedError, err) + } + return + } + if apiKey != tt.expectedKey { + t.Errorf("expected %s, got %s", tt.expectedKey, apiKey) + } + }) + } +} diff --git a/json.go b/json.go index 1e6e7985e18..6f35f2c46b3 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("Critical error writing response: %s" ,err) + } } diff --git a/main.go b/main.go index 19d7366c5f7..b551d341425 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "log" "net/http" "os" + "strconv" + "time" "github.com/go-chi/chi" "github.com/go-chi/cors" @@ -91,8 +93,9 @@ func main() { srv := &http.Server{ Addr: ":" + port, Handler: router, + ReadHeaderTimeout: time.Second * 5, } - log.Printf("Serving on port: %s\n", port) + log.Printf("Serving on port: %s\n", strconv.Quote(port)) log.Fatal(srv.ListenAndServe()) }